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