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