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