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