Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / mdt / mdt_io.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) 2017, Intel Corporation.
24  */
25 /*
26  * lustre/mdt/mdt_io.c
27  *
28  * Author: Mikhail Pershin <mike.pershin@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_FILTER
32
33 #include <dt_object.h>
34 #include "mdt_internal.h"
35
36 /* functions below are stubs for now, they will be implemented with
37  * grant support on MDT */
38 static inline void mdt_dom_read_lock(struct mdt_object *mo)
39 {
40         down_read(&mo->mot_dom_sem);
41 }
42
43 static inline void mdt_dom_read_unlock(struct mdt_object *mo)
44 {
45         up_read(&mo->mot_dom_sem);
46 }
47
48 static inline void mdt_dom_write_lock(struct mdt_object *mo)
49 {
50         down_write(&mo->mot_dom_sem);
51 }
52
53 static inline void mdt_dom_write_unlock(struct mdt_object *mo)
54 {
55         up_write(&mo->mot_dom_sem);
56 }
57
58 static void mdt_dom_resource_prolong(struct ldlm_prolong_args *arg)
59 {
60         struct ldlm_resource *res;
61         struct ldlm_lock *lock;
62
63         ENTRY;
64
65         res = ldlm_resource_get(arg->lpa_export->exp_obd->obd_namespace, NULL,
66                                 &arg->lpa_resid, LDLM_IBITS, 0);
67         if (IS_ERR(res)) {
68                 CDEBUG(D_DLMTRACE,
69                        "Failed to get resource for resid %llu/%llu\n",
70                        arg->lpa_resid.name[0], arg->lpa_resid.name[1]);
71                 RETURN_EXIT;
72         }
73
74         lock_res(res);
75         list_for_each_entry(lock, &res->lr_granted, l_res_link) {
76                 if (ldlm_has_dom(lock)) {
77                         LDLM_DEBUG(lock, "DOM lock to prolong ");
78                         ldlm_lock_prolong_one(lock, arg);
79                         /* only one PW or EX lock can be granted,
80                          * no need to continue search
81                          */
82                         if (lock->l_granted_mode & (LCK_PW | LCK_EX))
83                                 break;
84                 }
85         }
86         unlock_res(res);
87         ldlm_resource_putref(res);
88
89         EXIT;
90 }
91
92 static void mdt_prolong_dom_lock(struct tgt_session_info *tsi,
93                                  struct ldlm_prolong_args *data)
94 {
95         struct obdo *oa = &tsi->tsi_ost_body->oa;
96         struct ldlm_lock *lock;
97
98         ENTRY;
99
100         data->lpa_timeout = prolong_timeout(tgt_ses_req(tsi));
101         data->lpa_export = tsi->tsi_exp;
102         data->lpa_resid = tsi->tsi_resid;
103
104         CDEBUG(D_RPCTRACE, "Prolong DOM lock for req %p with x%llu\n",
105                tgt_ses_req(tsi), tgt_ses_req(tsi)->rq_xid);
106
107         if (oa->o_valid & OBD_MD_FLHANDLE) {
108                 /* mostly a request should be covered by only one lock, try
109                  * fast path. */
110                 lock = ldlm_handle2lock(&oa->o_handle);
111                 if (lock != NULL) {
112                         LASSERT(lock->l_export == data->lpa_export);
113                         ldlm_lock_prolong_one(lock, data);
114                         lock->l_last_used = ktime_get();
115                         LDLM_LOCK_PUT(lock);
116                         if (data->lpa_locks_cnt > 0)
117                                 RETURN_EXIT;
118                 }
119         }
120         mdt_dom_resource_prolong(data);
121         EXIT;
122 }
123
124 static int mdt_rw_hpreq_lock_match(struct ptlrpc_request *req,
125                                    struct ldlm_lock *lock)
126 {
127         struct obd_ioobj *ioo;
128         enum ldlm_mode mode;
129         __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
130
131         ENTRY;
132
133         if (!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM))
134                 RETURN(0);
135
136         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
137         LASSERT(ioo != NULL);
138
139         LASSERT(lock->l_resource != NULL);
140         if (!fid_res_name_eq(&ioo->ioo_oid.oi_fid, &lock->l_resource->lr_name))
141                 RETURN(0);
142
143         /* a bulk write can only hold a reference on a PW extent lock. */
144         mode = LCK_PW | LCK_GROUP;
145         if (opc == OST_READ)
146                 /* whereas a bulk read can be protected by either a PR or PW
147                  * extent lock */
148                 mode |= LCK_PR;
149
150         if (!(lock->l_granted_mode & mode))
151                 RETURN(0);
152
153         RETURN(1);
154 }
155
156 static int mdt_rw_hpreq_check(struct ptlrpc_request *req)
157 {
158         struct tgt_session_info *tsi;
159         struct obd_ioobj *ioo;
160         struct niobuf_remote *rnb;
161         int opc;
162         struct ldlm_prolong_args pa = { 0 };
163
164         ENTRY;
165
166         /* Don't use tgt_ses_info() to get session info, because lock_match()
167          * can be called while request has no processing thread yet. */
168         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
169
170         /*
171          * Use LASSERT below because malformed RPCs should have
172          * been filtered out in tgt_hpreq_handler().
173          */
174         opc = lustre_msg_get_opc(req->rq_reqmsg);
175         LASSERT(opc == OST_READ || opc == OST_WRITE);
176
177         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
178         LASSERT(ioo != NULL);
179
180         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
181         LASSERT(rnb != NULL);
182         LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
183
184         pa.lpa_mode = LCK_PW | LCK_GROUP;
185         if (opc == OST_READ)
186                 pa.lpa_mode |= LCK_PR;
187
188         DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks for " DFID,
189                   tgt_name(tsi->tsi_tgt), current->comm, PFID(&tsi->tsi_fid));
190
191         mdt_prolong_dom_lock(tsi, &pa);
192
193         if (pa.lpa_blocks_cnt > 0) {
194                 CDEBUG(D_DLMTRACE,
195                        "%s: refreshed %u locks timeout for req %p\n",
196                        tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
197                 RETURN(1);
198         }
199
200         RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
201 }
202
203 static void mdt_rw_hpreq_fini(struct ptlrpc_request *req)
204 {
205         mdt_rw_hpreq_check(req);
206 }
207
208 static struct ptlrpc_hpreq_ops mdt_hpreq_rw = {
209         .hpreq_lock_match = mdt_rw_hpreq_lock_match,
210         .hpreq_check = mdt_rw_hpreq_check,
211         .hpreq_fini = mdt_rw_hpreq_fini
212 };
213
214 /**
215  * Assign high priority operations to an IO request.
216  *
217  * Check if the incoming request is a candidate for
218  * high-priority processing. If it is, assign it a high
219  * priority operations table.
220  *
221  * \param[in] tsi       target session environment for this request
222  */
223 void mdt_hp_brw(struct tgt_session_info *tsi)
224 {
225         struct niobuf_remote    *rnb;
226         struct obd_ioobj        *ioo;
227
228         ENTRY;
229
230         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
231         LASSERT(ioo != NULL); /* must exist after request preprocessing */
232         if (ioo->ioo_bufcnt > 0) {
233                 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
234                 LASSERT(rnb != NULL); /* must exist after preprocessing */
235
236                 /* no high priority if server lock is needed */
237                 if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
238                     (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) &
239                      MSG_REPLAY))
240                         return;
241         }
242         tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_rw;
243 }
244
245 static int mdt_punch_hpreq_lock_match(struct ptlrpc_request *req,
246                                       struct ldlm_lock *lock)
247 {
248         struct tgt_session_info *tsi;
249         struct obdo *oa;
250
251         ENTRY;
252
253         /* Don't use tgt_ses_info() to get session info, because lock_match()
254          * can be called while request has no processing thread yet. */
255         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
256
257         /*
258          * Use LASSERT below because malformed RPCs should have
259          * been filtered out in tgt_hpreq_handler().
260          */
261         LASSERT(tsi->tsi_ost_body != NULL);
262         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
263             tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
264                 RETURN(1);
265
266         oa = &tsi->tsi_ost_body->oa;
267
268         LASSERT(lock->l_resource != NULL);
269         if (!fid_res_name_eq(&oa->o_oi.oi_fid, &lock->l_resource->lr_name))
270                 RETURN(0);
271
272         if (!(lock->l_granted_mode & (LCK_PW | LCK_GROUP)))
273                 RETURN(0);
274
275         RETURN(1);
276 }
277
278 /**
279  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
280  *
281  * High-priority queue request check for whether the given punch request
282  * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
283  * covered by an LDLM lock.
284  *
285
286  *
287  * \param[in] req       the incoming request
288  *
289  * \retval              1 if \a req is blocking an LDLM lock cancel
290  * \retval              0 if it is not
291  * \retval              -ESTALE if lock is not found
292  */
293 static int mdt_punch_hpreq_check(struct ptlrpc_request *req)
294 {
295         struct tgt_session_info *tsi;
296         struct obdo *oa;
297         struct ldlm_prolong_args pa = { 0 };
298
299         ENTRY;
300
301         /* Don't use tgt_ses_info() to get session info, because lock_match()
302          * can be called while request has no processing thread yet. */
303         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
304         LASSERT(tsi != NULL);
305         oa = &tsi->tsi_ost_body->oa;
306
307         LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
308                   oa->o_flags & OBD_FL_SRVLOCK));
309
310         pa.lpa_mode = LCK_PW | LCK_GROUP;
311
312         CDEBUG(D_DLMTRACE, "%s: refresh DOM lock for "DFID"\n",
313                tgt_name(tsi->tsi_tgt), PFID(&tsi->tsi_fid));
314
315         mdt_prolong_dom_lock(tsi, &pa);
316
317         if (pa.lpa_blocks_cnt > 0) {
318                 CDEBUG(D_DLMTRACE,
319                        "%s: refreshed %u locks timeout for req %p.\n",
320                        tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
321                 RETURN(1);
322         }
323
324         RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
325 }
326
327 /**
328  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
329  *
330  * Called after the request has been handled. It refreshes lock timeout again
331  * so that client has more time to send lock cancel RPC.
332  *
333  * \param[in] req       request which is being processed.
334  */
335 static void mdt_punch_hpreq_fini(struct ptlrpc_request *req)
336 {
337         mdt_punch_hpreq_check(req);
338 }
339
340 static struct ptlrpc_hpreq_ops mdt_hpreq_punch = {
341         .hpreq_lock_match = mdt_punch_hpreq_lock_match,
342         .hpreq_check = mdt_punch_hpreq_check,
343         .hpreq_fini = mdt_punch_hpreq_fini
344 };
345
346 void mdt_hp_punch(struct tgt_session_info *tsi)
347 {
348         LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
349         /* no high-priority if server lock is needed */
350         if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
351              tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
352             tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
353             lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
354                 return;
355         tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_punch;
356 }
357
358 static int mdt_preprw_read(const struct lu_env *env, struct obd_export *exp,
359                            struct mdt_device *mdt, struct mdt_object *mo,
360                            struct lu_attr *la, int niocount,
361                            struct niobuf_remote *rnb, int *nr_local,
362                            struct niobuf_local *lnb, char *jobid)
363 {
364         struct tgt_session_info *tsi = tgt_ses_info(env);
365         struct ptlrpc_request *req = tgt_ses_req(tsi);
366         struct dt_object *dob;
367         int i, j, rc, tot_bytes = 0;
368         int maxlnb = *nr_local;
369         int level;
370
371         ENTRY;
372
373         mdt_dom_read_lock(mo);
374         *nr_local = 0;
375         /* the only valid case when READ can find object is missing or stale
376          * when export is just evicted and open files are closed forcefully
377          * on server while client's READ can be in progress.
378          * This should not happen on healthy export, object can't be missing
379          * or dying because both states means it was finally destroyed.
380          */
381         level = exp->exp_failed ? D_INFO : D_ERROR;
382         if (!mdt_object_exists(mo)) {
383                 CDEBUG_LIMIT(level,
384                              "%s: READ IO to missing obj "DFID": rc = %d\n",
385                              exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
386                              -ENOENT);
387                 /* return 0 and continue with empty commit to skip such READ
388                  * without more BRW errors.
389                  */
390                 RETURN(0);
391         }
392         if (lu_object_is_dying(&mo->mot_header)) {
393                 CDEBUG_LIMIT(level,
394                              "%s: READ IO to stale obj "DFID": rc = %d\n",
395                              exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
396                              -ESTALE);
397                 /* return 0 and continue with empty commit to skip such READ
398                  * without more BRW errors.
399                  */
400                 RETURN(0);
401         }
402
403         dob = mdt_obj2dt(mo);
404         /* parse remote buffers to local buffers and prepare the latter */
405         for (i = 0, j = 0; i < niocount; i++) {
406                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 0);
407                 if (unlikely(rc < 0))
408                         GOTO(buf_put, rc);
409                 /* correct index for local buffers to continue with */
410                 j += rc;
411                 maxlnb -= rc;
412                 *nr_local += rc;
413                 tot_bytes += rnb[i].rnb_len;
414         }
415
416         rc = dt_attr_get(env, dob, la);
417         if (unlikely(rc))
418                 GOTO(buf_put, rc);
419
420         rc = dt_read_prep(env, dob, lnb, *nr_local);
421         if (unlikely(rc))
422                 GOTO(buf_put, rc);
423
424         mdt_counter_incr(req, LPROC_MDT_IO_READ, tot_bytes);
425         RETURN(0);
426 buf_put:
427         dt_bufs_put(env, dob, lnb, *nr_local);
428         mdt_dom_read_unlock(mo);
429         return rc;
430 }
431
432 static int mdt_preprw_write(const struct lu_env *env, struct obd_export *exp,
433                             struct mdt_device *mdt, struct mdt_object *mo,
434                             struct lu_attr *la, struct obdo *oa,
435                             int objcount, struct obd_ioobj *obj,
436                             struct niobuf_remote *rnb, int *nr_local,
437                             struct niobuf_local *lnb, char *jobid)
438 {
439         struct tgt_session_info *tsi = tgt_ses_info(env);
440         struct ptlrpc_request *req = tgt_ses_req(tsi);
441         struct dt_object *dob;
442         int i, j, k, rc = 0, tot_bytes = 0;
443         int maxlnb = *nr_local;
444
445         ENTRY;
446
447         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
448          * space back if possible */
449         tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
450
451         mdt_dom_read_lock(mo);
452         *nr_local = 0;
453         /* don't report error in cases with failed export */
454         if (!mdt_object_exists(mo)) {
455                 int level = exp->exp_failed ? D_INFO : D_ERROR;
456
457                 rc = -ENOENT;
458                 CDEBUG_LIMIT(level,
459                              "%s: WRITE IO to missing obj "DFID": rc = %d\n",
460                              exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
461                              rc);
462                 /* exit with no data written, note nr_local = 0 above */
463                 GOTO(unlock, rc);
464         }
465         if (lu_object_is_dying(&mo->mot_header)) {
466                 /* This is possible race between object destroy followed by
467                  * discard BL AST and client cache flushing. Object is
468                  * referenced until discard finish.
469                  */
470                 CDEBUG(D_INODE, "WRITE IO to stale object "DFID"\n",
471                        PFID(mdt_object_fid(mo)));
472                 /* Note: continue with no error here to don't cause BRW errors
473                  * but skip transaction in commitrw silently so no data is
474                  * written.
475                  */
476         }
477
478         dob = mdt_obj2dt(mo);
479         /* parse remote buffers to local buffers and prepare the latter */
480         for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
481                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 1);
482                 if (unlikely(rc < 0))
483                         GOTO(err, rc);
484                 /* correct index for local buffers to continue with */
485                 for (k = 0; k < rc; k++) {
486                         lnb[j + k].lnb_flags = rnb[i].rnb_flags;
487                         if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
488                                 lnb[j + k].lnb_rc = -ENOSPC;
489                 }
490                 j += rc;
491                 maxlnb -= rc;
492                 *nr_local += rc;
493                 tot_bytes += rnb[i].rnb_len;
494         }
495
496         rc = dt_write_prep(env, dob, lnb, *nr_local);
497         if (likely(rc))
498                 GOTO(err, rc);
499
500         mdt_counter_incr(req, LPROC_MDT_IO_WRITE, tot_bytes);
501         RETURN(0);
502 err:
503         dt_bufs_put(env, dob, lnb, *nr_local);
504 unlock:
505         mdt_dom_read_unlock(mo);
506         /* tgt_grant_prepare_write() was called, so we must commit */
507         tgt_grant_commit(exp, oa->o_grant_used, rc);
508         /* let's still process incoming grant information packed in the oa,
509          * but without enforcing grant since we won't proceed with the write.
510          * Just like a read request actually. */
511         tgt_grant_prepare_read(env, exp, oa);
512         return rc;
513 }
514
515 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
516                    struct obdo *oa, int objcount, struct obd_ioobj *obj,
517                    struct niobuf_remote *rnb, int *nr_local,
518                    struct niobuf_local *lnb)
519 {
520         struct tgt_session_info *tsi = tgt_ses_info(env);
521         struct mdt_thread_info *info = tsi2mdt_info(tsi);
522         struct lu_attr *la = &info->mti_attr.ma_attr;
523         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
524         struct mdt_object *mo;
525         char *jobid;
526         int rc = 0;
527
528         /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
529          * but for MDT it is different, correct it here. */
530         if (*nr_local > MD_MAX_BRW_PAGES)
531                 *nr_local = MD_MAX_BRW_PAGES;
532
533         jobid = tsi->tsi_jobid;
534
535         if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
536                 CERROR("%s: bad parameters %p/%i/%i\n",
537                        exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
538                 rc = -EPROTO;
539         }
540
541         mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
542         if (IS_ERR(mo))
543                 GOTO(out, rc = PTR_ERR(mo));
544
545         LASSERT(info->mti_object == NULL);
546         info->mti_object = mo;
547
548         if (cmd == OBD_BRW_WRITE) {
549                 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
550                 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
551                                       objcount, obj, rnb, nr_local, lnb,
552                                       jobid);
553         } else if (cmd == OBD_BRW_READ) {
554                 tgt_grant_prepare_read(env, exp, oa);
555                 rc = mdt_preprw_read(env, exp, mdt, mo, la,
556                                      obj->ioo_bufcnt, rnb, nr_local, lnb,
557                                      jobid);
558                 obdo_from_la(oa, la, LA_ATIME);
559         } else {
560                 CERROR("%s: wrong cmd %d received!\n",
561                        exp->exp_obd->obd_name, cmd);
562                 rc = -EPROTO;
563         }
564         if (rc) {
565                 lu_object_put(env, &mo->mot_obj);
566                 info->mti_object = NULL;
567         }
568 out:
569         RETURN(rc);
570 }
571
572 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
573                              struct mdt_object *mo, int objcount, int niocount,
574                              struct niobuf_local *lnb)
575 {
576         struct dt_object *dob;
577         int rc = 0;
578
579         ENTRY;
580
581         dob = mdt_obj2dt(mo);
582
583         if (niocount)
584                 dt_bufs_put(env, dob, lnb, niocount);
585
586         mdt_dom_read_unlock(mo);
587         RETURN(rc);
588 }
589
590 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
591                               struct mdt_device *mdt, struct mdt_object *mo,
592                               struct lu_attr *la, struct obdo *oa, int objcount,
593                               int niocount, struct niobuf_local *lnb,
594                               unsigned long granted, int old_rc)
595 {
596         struct dt_device *dt = mdt->mdt_bottom;
597         struct dt_object *dob;
598         struct thandle *th;
599         int rc = 0;
600         int retries = 0;
601         int i, restart = 0;
602
603         ENTRY;
604
605         dob = mdt_obj2dt(mo);
606
607         if (old_rc)
608                 GOTO(out, rc = old_rc);
609
610         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
611 retry:
612         if (!dt_object_exists(dob))
613                 GOTO(out, rc = -ENOENT);
614         if (lu_object_is_dying(&mo->mot_header)) {
615                 /* Commit to stale object can be just skipped silently. */
616                 CDEBUG(D_INODE, "skip commit to stale object "DFID"\n",
617                         PFID(mdt_object_fid(mo)));
618                 GOTO(out, rc = 0);
619         }
620
621         if (niocount == 0) {
622                 rc = -EPROTO;
623                 DEBUG_REQ(D_WARNING, tgt_ses_req(tgt_ses_info(env)),
624                           "%s: commit with no pages for "DFID": rc = %d\n",
625                           exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)), rc);
626                 GOTO(out, rc);
627         }
628
629         th = dt_trans_create(env, dt);
630         if (IS_ERR(th))
631                 GOTO(out, rc = PTR_ERR(th));
632
633         for (i = 0; i < niocount; i++) {
634                 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
635                         th->th_sync = 1;
636                         break;
637                 }
638         }
639
640         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
641                 GOTO(out_stop, rc = -EINPROGRESS);
642
643         rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
644         if (rc)
645                 GOTO(out_stop, rc);
646
647         if (la->la_valid) {
648                 /* update [mac]time if needed */
649                 rc = dt_declare_attr_set(env, dob, la, th);
650                 if (rc)
651                         GOTO(out_stop, rc);
652         }
653
654         tgt_vbr_obj_set(env, dob);
655         rc = dt_trans_start(env, dt, th);
656         if (rc)
657                 GOTO(out_stop, rc);
658
659         dt_write_lock(env, dob, 0);
660         rc = dt_write_commit(env, dob, lnb, niocount, th, oa->o_size);
661         if (rc) {
662                 restart = th->th_restart_tran;
663                 GOTO(unlock, rc);
664         }
665
666         if (la->la_valid) {
667                 rc = dt_attr_set(env, dob, la, th);
668                 if (rc)
669                         GOTO(unlock, rc);
670         }
671         /* get attr to return */
672         rc = dt_attr_get(env, dob, la);
673 unlock:
674         dt_write_unlock(env, dob);
675
676 out_stop:
677         /* Force commit to make the just-deleted blocks
678          * reusable. LU-456 */
679         if (rc == -ENOSPC)
680                 th->th_sync = 1;
681
682
683         if (rc == 0 && granted > 0) {
684                 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
685                         granted = 0;
686         }
687
688         th->th_result = restart ? 0 : rc;
689         dt_trans_stop(env, dt, th);
690         if (rc == -ENOSPC && retries++ < 3) {
691                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
692                        retries);
693                 goto retry;
694         }
695         if (restart) {
696                 retries++;
697                 restart = 0;
698                 if (retries % 10000 == 0)
699                         CERROR("%s: restart IO write too many times: %d\n",
700                                exp->exp_obd->obd_name, retries);
701                 CDEBUG(D_INODE, "retry transaction, retries:%d\n",
702                        retries);
703                 goto retry;
704         }
705
706 out:
707         dt_bufs_put(env, dob, lnb, niocount);
708         mdt_dom_read_unlock(mo);
709         if (granted > 0)
710                 tgt_grant_commit(exp, granted, old_rc);
711         RETURN(rc);
712 }
713
714 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
715                             bool increase_only)
716 {
717         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
718         struct ldlm_res_id resid;
719         struct ldlm_resource *res;
720
721         fid_build_reg_res_name(mdt_object_fid(mo), &resid);
722         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
723                                 LDLM_IBITS, 1);
724         if (IS_ERR(res))
725                 return;
726
727         /* Update lvbo data if exists. */
728         if (mdt_dom_lvb_is_valid(res))
729                 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
730         ldlm_resource_putref(res);
731 }
732
733 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
734                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
735                      struct niobuf_remote *rnb, int npages,
736                      struct niobuf_local *lnb, int old_rc)
737 {
738         struct mdt_thread_info *info = mdt_th_info(env);
739         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
740         struct mdt_object *mo = info->mti_object;
741         struct lu_attr *la = &info->mti_attr.ma_attr;
742         __u64 valid;
743         int rc = 0;
744
745         LASSERT(mo);
746
747         if (cmd == OBD_BRW_WRITE) {
748                 /* Don't update timestamps if this write is older than a
749                  * setattr which modifies the timestamps. b=10150 */
750
751                 /* XXX when we start having persistent reservations this needs
752                  * to be changed to ofd_fmd_get() to create the fmd if it
753                  * doesn't already exist so we can store the reservation handle
754                  * there. */
755                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
756                 if (tgt_fmd_check(exp, mdt_object_fid(mo),
757                                   mdt_info_req(info)->rq_xid))
758                         valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
759                                  OBD_MD_FLCTIME;
760
761                 la_from_obdo(la, oa, valid);
762
763                 rc = mdt_commitrw_write(env, exp, mdt, mo, la, oa, objcount,
764                                         npages, lnb, oa->o_grant_used, old_rc);
765                 if (rc == 0)
766                         obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
767                 else
768                         obdo_from_la(oa, la, LA_GID | LA_UID);
769
770                 mdt_dom_obj_lvb_update(env, mo, false);
771                 /* don't report overquota flag if we failed before reaching
772                  * commit */
773                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
774                         /* return the overquota flags to client */
775                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
776                                 if (oa->o_valid & OBD_MD_FLFLAGS)
777                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
778                                 else
779                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
780                         }
781
782                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
783                                 if (oa->o_valid & OBD_MD_FLFLAGS)
784                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
785                                 else
786                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
787                         }
788
789                         if (lnb[0].lnb_flags & OBD_BRW_OVER_PRJQUOTA) {
790                                 if (oa->o_valid & OBD_MD_FLFLAGS)
791                                         oa->o_flags |= OBD_FL_NO_PRJQUOTA;
792                                 else
793                                         oa->o_flags = OBD_FL_NO_PRJQUOTA;
794                         }
795
796                         oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
797                                        OBD_MD_FLGRPQUOTA | OBD_MD_FLPRJQUOTA;
798                 }
799         } else if (cmd == OBD_BRW_READ) {
800                 /* If oa != NULL then mdt_preprw_read updated the inode
801                  * atime and we should update the lvb so that other glimpses
802                  * will also get the updated value. bug 5972 */
803                 if (oa)
804                         mdt_dom_obj_lvb_update(env, mo, true);
805                 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
806                 if (old_rc)
807                         rc = old_rc;
808         } else {
809                 rc = -EPROTO;
810         }
811         mdt_thread_info_fini(info);
812         RETURN(rc);
813 }
814
815 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
816                      struct dt_object *dob, __u64 start, __u64 end,
817                      struct lu_attr *la)
818 {
819         struct thandle *th;
820         int rc;
821
822         ENTRY;
823
824         /* we support truncate, not punch yet */
825         LASSERT(end == OBD_OBJECT_EOF);
826
827         if (!dt_object_exists(dob))
828                 RETURN(-ENOENT);
829
830         th = dt_trans_create(env, dt);
831         if (IS_ERR(th))
832                 RETURN(PTR_ERR(th));
833
834         rc = dt_declare_attr_set(env, dob, la, th);
835         if (rc)
836                 GOTO(stop, rc);
837
838         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
839         if (rc)
840                 GOTO(stop, rc);
841
842         tgt_vbr_obj_set(env, dob);
843         rc = dt_trans_start(env, dt, th);
844         if (rc)
845                 GOTO(stop, rc);
846
847         dt_write_lock(env, dob, 0);
848         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
849         if (rc)
850                 GOTO(unlock, rc);
851         rc = dt_attr_set(env, dob, la, th);
852         if (rc)
853                 GOTO(unlock, rc);
854 unlock:
855         dt_write_unlock(env, dob);
856 stop:
857         th->th_result = rc;
858         dt_trans_stop(env, dt, th);
859         RETURN(rc);
860 }
861
862 int mdt_punch_hdl(struct tgt_session_info *tsi)
863 {
864         const struct obdo *oa = &tsi->tsi_ost_body->oa;
865         struct ptlrpc_request *req = tgt_ses_req(tsi);
866         struct ost_body *repbody;
867         struct mdt_thread_info *info;
868         struct lu_attr *la;
869         struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
870         struct obd_export *exp = tsi->tsi_exp;
871         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
872         struct mdt_object *mo;
873         struct dt_object *dob;
874         __u64 flags = 0;
875         struct lustre_handle lh = { 0, };
876         ktime_t kstart = ktime_get();
877         __u64 start, end;
878         int rc;
879         bool srvlock;
880
881         ENTRY;
882
883         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
884         BUILD_BUG_ON(!(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK));
885
886         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
887             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
888                 RETURN(err_serious(-EPROTO));
889
890         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
891         if (repbody == NULL)
892                 RETURN(err_serious(-ENOMEM));
893
894         /* punch start,end are passed in o_size,o_blocks throught wire */
895         start = oa->o_size;
896         end = oa->o_blocks;
897
898         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
899                 RETURN(-EPROTO);
900
901         info = tsi2mdt_info(tsi);
902         la = &info->mti_attr.ma_attr;
903         /* standard truncate optimization: if file body is completely
904          * destroyed, don't send data back to the server. */
905         if (start == 0)
906                 flags |= LDLM_FL_AST_DISCARD_DATA;
907
908         repbody->oa.o_oi = oa->o_oi;
909         repbody->oa.o_valid = OBD_MD_FLID;
910
911         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
912                   oa->o_valid & OBD_MD_FLFLAGS &&
913                   oa->o_flags & OBD_FL_SRVLOCK;
914
915         if (srvlock) {
916                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
917                                        &flags);
918                 if (rc != 0)
919                         GOTO(out, rc);
920         }
921
922         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
923                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
924                oa->o_valid, start, end);
925
926         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
927         if (IS_ERR(mo))
928                 GOTO(out_unlock, rc = PTR_ERR(mo));
929
930         if (!mdt_object_exists(mo))
931                 GOTO(out_put, rc = -ENOENT);
932
933         /* Shouldn't happen on dirs */
934         if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
935                 rc = -EPERM;
936                 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
937                        exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
938                 GOTO(out_put, rc);
939         }
940
941         mdt_dom_write_lock(mo);
942         dob = mdt_obj2dt(mo);
943
944         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
945         la->la_size = start;
946         la->la_valid |= LA_SIZE;
947
948         /* MDT supports FMD for Data-on-MDT needs */
949         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
950                 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
951                                tgt_ses_req(tsi)->rq_xid);
952
953         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
954                               start, end, la);
955         mdt_dom_write_unlock(mo);
956         if (rc)
957                 GOTO(out_put, rc);
958
959         mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
960         mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
961                          ktime_us_delta(ktime_get(), kstart));
962         EXIT;
963 out_put:
964         lu_object_put(tsi->tsi_env, &mo->mot_obj);
965 out_unlock:
966         if (srvlock)
967                 tgt_data_unlock(&lh, LCK_PW);
968 out:
969         mdt_thread_info_fini(info);
970         return rc;
971 }
972
973 /**
974  * MDT glimpse for Data-on-MDT
975  *
976  * If there is write lock on client then function issues glimpse_ast to get
977  * an actual size from that client.
978  *
979  */
980 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
981                    struct ldlm_resource *res)
982 {
983         union ldlm_policy_data policy;
984         struct lustre_handle lockh;
985         enum ldlm_mode mode;
986         struct ldlm_lock *lock;
987         struct ldlm_glimpse_work *gl_work;
988         LIST_HEAD(gl_list);
989         int rc;
990
991         ENTRY;
992
993         /* There can be only one write lock covering data, try to match it. */
994         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
995         mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
996                                &res->lr_name, LDLM_IBITS, &policy,
997                                LCK_PW, &lockh);
998
999         /* There is no PW lock on this object; finished. */
1000         if (mode == 0)
1001                 RETURN(0);
1002
1003         lock = ldlm_handle2lock(&lockh);
1004         if (lock == NULL)
1005                 RETURN(0);
1006
1007         /*
1008          * This check is for lock taken in mdt_reint_unlink() that does
1009          * not have l_glimpse_ast set. So the logic is: if there is a lock
1010          * with no l_glimpse_ast set, this object is being destroyed already.
1011          * Hence, if you are grabbing DLM locks on the server, always set
1012          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
1013          */
1014         if (lock->l_glimpse_ast == NULL) {
1015                 LDLM_DEBUG(lock, "no l_glimpse_ast");
1016                 GOTO(out, rc = -ENOENT);
1017         }
1018
1019         OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1020         if (!gl_work)
1021                 GOTO(out, rc = -ENOMEM);
1022
1023         /* Populate the gl_work structure.
1024          * Grab additional reference on the lock which will be released in
1025          * ldlm_work_gl_ast_lock() */
1026         gl_work->gl_lock = LDLM_LOCK_GET(lock);
1027         /* The glimpse callback is sent to one single IO lock. As a result,
1028          * the gl_work list is just composed of one element */
1029         list_add_tail(&gl_work->gl_list, &gl_list);
1030         /* There is actually no need for a glimpse descriptor when glimpsing
1031          * IO locks */
1032         gl_work->gl_desc = NULL;
1033         /* the ldlm_glimpse_work structure is allocated on the stack */
1034         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1035
1036         ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1037
1038         /* If the list is not empty, we failed to glimpse a lock and
1039          * must clean it up. Usually due to a race with unlink.*/
1040         if (!list_empty(&gl_list)) {
1041                 LDLM_LOCK_RELEASE(lock);
1042                 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1043         }
1044         rc = 0;
1045         EXIT;
1046 out:
1047         LDLM_LOCK_PUT(lock);
1048         return rc;
1049 }
1050
1051 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1052                           struct ost_lvb *lvb)
1053 {
1054         struct ost_lvb *res_lvb;
1055
1056         lock_res(res);
1057         res_lvb = res->lr_lvb_data;
1058         if (lvb)
1059                 *lvb = *res_lvb;
1060
1061         if (mb) {
1062                 mb->mbo_dom_size = res_lvb->lvb_size;
1063                 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1064                 mb->mbo_mtime = res_lvb->lvb_mtime;
1065                 mb->mbo_ctime = res_lvb->lvb_ctime;
1066                 mb->mbo_atime = res_lvb->lvb_atime;
1067                 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1068                                  OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1069         }
1070         CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1071         unlock_res(res);
1072 }
1073
1074 /**
1075  * MDT glimpse for Data-on-MDT
1076  *
1077  * This function is called when MDT get attributes for the DoM object.
1078  * If there is write lock on client then function issues glimpse_ast to get
1079  * an actual size from that client.
1080  */
1081 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1082                         const struct lu_fid *fid, struct mdt_body *mb,
1083                         bool dom_lock)
1084 {
1085         struct ldlm_res_id resid;
1086         struct ldlm_resource *res;
1087         int rc = 0;
1088
1089         ENTRY;
1090
1091         fid_build_reg_res_name(fid, &resid);
1092         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1093                                 LDLM_IBITS, 1);
1094         if (IS_ERR(res))
1095                 RETURN(-ENOENT);
1096
1097         /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1098         if (dom_lock || !mdt_dom_lvb_is_valid(res))
1099                 mdt_dom_lvbo_update(res, NULL, NULL, false);
1100
1101         mdt_lvb2reply(res, mb, NULL);
1102         ldlm_resource_putref(res);
1103         RETURN(rc);
1104 }
1105
1106 /**
1107  * MDT DoM lock intent policy (glimpse)
1108  *
1109  * Intent policy is called when lock has an intent, for DoM file that
1110  * means glimpse lock and policy fills Lock Value Block (LVB).
1111  *
1112  * If already granted lock is found it will be placed in \a lockp and
1113  * returned back to caller function.
1114  *
1115  * \param[in] tsi        session info
1116  * \param[in,out] lockp  pointer to the lock
1117  * \param[in] flags      LDLM flags
1118  *
1119  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
1120  *                      and placed in \a lockp
1121  * \retval              ELDLM_LOCK_ABORTED in other cases except error
1122  * \retval              negative value on error
1123  */
1124 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1125                         struct ldlm_lock **lockp, __u64 flags)
1126 {
1127         struct ldlm_lock *lock = *lockp;
1128         struct ldlm_resource *res = lock->l_resource;
1129         ldlm_processing_policy policy;
1130         struct ldlm_reply *rep;
1131         struct mdt_body *mbo;
1132         struct ost_lvb *lvb;
1133         bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1134         int rc;
1135
1136         ENTRY;
1137
1138         policy = ldlm_get_processing_policy(res);
1139         LASSERT(policy != NULL);
1140
1141         if (unlikely(old_client)) {
1142                 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1143                 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1144         } else {
1145                 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1146                                      sizeof(*lvb));
1147         }
1148         rc = req_capsule_server_pack(mti->mti_pill);
1149         if (rc)
1150                 RETURN(err_serious(rc));
1151
1152         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1153
1154         if (unlikely(old_client)) {
1155                 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1156                 LASSERT(mbo);
1157                 lvb = NULL;
1158         } else {
1159                 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1160                 LASSERT(lvb);
1161                 mbo = NULL;
1162         }
1163
1164         lock_res(res);
1165         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1166          * lock was found by ldlm_handle_enqueue(); if so no need to grant
1167          * it again. */
1168         if (flags & LDLM_FL_RESENT) {
1169                 rc = LDLM_ITER_CONTINUE;
1170         } else {
1171                 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1172                 enum ldlm_error err;
1173
1174                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1175                 check_res_locked(res);
1176         }
1177         unlock_res(res);
1178
1179         /* The lock met with no resistance; we're finished. */
1180         if (rc == LDLM_ITER_CONTINUE) {
1181                 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1182         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1183                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1184                  * callback for glimpse size. The real size user will trigger
1185                  * the glimpse callback when necessary. */
1186                 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1187         }
1188
1189         rc = mdt_do_glimpse(mti->mti_env, ns, res);
1190         if (rc == -ENOENT) {
1191                 /* We are racing with unlink(); just return -ENOENT */
1192                 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1193         } else if (rc == -EINVAL) {
1194                 /* this is possible is client lock has been cancelled but
1195                  * still exists on server. If that lock was found on server
1196                  * as only conflicting lock then the client has already
1197                  * size authority and glimpse is not needed. */
1198                 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1199         } else if (rc < 0) {
1200                 RETURN(rc);
1201         }
1202         rc = ELDLM_LOCK_ABORTED;
1203 fill_mbo:
1204         /* LVB can be without valid data in case of DOM */
1205         if (!mdt_dom_lvb_is_valid(res))
1206                 mdt_dom_lvbo_update(res, lock, NULL, false);
1207         mdt_lvb2reply(res, mbo, lvb);
1208
1209         RETURN(rc);
1210 }
1211
1212 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1213                     struct ldlm_lock **lockp, __u64 flags)
1214 {
1215         struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1216         struct lu_fid *fid = &tsi->tsi_fid;
1217         struct ldlm_lock *lock = *lockp;
1218         struct ldlm_resource *res = lock->l_resource;
1219         struct ldlm_reply *rep;
1220         struct mdt_body *mbo;
1221         struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1222         struct mdt_object *mo;
1223         int rc = 0;
1224
1225         ENTRY;
1226
1227         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1228         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1229         rc = req_capsule_server_pack(mti->mti_pill);
1230         if (rc)
1231                 RETURN(err_serious(rc));
1232
1233         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1234         if (rep == NULL)
1235                 RETURN(-EPROTO);
1236
1237         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1238         if (mbo == NULL)
1239                 RETURN(-EPROTO);
1240
1241         fid_extract_from_res_name(fid, &res->lr_name);
1242         mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1243         if (unlikely(IS_ERR(mo)))
1244                 RETURN(PTR_ERR(mo));
1245
1246         if (!mdt_object_exists(mo))
1247                 GOTO(out, rc = -ENOENT);
1248
1249         if (mdt_object_remote(mo))
1250                 GOTO(out, rc = -EPROTO);
1251
1252         /* Get lock from request for possible resent case. */
1253         mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1254         /* resent case */
1255         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1256                 mdt_lock_handle_init(lhc);
1257                 mdt_lh_reg_init(lhc, *lockp);
1258
1259                 /* This will block MDT thread but it should be fine until
1260                  * client caches small amount of data for DoM, which should be
1261                  * smaller than one BRW RPC and should be able to be
1262                  * piggybacked by lock cancel RPC.
1263                  * If the client could hold the lock too long, this code can be
1264                  * revised to call mdt_object_lock_try(). And if fails, it will
1265                  * return ELDLM_OK here and fall back into normal lock enqueue
1266                  * process.
1267                  */
1268                 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1269                 if (rc)
1270                         GOTO(out, rc);
1271         }
1272
1273         if (!mdt_dom_lvb_is_valid(res)) {
1274                 rc = mdt_dom_lvb_alloc(res);
1275                 if (rc)
1276                         GOTO(out_fail, rc);
1277                 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1278         }
1279         mdt_lvb2reply(res, mbo, NULL);
1280 out_fail:
1281         rep->lock_policy_res2 = clear_serious(rc);
1282         if (rep->lock_policy_res2) {
1283                 lhc->mlh_reg_lh.cookie = 0ull;
1284                 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1285         }
1286
1287         rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1288 out:
1289         if (rc < 0)
1290                 lhc->mlh_reg_lh.cookie = 0ull;
1291         mdt_object_put(mti->mti_env, mo);
1292         RETURN(rc);
1293 }
1294
1295 /* check if client has already DoM lock for given resource */
1296 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1297                              const struct lu_fid *fid)
1298 {
1299         struct mdt_device *mdt = info->mti_mdt;
1300         union ldlm_policy_data *policy = &info->mti_policy;
1301         struct ldlm_res_id *res_id = &info->mti_res_id;
1302         __u64 open_flags = info->mti_spec.sp_cr_flags;
1303         struct lustre_handle lockh;
1304         enum ldlm_mode mode;
1305         struct ldlm_lock *lock;
1306         enum ldlm_mode lm;
1307         bool rc;
1308
1309         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1310         fid_build_reg_res_name(fid, res_id);
1311
1312
1313         lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1314         mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1315                                LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1316                                lm, &lockh);
1317
1318         /* There is no other PW lock on this object; finished. */
1319         if (mode == 0)
1320                 return false;
1321
1322         lock = ldlm_handle2lock(&lockh);
1323         if (lock == 0)
1324                 return false;
1325
1326         /* check if lock from the same client */
1327         rc = (lock->l_export->exp_handle.h_cookie ==
1328               info->mti_exp->exp_handle.h_cookie);
1329         LDLM_LOCK_PUT(lock);
1330         return rc;
1331 }
1332
1333 /**
1334  * MDT request handler for OST_GETATTR RPC.
1335  *
1336  * This is data-specific request to get object and layout versions under
1337  * IO lock. It is reliable only for Data-on-MDT files.
1338  *
1339  * \param[in] tsi target session environment for this request
1340  *
1341  * \retval 0 if successful
1342  * \retval negative value on error
1343  */
1344 int mdt_data_version_get(struct tgt_session_info *tsi)
1345 {
1346         struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1347         struct mdt_device *mdt = mti->mti_mdt;
1348         struct mdt_body *repbody;
1349         struct mdt_object *mo = mti->mti_object;
1350         struct lov_comp_md_v1 *comp;
1351         struct lustre_handle lh = { 0 };
1352         __u64 flags = 0;
1353         __s64 version;
1354         enum ldlm_mode lock_mode = LCK_PR;
1355         bool srvlock;
1356         int rc;
1357
1358         ENTRY;
1359
1360         req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1361         req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1362         rc = req_capsule_server_pack(tsi->tsi_pill);
1363         if (unlikely(rc != 0))
1364                 RETURN(err_serious(rc));
1365
1366         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1367         if (repbody == NULL)
1368                 RETURN(-ENOMEM);
1369
1370         srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1371                   tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1372
1373         if (srvlock) {
1374                 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1375                         lock_mode = LCK_PW;
1376
1377                 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1378                 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1379                                        &lh, lock_mode, &flags);
1380                 if (rc != 0)
1381                         RETURN(rc);
1382         }
1383
1384         if (!mdt_object_exists(mo))
1385                 GOTO(out, rc = -ENOENT);
1386         if (mdt_object_remote(mo))
1387                 GOTO(out, rc = -EREMOTE);
1388         if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1389                 GOTO(out, rc = -EBADF);
1390
1391         /* Get version first */
1392         version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1393         if (version && version != -EOPNOTSUPP) {
1394                 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1395                 /* re-use mbo_ioepoch to transfer version */
1396                 repbody->mbo_version = version;
1397         }
1398
1399         /* Read layout to get its version */
1400         rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1401         if (rc == -ENODATA) /* File has no layout yet */
1402                 GOTO(out, rc = 0);
1403         else if (rc < 0)
1404                 GOTO(out, rc);
1405
1406         comp = mti->mti_buf.lb_buf;
1407         if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1408                 CDEBUG(D_INFO, DFID" has no composite layout",
1409                        PFID(&tsi->tsi_fid));
1410                 GOTO(out, rc = -ESTALE);
1411         }
1412
1413         CDEBUG(D_INODE, DFID": layout version: %u\n",
1414                PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1415
1416         repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1417         /* re-use mbo_rdev for that */
1418         repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1419         rc = 0;
1420 out:
1421         if (srvlock)
1422                 tgt_data_unlock(&lh, lock_mode);
1423
1424         repbody->mbo_valid |= OBD_MD_FLFLAGS;
1425         repbody->mbo_flags = OBD_FL_FLUSH;
1426         RETURN(rc);
1427 }
1428
1429 /* read file data to the buffer */
1430 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1431                          struct lustre_handle *lh)
1432 {
1433         const struct lu_env *env = mti->mti_env;
1434         struct tgt_session_info *tsi = tgt_ses_info(env);
1435         struct req_capsule *pill = tsi->tsi_pill;
1436         const struct lu_fid *fid;
1437         struct ptlrpc_request *req = tgt_ses_req(tsi);
1438         struct mdt_body *mbo;
1439         struct dt_device *dt = mdt->mdt_bottom;
1440         struct dt_object *mo;
1441         void *buf;
1442         struct niobuf_remote *rnb = NULL;
1443         struct niobuf_local *lnb;
1444         int rc;
1445         loff_t offset;
1446         unsigned int len, copied = 0;
1447         __u64 real_dom_size;
1448         int lnbs, nr_local, i;
1449         bool dom_lock = false;
1450
1451         ENTRY;
1452
1453         if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1454                 /* There is no reply buffers for this field, this means that
1455                  * client has no support for data in reply.
1456                  */
1457                 RETURN(0);
1458         }
1459
1460         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1461         if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1462                 RETURN(0);
1463
1464         if (!mbo->mbo_dom_size)
1465                 RETURN(0);
1466
1467         if (lustre_handle_is_used(lh)) {
1468                 struct ldlm_lock *lock;
1469
1470                 lock = ldlm_handle2lock(lh);
1471                 if (lock) {
1472                         dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1473                         LDLM_LOCK_PUT(lock);
1474                 }
1475         }
1476
1477         /* return data along with open only along with DoM lock */
1478         if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1479                 RETURN(0);
1480
1481         /* if DoM object holds encrypted content, we need to make sure we
1482          * send whole encryption units, or client will read corrupted content
1483          */
1484         if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1485             mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1486                 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1487                                 LUSTRE_ENCRYPTION_UNIT_SIZE;
1488         else
1489                 real_dom_size = mbo->mbo_dom_size;
1490
1491         CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1492                real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1493         len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1494
1495         /* NB: at this moment we have the following sizes:
1496          * - req->rq_replen: used data in reply
1497          * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1498          *
1499          * Ideal case when file size fits in allocated reply buffer,
1500          * that mean we can return whole data in reply. We can also fit more
1501          * data up to max_reply_size in total reply size, but this will cause
1502          * re-allocation on client and resend with larger buffer. This is still
1503          * faster than separate READ IO.
1504          * Third case if file is too big to fit even in maximum size, in that
1505          * case we return just tail to optimize possible append.
1506          *
1507          * At the moment the following strategy is used:
1508          * 1) try to fit into the buffer we have
1509          * 2) return just file tail otherwise.
1510          */
1511         if (real_dom_size <= len) {
1512                 /* can fit whole data */
1513                 len = real_dom_size;
1514                 offset = 0;
1515         } else if (real_dom_size <
1516                    mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1517                 int tail, pgbits;
1518
1519                 /* File tail offset must be aligned with larger page size
1520                  * between client and server, so the maximum page size is
1521                  * used here to align offset.
1522                  *
1523                  * NB: DOM feature was introduced when server supports pagebits
1524                  * already, so it should be always non-zero value. Report error
1525                  * if it is not for some reason.
1526                  */
1527                 if (!req->rq_export->exp_target_data.ted_pagebits) {
1528                         CERROR("%s: client page bits are not saved on server\n",
1529                                mdt_obd_name(mdt));
1530                         RETURN(0);
1531                 }
1532                 pgbits = max_t(int, PAGE_SHIFT,
1533                                req->rq_export->exp_target_data.ted_pagebits);
1534                 tail = real_dom_size % (1 << pgbits);
1535
1536                 /* no partial tail or tail can't fit in reply */
1537                 if (tail == 0 || len < tail)
1538                         RETURN(0);
1539
1540                 len = tail;
1541                 offset = real_dom_size - len;
1542         } else {
1543                 /* DOM stripe is fully written, so don't expect its tail
1544                  * will be used by append.
1545                  */
1546                 RETURN(0);
1547         }
1548
1549         LASSERT((offset & ~PAGE_MASK) == 0);
1550         rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1551                                      sizeof(*rnb) + len);
1552         if (rc != 0) {
1553                 /* failed to grow data buffer, just exit */
1554                 GOTO(out, rc = -E2BIG);
1555         }
1556
1557         /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1558         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1559         fid = &mbo->mbo_fid1;
1560         if (!fid_is_sane(fid))
1561                 GOTO(out, rc = -EINVAL);
1562
1563         rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1564         if (rnb == NULL)
1565                 GOTO(out, rc = -EPROTO);
1566
1567         buf = (char *)rnb + sizeof(*rnb);
1568         rnb->rnb_len = len;
1569         rnb->rnb_offset = offset;
1570
1571         mo = dt_locate(env, dt, fid);
1572         if (IS_ERR(mo))
1573                 GOTO(out_rnb, rc = PTR_ERR(mo));
1574         LASSERT(mo != NULL);
1575
1576         dt_read_lock(env, mo, 0);
1577         if (!dt_object_exists(mo))
1578                 GOTO(unlock, rc = -ENOENT);
1579
1580         /* parse remote buffers to local buffers and prepare the latter */
1581         lnbs = (len >> PAGE_SHIFT) + 1;
1582         OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1583         if (lnb == NULL)
1584                 GOTO(unlock, rc = -ENOMEM);
1585
1586         rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1587         if (unlikely(rc < 0))
1588                 GOTO(free, rc);
1589         LASSERT(rc <= lnbs);
1590         nr_local = rc;
1591         rc = dt_read_prep(env, mo, lnb, nr_local);
1592         if (unlikely(rc))
1593                 GOTO(buf_put, rc);
1594         /* copy data to the buffer finally */
1595         for (i = 0; i < nr_local; i++) {
1596                 char *p = kmap(lnb[i].lnb_page);
1597                 long off;
1598
1599                 LASSERT(lnb[i].lnb_page_offset == 0);
1600                 off = lnb[i].lnb_len & ~PAGE_MASK;
1601                 if (off > 0)
1602                         memset(p + off, 0, PAGE_SIZE - off);
1603
1604                 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1605                 kunmap(lnb[i].lnb_page);
1606                 copied += lnb[i].lnb_len;
1607                 LASSERT(rc <= len);
1608         }
1609         CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1610                len, offset);
1611         if (copied < len) {
1612                 CWARN("%s: read %i bytes for "DFID
1613                       " but wanted %u, is size wrong?\n",
1614                       tsi->tsi_exp->exp_obd->obd_name, copied,
1615                       PFID(&tsi->tsi_fid), len);
1616                 /* Ignore partially copied data */
1617                 copied = 0;
1618         }
1619         EXIT;
1620 buf_put:
1621         dt_bufs_put(env, mo, lnb, nr_local);
1622 free:
1623         OBD_FREE_PTR_ARRAY(lnb, lnbs);
1624 unlock:
1625         dt_read_unlock(env, mo);
1626         lu_object_put(env, &mo->do_lu);
1627 out_rnb:
1628         rnb->rnb_len = copied;
1629 out:
1630         /* Don't fail OPEN request if read-on-open is failed, but drop
1631          * a message in log about the error.
1632          */
1633         if (rc)
1634                 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1635
1636         RETURN(0);
1637 }
1638
1639 /**
1640  * Completion AST for DOM discard locks:
1641  *
1642  * CP AST an DOM discard lock is called always right after enqueue or from
1643  * reprocess if lock was blocked, in the latest case l_ast_data is set to
1644  * the mdt_object which is kept while there are pending locks on it.
1645  */
1646 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1647 {
1648         struct mdt_object *mo;
1649         struct lustre_handle dom_lh;
1650         struct lu_env *env;
1651
1652         ENTRY;
1653
1654         /* l_ast_data is set when lock was not granted immediately
1655          * in mdt_dom_discard_data() below but put into waiting list,
1656          * so this CP callback means we are finished and corresponding
1657          * MDT object should be released finally as well as lock itself.
1658          */
1659         lock_res_and_lock(lock);
1660         if (!lock->l_ast_data) {
1661                 unlock_res_and_lock(lock);
1662                 RETURN(0);
1663         }
1664
1665         mo = lock->l_ast_data;
1666         lock->l_ast_data = NULL;
1667         unlock_res_and_lock(lock);
1668
1669         ldlm_lock2handle(lock, &dom_lh);
1670         ldlm_lock_decref(&dom_lh, LCK_PW);
1671
1672         env = lu_env_find();
1673         LASSERT(env);
1674         mdt_object_put(env, mo);
1675
1676         RETURN(0);
1677 }
1678
1679 void mdt_dom_discard_data(struct mdt_thread_info *info,
1680                           struct mdt_object *mo)
1681 {
1682         struct ptlrpc_request *req = mdt_info_req(info);
1683         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1684         union ldlm_policy_data policy;
1685         struct ldlm_res_id res_id;
1686         struct lustre_handle dom_lh;
1687         struct ldlm_lock *lock;
1688         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1689         int rc = 0;
1690         bool old_client;
1691
1692         ENTRY;
1693
1694         if (req && req_is_replay(req))
1695                 RETURN_EXIT;
1696
1697         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1698         policy.l_inodebits.try_bits = 0;
1699         fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1700
1701         /* Keep blocking version of discard for an old client to avoid
1702          * crashes on non-patched clients. LU-11359.
1703          */
1704         old_client = req && !(exp_connect_flags2(req->rq_export) &
1705                               OBD_CONNECT2_ASYNC_DISCARD);
1706
1707         /* Tell the clients that the object is gone now and that they should
1708          * throw away any cached pages. */
1709         rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1710                                     LDLM_IBITS, &policy, LCK_PW, &flags,
1711                                     ldlm_blocking_ast, old_client ?
1712                                     ldlm_completion_ast :
1713                                     ldlm_dom_discard_cp_ast,
1714                                     NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1715         if (rc != ELDLM_OK) {
1716                 CDEBUG(D_DLMTRACE,
1717                        "Failed to issue discard lock, rc = %d\n", rc);
1718                 RETURN_EXIT;
1719         }
1720
1721         lock = ldlm_handle2lock(&dom_lh);
1722         lock_res_and_lock(lock);
1723         /* if lock is not granted then there are BL ASTs in progress and
1724          * lock will be granted in result of reprocessing with CP callback
1725          * notifying about that. The mdt object has to be kept until that and
1726          * it is saved in l_ast_data of the lock. Lock reference is kept too
1727          * until that to prevent it from canceling.
1728          */
1729         if (!is_granted_or_cancelled_nolock(lock)) {
1730                 mdt_object_get(info->mti_env, mo);
1731                 lock->l_ast_data = mo;
1732                 unlock_res_and_lock(lock);
1733         } else {
1734                 unlock_res_and_lock(lock);
1735                 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1736         }
1737         LDLM_LOCK_PUT(lock);
1738
1739         RETURN_EXIT;
1740 }