Whamcloud - gitweb
LU-11164 ldlm: pass env to lvbo methods
[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
387         ENTRY;
388
389         mdt_dom_read_lock(mo);
390         if (!mdt_object_exists(mo))
391                 GOTO(unlock, rc = -ENOENT);
392
393         dob = mdt_obj2dt(mo);
394         /* parse remote buffers to local buffers and prepare the latter */
395         *nr_local = 0;
396         for (i = 0, j = 0; i < niocount; i++) {
397                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 0);
398                 if (unlikely(rc < 0))
399                         GOTO(buf_put, rc);
400                 /* correct index for local buffers to continue with */
401                 j += rc;
402                 *nr_local += rc;
403                 tot_bytes += rnb[i].rnb_len;
404         }
405
406         rc = dt_attr_get(env, dob, la);
407         if (unlikely(rc))
408                 GOTO(buf_put, rc);
409
410         rc = dt_read_prep(env, dob, lnb, *nr_local);
411         if (unlikely(rc))
412                 GOTO(buf_put, rc);
413
414         mdt_io_counter_incr(exp, LPROC_MDT_IO_READ, jobid, tot_bytes);
415         RETURN(0);
416 buf_put:
417         dt_bufs_put(env, dob, lnb, *nr_local);
418 unlock:
419         mdt_dom_read_unlock(mo);
420         return rc;
421 }
422
423 static int mdt_preprw_write(const struct lu_env *env, struct obd_export *exp,
424                             struct mdt_device *mdt, struct mdt_object *mo,
425                             struct lu_attr *la, struct obdo *oa,
426                             int objcount, struct obd_ioobj *obj,
427                             struct niobuf_remote *rnb, int *nr_local,
428                             struct niobuf_local *lnb, char *jobid)
429 {
430         struct dt_object *dob;
431         int i, j, k, rc = 0, tot_bytes = 0;
432
433         ENTRY;
434
435         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
436          * space back if possible */
437         tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
438
439         mdt_dom_read_lock(mo);
440         if (!mdt_object_exists(mo)) {
441                 CDEBUG(D_ERROR, "%s: BRW to missing obj "DFID"\n",
442                        exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)));
443                 GOTO(unlock, rc = -ENOENT);
444         }
445
446         dob = mdt_obj2dt(mo);
447         /* parse remote buffers to local buffers and prepare the latter */
448         *nr_local = 0;
449         for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
450                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 1);
451                 if (unlikely(rc < 0))
452                         GOTO(err, rc);
453                 /* correct index for local buffers to continue with */
454                 for (k = 0; k < rc; k++) {
455                         lnb[j + k].lnb_flags = rnb[i].rnb_flags;
456                         if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
457                                 lnb[j + k].lnb_rc = -ENOSPC;
458                 }
459                 j += rc;
460                 *nr_local += rc;
461                 tot_bytes += rnb[i].rnb_len;
462         }
463
464         rc = dt_write_prep(env, dob, lnb, *nr_local);
465         if (likely(rc))
466                 GOTO(err, rc);
467
468         mdt_io_counter_incr(exp, LPROC_MDT_IO_WRITE, jobid, tot_bytes);
469         RETURN(0);
470 err:
471         dt_bufs_put(env, dob, lnb, *nr_local);
472 unlock:
473         mdt_dom_read_unlock(mo);
474         /* tgt_grant_prepare_write() was called, so we must commit */
475         tgt_grant_commit(exp, oa->o_grant_used, rc);
476         /* let's still process incoming grant information packed in the oa,
477          * but without enforcing grant since we won't proceed with the write.
478          * Just like a read request actually. */
479         tgt_grant_prepare_read(env, exp, oa);
480         return rc;
481 }
482
483 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
484                    struct obdo *oa, int objcount, struct obd_ioobj *obj,
485                    struct niobuf_remote *rnb, int *nr_local,
486                    struct niobuf_local *lnb)
487 {
488         struct tgt_session_info *tsi = tgt_ses_info(env);
489         struct mdt_thread_info *info = tsi2mdt_info(tsi);
490         struct lu_attr *la = &info->mti_attr.ma_attr;
491         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
492         struct mdt_object *mo;
493         char *jobid;
494         int rc = 0;
495
496         /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
497          * but for MDT it is different, correct it here. */
498         if (*nr_local > MD_MAX_BRW_PAGES)
499                 *nr_local = MD_MAX_BRW_PAGES;
500
501         jobid = tsi->tsi_jobid;
502
503         if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
504                 CERROR("%s: bad parameters %p/%i/%i\n",
505                        exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
506                 rc = -EPROTO;
507         }
508
509         mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
510         if (IS_ERR(mo))
511                 GOTO(out, rc = PTR_ERR(mo));
512
513         LASSERT(info->mti_object == NULL);
514         info->mti_object = mo;
515
516         if (cmd == OBD_BRW_WRITE) {
517                 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
518                 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
519                                       objcount, obj, rnb, nr_local, lnb,
520                                       jobid);
521         } else if (cmd == OBD_BRW_READ) {
522                 tgt_grant_prepare_read(env, exp, oa);
523                 rc = mdt_preprw_read(env, exp, mdt, mo, la,
524                                      obj->ioo_bufcnt, rnb, nr_local, lnb,
525                                      jobid);
526                 obdo_from_la(oa, la, LA_ATIME);
527         } else {
528                 CERROR("%s: wrong cmd %d received!\n",
529                        exp->exp_obd->obd_name, cmd);
530                 rc = -EPROTO;
531         }
532         if (rc) {
533                 lu_object_put(env, &mo->mot_obj);
534                 info->mti_object = NULL;
535         }
536 out:
537         RETURN(rc);
538 }
539
540 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
541                              struct mdt_object *mo, int objcount, int niocount,
542                              struct niobuf_local *lnb)
543 {
544         struct dt_object *dob;
545         int rc = 0;
546
547         ENTRY;
548
549         LASSERT(niocount > 0);
550
551         dob = mdt_obj2dt(mo);
552
553         dt_bufs_put(env, dob, lnb, niocount);
554
555         mdt_dom_read_unlock(mo);
556         RETURN(rc);
557 }
558
559 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
560                               struct mdt_device *mdt, struct mdt_object *mo,
561                               struct lu_attr *la, int objcount, int niocount,
562                               struct niobuf_local *lnb, unsigned long granted,
563                               int old_rc)
564 {
565         struct dt_device *dt = mdt->mdt_bottom;
566         struct dt_object *dob;
567         struct thandle *th;
568         int rc = 0;
569         int retries = 0;
570         int i;
571
572         ENTRY;
573
574         dob = mdt_obj2dt(mo);
575
576         if (old_rc)
577                 GOTO(out, rc = old_rc);
578
579         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
580 retry:
581         if (!dt_object_exists(dob))
582                 GOTO(out, rc = -ENOENT);
583
584         th = dt_trans_create(env, dt);
585         if (IS_ERR(th))
586                 GOTO(out, rc = PTR_ERR(th));
587
588         for (i = 0; i < niocount; i++) {
589                 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
590                         th->th_sync = 1;
591                         break;
592                 }
593         }
594
595         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
596                 GOTO(out_stop, rc = -EINPROGRESS);
597
598         rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
599         if (rc)
600                 GOTO(out_stop, rc);
601
602         if (la->la_valid) {
603                 /* update [mac]time if needed */
604                 rc = dt_declare_attr_set(env, dob, la, th);
605                 if (rc)
606                         GOTO(out_stop, rc);
607         }
608
609         tgt_vbr_obj_set(env, dob);
610         rc = dt_trans_start(env, dt, th);
611         if (rc)
612                 GOTO(out_stop, rc);
613
614         dt_write_lock(env, dob, 0);
615         rc = dt_write_commit(env, dob, lnb, niocount, th);
616         if (rc)
617                 GOTO(unlock, rc);
618
619         if (la->la_valid) {
620                 rc = dt_attr_set(env, dob, la, th);
621                 if (rc)
622                         GOTO(unlock, rc);
623         }
624         /* get attr to return */
625         rc = dt_attr_get(env, dob, la);
626 unlock:
627         dt_write_unlock(env, dob);
628
629 out_stop:
630         /* Force commit to make the just-deleted blocks
631          * reusable. LU-456 */
632         if (rc == -ENOSPC)
633                 th->th_sync = 1;
634
635
636         if (rc == 0 && granted > 0) {
637                 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
638                         granted = 0;
639         }
640
641         th->th_result = rc;
642         dt_trans_stop(env, dt, th);
643         if (rc == -ENOSPC && retries++ < 3) {
644                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
645                        retries);
646                 goto retry;
647         }
648
649 out:
650         dt_bufs_put(env, dob, lnb, niocount);
651         mdt_dom_read_unlock(mo);
652         if (granted > 0)
653                 tgt_grant_commit(exp, granted, old_rc);
654         RETURN(rc);
655 }
656
657 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
658                             bool increase_only)
659 {
660         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
661         struct ldlm_res_id resid;
662         struct ldlm_resource *res;
663
664         fid_build_reg_res_name(mdt_object_fid(mo), &resid);
665         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
666                                 LDLM_IBITS, 1);
667         if (IS_ERR(res))
668                 return;
669
670         /* Update lvbo data if exists. */
671         if (mdt_dom_lvb_is_valid(res))
672                 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
673         ldlm_resource_putref(res);
674 }
675
676 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
677                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
678                      struct niobuf_remote *rnb, int npages,
679                      struct niobuf_local *lnb, int old_rc)
680 {
681         struct mdt_thread_info *info = mdt_th_info(env);
682         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
683         struct mdt_object *mo = info->mti_object;
684         struct lu_attr *la = &info->mti_attr.ma_attr;
685         __u64 valid;
686         int rc = 0;
687
688         if (npages == 0) {
689                 CERROR("%s: no pages to commit\n",
690                        exp->exp_obd->obd_name);
691                 rc = -EPROTO;
692         }
693
694         LASSERT(mo);
695
696         if (cmd == OBD_BRW_WRITE) {
697                 /* Don't update timestamps if this write is older than a
698                  * setattr which modifies the timestamps. b=10150 */
699
700                 /* XXX when we start having persistent reservations this needs
701                  * to be changed to ofd_fmd_get() to create the fmd if it
702                  * doesn't already exist so we can store the reservation handle
703                  * there. */
704                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
705                 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
706
707                 la_from_obdo(la, oa, valid);
708
709                 rc = mdt_commitrw_write(env, exp, mdt, mo, la, objcount,
710                                         npages, lnb, oa->o_grant_used, old_rc);
711                 if (rc == 0)
712                         obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
713                 else
714                         obdo_from_la(oa, la, LA_GID | LA_UID);
715
716                 mdt_dom_obj_lvb_update(env, mo, false);
717                 /* don't report overquota flag if we failed before reaching
718                  * commit */
719                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
720                         /* return the overquota flags to client */
721                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
722                                 if (oa->o_valid & OBD_MD_FLFLAGS)
723                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
724                                 else
725                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
726                         }
727
728                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
729                                 if (oa->o_valid & OBD_MD_FLFLAGS)
730                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
731                                 else
732                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
733                         }
734
735                         oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
736                                        OBD_MD_FLGRPQUOTA;
737                 }
738         } else if (cmd == OBD_BRW_READ) {
739                 /* If oa != NULL then mdt_preprw_read updated the inode
740                  * atime and we should update the lvb so that other glimpses
741                  * will also get the updated value. bug 5972 */
742                 if (oa)
743                         mdt_dom_obj_lvb_update(env, mo, true);
744                 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
745                 if (old_rc)
746                         rc = old_rc;
747         } else {
748                 rc = -EPROTO;
749         }
750         /* this put is pair to object_get in ofd_preprw_write */
751         mdt_thread_info_fini(info);
752         RETURN(rc);
753 }
754
755 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
756                      struct dt_object *dob, __u64 start, __u64 end,
757                      struct lu_attr *la)
758 {
759         struct thandle *th;
760         int rc;
761
762         ENTRY;
763
764         /* we support truncate, not punch yet */
765         LASSERT(end == OBD_OBJECT_EOF);
766
767         if (!dt_object_exists(dob))
768                 RETURN(-ENOENT);
769
770         th = dt_trans_create(env, dt);
771         if (IS_ERR(th))
772                 RETURN(PTR_ERR(th));
773
774         rc = dt_declare_attr_set(env, dob, la, th);
775         if (rc)
776                 GOTO(stop, rc);
777
778         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
779         if (rc)
780                 GOTO(stop, rc);
781
782         tgt_vbr_obj_set(env, dob);
783         rc = dt_trans_start(env, dt, th);
784         if (rc)
785                 GOTO(stop, rc);
786
787         dt_write_lock(env, dob, 0);
788         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
789         if (rc)
790                 GOTO(unlock, rc);
791         rc = dt_attr_set(env, dob, la, th);
792         if (rc)
793                 GOTO(unlock, rc);
794 unlock:
795         dt_write_unlock(env, dob);
796 stop:
797         th->th_result = rc;
798         dt_trans_stop(env, dt, th);
799         RETURN(rc);
800 }
801
802 int mdt_punch_hdl(struct tgt_session_info *tsi)
803 {
804         const struct obdo *oa = &tsi->tsi_ost_body->oa;
805         struct ost_body *repbody;
806         struct mdt_thread_info *info;
807         struct lu_attr *la;
808         struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
809         struct obd_export *exp = tsi->tsi_exp;
810         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
811         struct mdt_object *mo;
812         struct dt_object *dob;
813         __u64 flags = 0;
814         struct lustre_handle lh = { 0, };
815         __u64 start, end;
816         int rc;
817         bool srvlock;
818
819         ENTRY;
820
821         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
822         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
823
824         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
825             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
826                 RETURN(err_serious(-EPROTO));
827
828         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
829         if (repbody == NULL)
830                 RETURN(err_serious(-ENOMEM));
831
832         /* punch start,end are passed in o_size,o_blocks throught wire */
833         start = oa->o_size;
834         end = oa->o_blocks;
835
836         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
837                 RETURN(-EPROTO);
838
839         info = tsi2mdt_info(tsi);
840         la = &info->mti_attr.ma_attr;
841         /* standard truncate optimization: if file body is completely
842          * destroyed, don't send data back to the server. */
843         if (start == 0)
844                 flags |= LDLM_FL_AST_DISCARD_DATA;
845
846         repbody->oa.o_oi = oa->o_oi;
847         repbody->oa.o_valid = OBD_MD_FLID;
848
849         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
850                   oa->o_valid & OBD_MD_FLFLAGS &&
851                   oa->o_flags & OBD_FL_SRVLOCK;
852
853         if (srvlock) {
854                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
855                                        &flags);
856                 if (rc != 0)
857                         GOTO(out, rc);
858         }
859
860         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
861                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
862                oa->o_valid, start, end);
863
864         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
865         if (IS_ERR(mo))
866                 GOTO(out_unlock, rc = PTR_ERR(mo));
867
868         if (!mdt_object_exists(mo))
869                 GOTO(out_put, rc = -ENOENT);
870
871         /* Shouldn't happen on dirs */
872         if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
873                 rc = -EPERM;
874                 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
875                        exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
876                 GOTO(out_put, rc);
877         }
878
879         mdt_dom_write_lock(mo);
880         dob = mdt_obj2dt(mo);
881
882         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
883         la->la_size = start;
884         la->la_valid |= LA_SIZE;
885
886         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
887                               start, end, la);
888         mdt_dom_write_unlock(mo);
889         if (rc)
890                 GOTO(out_put, rc);
891
892         mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
893         mdt_io_counter_incr(tsi->tsi_exp, LPROC_MDT_IO_PUNCH,
894                             tsi->tsi_jobid, 1);
895         EXIT;
896 out_put:
897         lu_object_put(tsi->tsi_env, &mo->mot_obj);
898 out_unlock:
899         if (srvlock)
900                 tgt_extent_unlock(&lh, LCK_PW);
901 out:
902         mdt_thread_info_fini(info);
903         return rc;
904 }
905
906 /**
907  * MDT glimpse for Data-on-MDT
908  *
909  * If there is write lock on client then function issues glimpse_ast to get
910  * an actual size from that client.
911  *
912  */
913 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
914                    struct ldlm_resource *res)
915 {
916         union ldlm_policy_data policy;
917         struct lustre_handle lockh;
918         enum ldlm_mode mode;
919         struct ldlm_lock *lock;
920         struct ldlm_glimpse_work *gl_work;
921         struct list_head gl_list;
922         int rc;
923
924         ENTRY;
925
926         /* There can be only one write lock covering data, try to match it. */
927         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
928         mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
929                                &res->lr_name, LDLM_IBITS, &policy,
930                                LCK_PW, &lockh, 0);
931
932         /* There is no PW lock on this object; finished. */
933         if (mode == 0)
934                 RETURN(0);
935
936         lock = ldlm_handle2lock(&lockh);
937         if (lock == NULL)
938                 RETURN(0);
939
940         /*
941          * This check is for lock taken in mdt_reint_unlink() that does
942          * not have l_glimpse_ast set. So the logic is: if there is a lock
943          * with no l_glimpse_ast set, this object is being destroyed already.
944          * Hence, if you are grabbing DLM locks on the server, always set
945          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
946          */
947         if (lock->l_glimpse_ast == NULL) {
948                 LDLM_DEBUG(lock, "no l_glimpse_ast");
949                 GOTO(out, rc = -ENOENT);
950         }
951
952         OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
953         if (!gl_work)
954                 GOTO(out, rc = -ENOMEM);
955
956         /* Populate the gl_work structure.
957          * Grab additional reference on the lock which will be released in
958          * ldlm_work_gl_ast_lock() */
959         gl_work->gl_lock = LDLM_LOCK_GET(lock);
960         /* The glimpse callback is sent to one single IO lock. As a result,
961          * the gl_work list is just composed of one element */
962         INIT_LIST_HEAD(&gl_list);
963         list_add_tail(&gl_work->gl_list, &gl_list);
964         /* There is actually no need for a glimpse descriptor when glimpsing
965          * IO locks */
966         gl_work->gl_desc = NULL;
967         /* the ldlm_glimpse_work structure is allocated on the stack */
968         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
969
970         ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
971
972         /* If the list is not empty, we failed to glimpse a lock and
973          * must clean it up. Usually due to a race with unlink.*/
974         if (!list_empty(&gl_list)) {
975                 LDLM_LOCK_RELEASE(lock);
976                 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
977         }
978         rc = 0;
979         EXIT;
980 out:
981         LDLM_LOCK_PUT(lock);
982         return rc;
983 }
984
985 static void mdt_lvb2body(struct ldlm_resource *res, struct mdt_body *mb)
986 {
987         struct ost_lvb *res_lvb;
988
989         lock_res(res);
990         res_lvb = res->lr_lvb_data;
991         mb->mbo_dom_size = res_lvb->lvb_size;
992         mb->mbo_dom_blocks = res_lvb->lvb_blocks;
993         mb->mbo_mtime = res_lvb->lvb_mtime;
994         mb->mbo_ctime = res_lvb->lvb_ctime;
995         mb->mbo_atime = res_lvb->lvb_atime;
996
997         CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
998
999         mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME | OBD_MD_FLMTIME |
1000                          OBD_MD_DOM_SIZE;
1001         unlock_res(res);
1002 }
1003
1004 /**
1005  * MDT glimpse for Data-on-MDT
1006  *
1007  * This function is called when MDT get attributes for the DoM object.
1008  * If there is write lock on client then function issues glimpse_ast to get
1009  * an actual size from that client.
1010  */
1011 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1012                         const struct lu_fid *fid, struct mdt_body *mb,
1013                         bool dom_lock)
1014 {
1015         struct ldlm_res_id resid;
1016         struct ldlm_resource *res;
1017         int rc = 0;
1018
1019         ENTRY;
1020
1021         fid_build_reg_res_name(fid, &resid);
1022         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1023                                 LDLM_IBITS, 1);
1024         if (IS_ERR(res))
1025                 RETURN(-ENOENT);
1026
1027         /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1028         if (dom_lock || !mdt_dom_lvb_is_valid(res))
1029                 mdt_dom_lvbo_update(env, res, NULL, NULL, false);
1030
1031         mdt_lvb2body(res, mb);
1032         ldlm_resource_putref(res);
1033         RETURN(rc);
1034 }
1035
1036 /**
1037  * MDT DoM lock intent policy (glimpse)
1038  *
1039  * Intent policy is called when lock has an intent, for DoM file that
1040  * means glimpse lock and policy fills Lock Value Block (LVB).
1041  *
1042  * If already granted lock is found it will be placed in \a lockp and
1043  * returned back to caller function.
1044  *
1045  * \param[in] tsi        session info
1046  * \param[in,out] lockp  pointer to the lock
1047  * \param[in] flags      LDLM flags
1048  *
1049  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
1050  *                      and placed in \a lockp
1051  * \retval              ELDLM_LOCK_ABORTED in other cases except error
1052  * \retval              negative value on error
1053  */
1054 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1055                         struct ldlm_lock **lockp, __u64 flags)
1056 {
1057         struct ldlm_lock *lock = *lockp;
1058         struct ldlm_resource *res = lock->l_resource;
1059         ldlm_processing_policy policy;
1060         struct ldlm_reply *rep;
1061         struct mdt_body *mbo;
1062         int rc;
1063
1064         ENTRY;
1065
1066         policy = ldlm_get_processing_policy(res);
1067         LASSERT(policy != NULL);
1068
1069         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1070         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1071         rc = req_capsule_server_pack(mti->mti_pill);
1072         if (rc)
1073                 RETURN(err_serious(rc));
1074
1075         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1076         if (rep == NULL)
1077                 RETURN(-EPROTO);
1078
1079         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1080         if (mbo == NULL)
1081                 RETURN(-EPROTO);
1082
1083         lock_res(res);
1084         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1085          * lock was found by ldlm_handle_enqueue(); if so no need to grant
1086          * it again. */
1087         if (flags & LDLM_FL_RESENT) {
1088                 rc = LDLM_ITER_CONTINUE;
1089         } else {
1090                 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1091                 enum ldlm_error err;
1092
1093                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1094                 check_res_locked(res);
1095         }
1096         unlock_res(res);
1097
1098         /* The lock met with no resistance; we're finished. */
1099         if (rc == LDLM_ITER_CONTINUE) {
1100                 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1101         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1102                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1103                  * callback for glimpse size. The real size user will trigger
1104                  * the glimpse callback when necessary. */
1105                 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1106         }
1107
1108         rc = mdt_do_glimpse(mti->mti_env, ns, res);
1109         if (rc == -ENOENT) {
1110                 /* We are racing with unlink(); just return -ENOENT */
1111                 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1112                 rc = 0;
1113         } else if (rc == -EINVAL) {
1114                 /* this is possible is client lock has been cancelled but
1115                  * still exists on server. If that lock was found on server
1116                  * as only conflicting lock then the client has already
1117                  * size authority and glimpse is not needed. */
1118                 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1119                 rc = 0;
1120         } else if (rc < 0) {
1121                 RETURN(rc);
1122         }
1123         rc = ELDLM_LOCK_ABORTED;
1124 fill_mbo:
1125         /* LVB can be without valid data in case of DOM */
1126         if (!mdt_dom_lvb_is_valid(res))
1127                 mdt_dom_lvbo_update(mti->mti_env, res, lock, NULL, false);
1128         mdt_lvb2body(res, mbo);
1129         RETURN(rc);
1130 }
1131
1132 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1133                     struct ldlm_lock **lockp, __u64 flags)
1134 {
1135         struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1136         struct lu_fid *fid = &tsi->tsi_fid;
1137         struct ldlm_lock *lock = *lockp;
1138         struct ldlm_resource *res = lock->l_resource;
1139         struct ldlm_reply *rep;
1140         struct mdt_body *mbo;
1141         struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1142         struct mdt_object *mo;
1143         int rc = 0;
1144
1145         ENTRY;
1146
1147         /* Get lock from request for possible resent case. */
1148         mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1149         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1150         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1151         rc = req_capsule_server_pack(mti->mti_pill);
1152         if (rc)
1153                 RETURN(err_serious(rc));
1154
1155         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1156         if (rep == NULL)
1157                 RETURN(-EPROTO);
1158
1159         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1160         if (mbo == NULL)
1161                 RETURN(-EPROTO);
1162
1163         fid_extract_from_res_name(fid, &res->lr_name);
1164         mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1165         if (unlikely(IS_ERR(mo)))
1166                 RETURN(PTR_ERR(mo));
1167
1168         if (!mdt_object_exists(mo))
1169                 GOTO(out, rc = -ENOENT);
1170
1171         if (mdt_object_remote(mo))
1172                 GOTO(out, rc = -EPROTO);
1173
1174         /* resent case */
1175         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1176                 mdt_lock_handle_init(lhc);
1177                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
1178                 /* This will block MDT thread but it should be fine until
1179                  * client caches small amount of data for DoM, which should be
1180                  * smaller than one BRW RPC and should be able to be
1181                  * piggybacked by lock cancel RPC.
1182                  * If the client could hold the lock too long, this code can be
1183                  * revised to call mdt_object_lock_try(). And if fails, it will
1184                  * return ELDLM_OK here and fall back into normal lock enqueue
1185                  * process.
1186                  */
1187                 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1188                 if (rc)
1189                         GOTO(out, rc);
1190         }
1191
1192         if (!mdt_dom_lvb_is_valid(res)) {
1193                 rc = mdt_dom_lvb_alloc(res);
1194                 if (rc)
1195                         GOTO(out_fail, rc);
1196                 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1197         }
1198         mdt_lvb2body(res, mbo);
1199 out_fail:
1200         rep->lock_policy_res2 = clear_serious(rc);
1201         if (rep->lock_policy_res2) {
1202                 lhc->mlh_reg_lh.cookie = 0ull;
1203                 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1204         }
1205
1206         rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1207 out:
1208         mdt_object_put(mti->mti_env, mo);
1209         RETURN(rc);
1210 }
1211
1212 void mdt_dom_discard_data(struct mdt_thread_info *info,
1213                           const struct lu_fid *fid)
1214 {
1215         struct mdt_device *mdt = info->mti_mdt;
1216         union ldlm_policy_data *policy = &info->mti_policy;
1217         struct ldlm_res_id *res_id = &info->mti_res_id;
1218         struct lustre_handle dom_lh;
1219         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1220         int rc = 0;
1221
1222         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1223         policy->l_inodebits.try_bits = 0;
1224         fid_build_reg_res_name(fid, res_id);
1225
1226         /* Tell the clients that the object is gone now and that they should
1227          * throw away any cached pages. */
1228         rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, res_id,
1229                                     LDLM_IBITS, policy, LCK_PW, &flags,
1230                                     ldlm_blocking_ast, ldlm_completion_ast,
1231                                     NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1232
1233         /* We only care about the side-effects, just drop the lock. */
1234         if (rc == ELDLM_OK)
1235                 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1236 }
1237
1238 /* check if client has already DoM lock for given resource */
1239 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1240                              const struct lu_fid *fid)
1241 {
1242         struct mdt_device *mdt = info->mti_mdt;
1243         union ldlm_policy_data *policy = &info->mti_policy;
1244         struct ldlm_res_id *res_id = &info->mti_res_id;
1245         struct lustre_handle lockh;
1246         enum ldlm_mode mode;
1247         struct ldlm_lock *lock;
1248         bool rc;
1249
1250         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1251         fid_build_reg_res_name(fid, res_id);
1252
1253         mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1254                                LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1255                                LCK_PW, &lockh, 0);
1256
1257         /* There is no other PW lock on this object; finished. */
1258         if (mode == 0)
1259                 return false;
1260
1261         lock = ldlm_handle2lock(&lockh);
1262         if (lock == 0)
1263                 return false;
1264
1265         /* check if lock from the same client */
1266         rc = (lock->l_export->exp_handle.h_cookie ==
1267               info->mti_exp->exp_handle.h_cookie);
1268         LDLM_LOCK_PUT(lock);
1269         return rc;
1270 }
1271
1272 /**
1273  * MDT request handler for OST_GETATTR RPC.
1274  *
1275  * This is data-specific request to get object and layout versions under
1276  * IO lock. It is reliable only for Data-on-MDT files.
1277  *
1278  * \param[in] tsi target session environment for this request
1279  *
1280  * \retval 0 if successful
1281  * \retval negative value on error
1282  */
1283 int mdt_data_version_get(struct tgt_session_info *tsi)
1284 {
1285         struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1286         struct mdt_device *mdt = mti->mti_mdt;
1287         struct mdt_body *repbody;
1288         struct mdt_object *mo = mti->mti_object;
1289         struct lov_comp_md_v1 *comp;
1290         struct lustre_handle lh = { 0 };
1291         __u64 flags = 0;
1292         __s64 version;
1293         enum ldlm_mode lock_mode = LCK_PR;
1294         bool srvlock;
1295         int rc;
1296
1297         ENTRY;
1298
1299         req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1300         req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1301         rc = req_capsule_server_pack(tsi->tsi_pill);
1302         if (unlikely(rc != 0))
1303                 RETURN(err_serious(rc));
1304
1305         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1306         if (repbody == NULL)
1307                 RETURN(-ENOMEM);
1308
1309         srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1310                   tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1311
1312         if (srvlock) {
1313                 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1314                         lock_mode = LCK_PW;
1315
1316                 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1317                 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1318                                        &lh, lock_mode, &flags);
1319                 if (rc != 0)
1320                         RETURN(rc);
1321         }
1322
1323         if (!mdt_object_exists(mo))
1324                 GOTO(out, rc = -ENOENT);
1325         if (mdt_object_remote(mo))
1326                 GOTO(out, rc = -EREMOTE);
1327         if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1328                 GOTO(out, rc = -EBADF);
1329
1330         /* Get version first */
1331         version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1332         if (version && version != -EOPNOTSUPP) {
1333                 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1334                 /* re-use mbo_ioepoch to transfer version */
1335                 repbody->mbo_version = version;
1336         }
1337
1338         /* Read layout to get its version */
1339         rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1340         if (rc == -ENODATA) /* File has no layout yet */
1341                 GOTO(out, rc = 0);
1342         else if (rc < 0)
1343                 GOTO(out, rc);
1344
1345         comp = mti->mti_buf.lb_buf;
1346         if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1347                 CDEBUG(D_INFO, DFID" has no composite layout",
1348                        PFID(&tsi->tsi_fid));
1349                 GOTO(out, rc = -ESTALE);
1350         }
1351
1352         CDEBUG(D_INODE, DFID": layout version: %u\n",
1353                PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1354
1355         repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1356         /* re-use mbo_rdev for that */
1357         repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1358         rc = 0;
1359 out:
1360         if (srvlock)
1361                 tgt_mdt_data_unlock(&lh, lock_mode);
1362
1363         repbody->mbo_valid |= OBD_MD_FLFLAGS;
1364         repbody->mbo_flags = OBD_FL_FLUSH;
1365         RETURN(rc);
1366 }
1367
1368 /* read file data to the buffer */
1369 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1370                          struct lustre_handle *lh)
1371 {
1372         const struct lu_env *env = mti->mti_env;
1373         struct tgt_session_info *tsi = tgt_ses_info(env);
1374         struct req_capsule *pill = tsi->tsi_pill;
1375         const struct lu_fid *fid;
1376         struct ptlrpc_request *req = tgt_ses_req(tsi);
1377         struct mdt_body *mbo;
1378         struct dt_device *dt = mdt->mdt_bottom;
1379         struct dt_object *mo;
1380         void *buf;
1381         struct niobuf_remote *rnb = NULL;
1382         struct niobuf_local *lnb;
1383         int rc;
1384         int max_reply_len;
1385         loff_t offset;
1386         unsigned int len, copied = 0;
1387         int lnbs, nr_local, i;
1388         bool dom_lock = false;
1389
1390         ENTRY;
1391
1392         if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1393                 /* There is no reply buffers for this field, this means that
1394                  * client has no support for data in reply.
1395                  */
1396                 RETURN(0);
1397         }
1398
1399         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1400
1401         if (lustre_handle_is_used(lh)) {
1402                 struct ldlm_lock *lock;
1403
1404                 lock = ldlm_handle2lock(lh);
1405                 if (lock) {
1406                         dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1407                         LDLM_LOCK_PUT(lock);
1408                 }
1409         }
1410
1411         /* return data along with open only along with DoM lock */
1412         if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1413                 RETURN(0);
1414
1415         if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1416                 RETURN(0);
1417
1418         if (mbo->mbo_dom_size == 0)
1419                 RETURN(0);
1420
1421         /* check the maximum size available in reply */
1422         max_reply_len =
1423                 req->rq_rqbd->rqbd_svcpt->scp_service->srv_max_reply_size;
1424
1425         CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d/%d\n",
1426                mbo->mbo_dom_size, max_reply_len, req->rq_reqmsg->lm_repsize,
1427                req->rq_replen);
1428         len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1429         max_reply_len -= req->rq_replen;
1430
1431         /* NB: at this moment we have the following sizes:
1432          * - req->rq_replen: used data in reply
1433          * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1434          * - max_reply_len: maximum reply size allowed by protocol
1435          *
1436          * Ideal case when file size fits in allocated reply buffer,
1437          * that mean we can return whole data in reply. We can also fit more
1438          * data up to max_reply_size in total reply size, but this will cause
1439          * re-allocation on client and resend with larger buffer. This is still
1440          * faster than separate READ IO.
1441          * Third case if file is too big to fit even in maximum size, in that
1442          * case we return just tail to optimize possible append.
1443          *
1444          * At the moment the following strategy is used:
1445          * 1) try to fit into the buffer we have
1446          * 2) respond with bigger buffer so client will re-allocate it and
1447          *    resend (up to srv_max_reply_size value).
1448          * 3) return just file tail otherwise.
1449          */
1450         if (mbo->mbo_dom_size <= len) {
1451                 /* can fit whole data */
1452                 len = mbo->mbo_dom_size;
1453                 offset = 0;
1454         } else if (mbo->mbo_dom_size <= max_reply_len) {
1455                 /* It is worth to make this tunable ON/OFF because this will
1456                  * cause buffer re-allocation and resend
1457                  */
1458                 len = mbo->mbo_dom_size;
1459                 offset = 0;
1460         } else {
1461                 int tail = mbo->mbo_dom_size % PAGE_SIZE;
1462
1463                 /* no tail or tail can't fit in reply */
1464                 if (tail == 0 || len < tail)
1465                         RETURN(0);
1466
1467                 len = tail;
1468                 offset = mbo->mbo_dom_size - len;
1469         }
1470         LASSERT((offset % PAGE_SIZE) == 0);
1471         rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1472                                      sizeof(*rnb) + len);
1473         if (rc != 0) {
1474                 /* failed to grow data buffer, just exit */
1475                 GOTO(out, rc = -E2BIG);
1476         }
1477
1478         /* re-take MDT_BODY buffer after the buffer growing above */
1479         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1480         fid = &mbo->mbo_fid1;
1481         if (!fid_is_sane(fid))
1482                 RETURN(0);
1483
1484         rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1485         if (rnb == NULL)
1486                 GOTO(out, rc = -EPROTO);
1487         buf = (char *)rnb + sizeof(*rnb);
1488         rnb->rnb_len = len;
1489         rnb->rnb_offset = offset;
1490
1491         mo = dt_locate(env, dt, fid);
1492         if (IS_ERR(mo))
1493                 GOTO(out, rc = PTR_ERR(mo));
1494         LASSERT(mo != NULL);
1495
1496         dt_read_lock(env, mo, 0);
1497         if (!dt_object_exists(mo))
1498                 GOTO(unlock, rc = -ENOENT);
1499
1500         /* parse remote buffers to local buffers and prepare the latter */
1501         lnbs = (len >> PAGE_SHIFT) + 1;
1502         OBD_ALLOC(lnb, sizeof(*lnb) * lnbs);
1503         if (lnb == NULL)
1504                 GOTO(unlock, rc = -ENOMEM);
1505
1506         rc = dt_bufs_get(env, mo, rnb, lnb, 0);
1507         if (unlikely(rc < 0))
1508                 GOTO(free, rc);
1509         LASSERT(rc <= lnbs);
1510         nr_local = rc;
1511         rc = dt_read_prep(env, mo, lnb, nr_local);
1512         if (unlikely(rc))
1513                 GOTO(buf_put, rc);
1514         /* copy data to the buffer finally */
1515         for (i = 0; i < nr_local; i++) {
1516                 char *p = kmap(lnb[i].lnb_page);
1517                 long off;
1518
1519                 LASSERT(lnb[i].lnb_page_offset == 0);
1520                 off = lnb[i].lnb_len & ~PAGE_MASK;
1521                 if (off > 0)
1522                         memset(p + off, 0, PAGE_SIZE - off);
1523
1524                 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1525                 kunmap(lnb[i].lnb_page);
1526                 copied += lnb[i].lnb_len;
1527                 LASSERT(rc <= len);
1528         }
1529         CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1530                len, offset);
1531         if (copied < len)
1532                 CWARN("%s: read %i bytes for "DFID
1533                       " but wanted %u, is size wrong?\n",
1534                       tsi->tsi_exp->exp_obd->obd_name, copied,
1535                       PFID(&tsi->tsi_fid), len);
1536         EXIT;
1537 buf_put:
1538         dt_bufs_put(env, mo, lnb, nr_local);
1539 free:
1540         OBD_FREE(lnb, sizeof(*lnb) * lnbs);
1541 unlock:
1542         dt_read_unlock(env, mo);
1543         lu_object_put(env, &mo->do_lu);
1544 out:
1545         if (rnb != NULL)
1546                 rnb->rnb_len = copied;
1547         RETURN(0);
1548 }
1549