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