Whamcloud - gitweb
929563b7178b1555104b4c5223453205c856f4dc
[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;
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                 GOTO(unlock, rc);
660
661         if (la->la_valid) {
662                 rc = dt_attr_set(env, dob, la, th);
663                 if (rc)
664                         GOTO(unlock, rc);
665         }
666         /* get attr to return */
667         rc = dt_attr_get(env, dob, la);
668 unlock:
669         dt_write_unlock(env, dob);
670
671 out_stop:
672         /* Force commit to make the just-deleted blocks
673          * reusable. LU-456 */
674         if (rc == -ENOSPC)
675                 th->th_sync = 1;
676
677
678         if (rc == 0 && granted > 0) {
679                 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
680                         granted = 0;
681         }
682
683         th->th_result = rc;
684         dt_trans_stop(env, dt, th);
685         if (rc == -ENOSPC && retries++ < 3) {
686                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
687                        retries);
688                 goto retry;
689         }
690
691 out:
692         dt_bufs_put(env, dob, lnb, niocount);
693         mdt_dom_read_unlock(mo);
694         if (granted > 0)
695                 tgt_grant_commit(exp, granted, old_rc);
696         RETURN(rc);
697 }
698
699 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
700                             bool increase_only)
701 {
702         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
703         struct ldlm_res_id resid;
704         struct ldlm_resource *res;
705
706         fid_build_reg_res_name(mdt_object_fid(mo), &resid);
707         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
708                                 LDLM_IBITS, 1);
709         if (IS_ERR(res))
710                 return;
711
712         /* Update lvbo data if exists. */
713         if (mdt_dom_lvb_is_valid(res))
714                 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
715         ldlm_resource_putref(res);
716 }
717
718 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
719                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
720                      struct niobuf_remote *rnb, int npages,
721                      struct niobuf_local *lnb, int old_rc)
722 {
723         struct mdt_thread_info *info = mdt_th_info(env);
724         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
725         struct mdt_object *mo = info->mti_object;
726         struct lu_attr *la = &info->mti_attr.ma_attr;
727         __u64 valid;
728         int rc = 0;
729
730         LASSERT(mo);
731
732         if (cmd == OBD_BRW_WRITE) {
733                 /* Don't update timestamps if this write is older than a
734                  * setattr which modifies the timestamps. b=10150 */
735
736                 /* XXX when we start having persistent reservations this needs
737                  * to be changed to ofd_fmd_get() to create the fmd if it
738                  * doesn't already exist so we can store the reservation handle
739                  * there. */
740                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
741                 if (tgt_fmd_check(exp, mdt_object_fid(mo),
742                                   mdt_info_req(info)->rq_xid))
743                         valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
744                                  OBD_MD_FLCTIME;
745
746                 la_from_obdo(la, oa, valid);
747
748                 rc = mdt_commitrw_write(env, exp, mdt, mo, la, oa, objcount,
749                                         npages, lnb, oa->o_grant_used, old_rc);
750                 if (rc == 0)
751                         obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
752                 else
753                         obdo_from_la(oa, la, LA_GID | LA_UID);
754
755                 mdt_dom_obj_lvb_update(env, mo, false);
756                 /* don't report overquota flag if we failed before reaching
757                  * commit */
758                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
759                         /* return the overquota flags to client */
760                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
761                                 if (oa->o_valid & OBD_MD_FLFLAGS)
762                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
763                                 else
764                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
765                         }
766
767                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
768                                 if (oa->o_valid & OBD_MD_FLFLAGS)
769                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
770                                 else
771                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
772                         }
773
774                         if (lnb[0].lnb_flags & OBD_BRW_OVER_PRJQUOTA) {
775                                 if (oa->o_valid & OBD_MD_FLFLAGS)
776                                         oa->o_flags |= OBD_FL_NO_PRJQUOTA;
777                                 else
778                                         oa->o_flags = OBD_FL_NO_PRJQUOTA;
779                         }
780
781                         oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
782                                        OBD_MD_FLGRPQUOTA | OBD_MD_FLPRJQUOTA;
783                 }
784         } else if (cmd == OBD_BRW_READ) {
785                 /* If oa != NULL then mdt_preprw_read updated the inode
786                  * atime and we should update the lvb so that other glimpses
787                  * will also get the updated value. bug 5972 */
788                 if (oa)
789                         mdt_dom_obj_lvb_update(env, mo, true);
790                 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
791                 if (old_rc)
792                         rc = old_rc;
793         } else {
794                 rc = -EPROTO;
795         }
796         mdt_thread_info_fini(info);
797         RETURN(rc);
798 }
799
800 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
801                      struct dt_object *dob, __u64 start, __u64 end,
802                      struct lu_attr *la)
803 {
804         struct thandle *th;
805         int rc;
806
807         ENTRY;
808
809         /* we support truncate, not punch yet */
810         LASSERT(end == OBD_OBJECT_EOF);
811
812         if (!dt_object_exists(dob))
813                 RETURN(-ENOENT);
814
815         th = dt_trans_create(env, dt);
816         if (IS_ERR(th))
817                 RETURN(PTR_ERR(th));
818
819         rc = dt_declare_attr_set(env, dob, la, th);
820         if (rc)
821                 GOTO(stop, rc);
822
823         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
824         if (rc)
825                 GOTO(stop, rc);
826
827         tgt_vbr_obj_set(env, dob);
828         rc = dt_trans_start(env, dt, th);
829         if (rc)
830                 GOTO(stop, rc);
831
832         dt_write_lock(env, dob, 0);
833         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
834         if (rc)
835                 GOTO(unlock, rc);
836         rc = dt_attr_set(env, dob, la, th);
837         if (rc)
838                 GOTO(unlock, rc);
839 unlock:
840         dt_write_unlock(env, dob);
841 stop:
842         th->th_result = rc;
843         dt_trans_stop(env, dt, th);
844         RETURN(rc);
845 }
846
847 int mdt_punch_hdl(struct tgt_session_info *tsi)
848 {
849         const struct obdo *oa = &tsi->tsi_ost_body->oa;
850         struct ptlrpc_request *req = tgt_ses_req(tsi);
851         struct ost_body *repbody;
852         struct mdt_thread_info *info;
853         struct lu_attr *la;
854         struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
855         struct obd_export *exp = tsi->tsi_exp;
856         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
857         struct mdt_object *mo;
858         struct dt_object *dob;
859         __u64 flags = 0;
860         struct lustre_handle lh = { 0, };
861         ktime_t kstart = ktime_get();
862         __u64 start, end;
863         int rc;
864         bool srvlock;
865
866         ENTRY;
867
868         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
869         BUILD_BUG_ON(!(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK));
870
871         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
872             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
873                 RETURN(err_serious(-EPROTO));
874
875         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
876         if (repbody == NULL)
877                 RETURN(err_serious(-ENOMEM));
878
879         /* punch start,end are passed in o_size,o_blocks throught wire */
880         start = oa->o_size;
881         end = oa->o_blocks;
882
883         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
884                 RETURN(-EPROTO);
885
886         info = tsi2mdt_info(tsi);
887         la = &info->mti_attr.ma_attr;
888         /* standard truncate optimization: if file body is completely
889          * destroyed, don't send data back to the server. */
890         if (start == 0)
891                 flags |= LDLM_FL_AST_DISCARD_DATA;
892
893         repbody->oa.o_oi = oa->o_oi;
894         repbody->oa.o_valid = OBD_MD_FLID;
895
896         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
897                   oa->o_valid & OBD_MD_FLFLAGS &&
898                   oa->o_flags & OBD_FL_SRVLOCK;
899
900         if (srvlock) {
901                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
902                                        &flags);
903                 if (rc != 0)
904                         GOTO(out, rc);
905         }
906
907         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
908                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
909                oa->o_valid, start, end);
910
911         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
912         if (IS_ERR(mo))
913                 GOTO(out_unlock, rc = PTR_ERR(mo));
914
915         if (!mdt_object_exists(mo))
916                 GOTO(out_put, rc = -ENOENT);
917
918         /* Shouldn't happen on dirs */
919         if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
920                 rc = -EPERM;
921                 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
922                        exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
923                 GOTO(out_put, rc);
924         }
925
926         mdt_dom_write_lock(mo);
927         dob = mdt_obj2dt(mo);
928
929         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
930         la->la_size = start;
931         la->la_valid |= LA_SIZE;
932
933         /* MDT supports FMD for Data-on-MDT needs */
934         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
935                 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
936                                tgt_ses_req(tsi)->rq_xid);
937
938         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
939                               start, end, la);
940         mdt_dom_write_unlock(mo);
941         if (rc)
942                 GOTO(out_put, rc);
943
944         mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
945         mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
946                          ktime_us_delta(ktime_get(), kstart));
947         EXIT;
948 out_put:
949         lu_object_put(tsi->tsi_env, &mo->mot_obj);
950 out_unlock:
951         if (srvlock)
952                 tgt_data_unlock(&lh, LCK_PW);
953 out:
954         mdt_thread_info_fini(info);
955         return rc;
956 }
957
958 /**
959  * MDT glimpse for Data-on-MDT
960  *
961  * If there is write lock on client then function issues glimpse_ast to get
962  * an actual size from that client.
963  *
964  */
965 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
966                    struct ldlm_resource *res)
967 {
968         union ldlm_policy_data policy;
969         struct lustre_handle lockh;
970         enum ldlm_mode mode;
971         struct ldlm_lock *lock;
972         struct ldlm_glimpse_work *gl_work;
973         LIST_HEAD(gl_list);
974         int rc;
975
976         ENTRY;
977
978         /* There can be only one write lock covering data, try to match it. */
979         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
980         mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
981                                &res->lr_name, LDLM_IBITS, &policy,
982                                LCK_PW, &lockh);
983
984         /* There is no PW lock on this object; finished. */
985         if (mode == 0)
986                 RETURN(0);
987
988         lock = ldlm_handle2lock(&lockh);
989         if (lock == NULL)
990                 RETURN(0);
991
992         /*
993          * This check is for lock taken in mdt_reint_unlink() that does
994          * not have l_glimpse_ast set. So the logic is: if there is a lock
995          * with no l_glimpse_ast set, this object is being destroyed already.
996          * Hence, if you are grabbing DLM locks on the server, always set
997          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
998          */
999         if (lock->l_glimpse_ast == NULL) {
1000                 LDLM_DEBUG(lock, "no l_glimpse_ast");
1001                 GOTO(out, rc = -ENOENT);
1002         }
1003
1004         OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1005         if (!gl_work)
1006                 GOTO(out, rc = -ENOMEM);
1007
1008         /* Populate the gl_work structure.
1009          * Grab additional reference on the lock which will be released in
1010          * ldlm_work_gl_ast_lock() */
1011         gl_work->gl_lock = LDLM_LOCK_GET(lock);
1012         /* The glimpse callback is sent to one single IO lock. As a result,
1013          * the gl_work list is just composed of one element */
1014         list_add_tail(&gl_work->gl_list, &gl_list);
1015         /* There is actually no need for a glimpse descriptor when glimpsing
1016          * IO locks */
1017         gl_work->gl_desc = NULL;
1018         /* the ldlm_glimpse_work structure is allocated on the stack */
1019         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1020
1021         ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1022
1023         /* If the list is not empty, we failed to glimpse a lock and
1024          * must clean it up. Usually due to a race with unlink.*/
1025         if (!list_empty(&gl_list)) {
1026                 LDLM_LOCK_RELEASE(lock);
1027                 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1028         }
1029         rc = 0;
1030         EXIT;
1031 out:
1032         LDLM_LOCK_PUT(lock);
1033         return rc;
1034 }
1035
1036 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1037                           struct ost_lvb *lvb)
1038 {
1039         struct ost_lvb *res_lvb;
1040
1041         lock_res(res);
1042         res_lvb = res->lr_lvb_data;
1043         if (lvb)
1044                 *lvb = *res_lvb;
1045
1046         if (mb) {
1047                 mb->mbo_dom_size = res_lvb->lvb_size;
1048                 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1049                 mb->mbo_mtime = res_lvb->lvb_mtime;
1050                 mb->mbo_ctime = res_lvb->lvb_ctime;
1051                 mb->mbo_atime = res_lvb->lvb_atime;
1052                 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1053                                  OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1054         }
1055         CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1056         unlock_res(res);
1057 }
1058
1059 /**
1060  * MDT glimpse for Data-on-MDT
1061  *
1062  * This function is called when MDT get attributes for the DoM object.
1063  * If there is write lock on client then function issues glimpse_ast to get
1064  * an actual size from that client.
1065  */
1066 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1067                         const struct lu_fid *fid, struct mdt_body *mb,
1068                         bool dom_lock)
1069 {
1070         struct ldlm_res_id resid;
1071         struct ldlm_resource *res;
1072         int rc = 0;
1073
1074         ENTRY;
1075
1076         fid_build_reg_res_name(fid, &resid);
1077         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1078                                 LDLM_IBITS, 1);
1079         if (IS_ERR(res))
1080                 RETURN(-ENOENT);
1081
1082         /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1083         if (dom_lock || !mdt_dom_lvb_is_valid(res))
1084                 mdt_dom_lvbo_update(res, NULL, NULL, false);
1085
1086         mdt_lvb2reply(res, mb, NULL);
1087         ldlm_resource_putref(res);
1088         RETURN(rc);
1089 }
1090
1091 /**
1092  * MDT DoM lock intent policy (glimpse)
1093  *
1094  * Intent policy is called when lock has an intent, for DoM file that
1095  * means glimpse lock and policy fills Lock Value Block (LVB).
1096  *
1097  * If already granted lock is found it will be placed in \a lockp and
1098  * returned back to caller function.
1099  *
1100  * \param[in] tsi        session info
1101  * \param[in,out] lockp  pointer to the lock
1102  * \param[in] flags      LDLM flags
1103  *
1104  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
1105  *                      and placed in \a lockp
1106  * \retval              ELDLM_LOCK_ABORTED in other cases except error
1107  * \retval              negative value on error
1108  */
1109 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1110                         struct ldlm_lock **lockp, __u64 flags)
1111 {
1112         struct ldlm_lock *lock = *lockp;
1113         struct ldlm_resource *res = lock->l_resource;
1114         ldlm_processing_policy policy;
1115         struct ldlm_reply *rep;
1116         struct mdt_body *mbo;
1117         struct ost_lvb *lvb;
1118         bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1119         int rc;
1120
1121         ENTRY;
1122
1123         policy = ldlm_get_processing_policy(res);
1124         LASSERT(policy != NULL);
1125
1126         if (unlikely(old_client)) {
1127                 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1128                 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1129         } else {
1130                 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1131                                      sizeof(*lvb));
1132         }
1133         rc = req_capsule_server_pack(mti->mti_pill);
1134         if (rc)
1135                 RETURN(err_serious(rc));
1136
1137         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1138
1139         if (unlikely(old_client)) {
1140                 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1141                 LASSERT(mbo);
1142                 lvb = NULL;
1143         } else {
1144                 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1145                 LASSERT(lvb);
1146                 mbo = NULL;
1147         }
1148
1149         lock_res(res);
1150         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1151          * lock was found by ldlm_handle_enqueue(); if so no need to grant
1152          * it again. */
1153         if (flags & LDLM_FL_RESENT) {
1154                 rc = LDLM_ITER_CONTINUE;
1155         } else {
1156                 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1157                 enum ldlm_error err;
1158
1159                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1160                 check_res_locked(res);
1161         }
1162         unlock_res(res);
1163
1164         /* The lock met with no resistance; we're finished. */
1165         if (rc == LDLM_ITER_CONTINUE) {
1166                 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1167         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1168                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1169                  * callback for glimpse size. The real size user will trigger
1170                  * the glimpse callback when necessary. */
1171                 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1172         }
1173
1174         rc = mdt_do_glimpse(mti->mti_env, ns, res);
1175         if (rc == -ENOENT) {
1176                 /* We are racing with unlink(); just return -ENOENT */
1177                 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1178         } else if (rc == -EINVAL) {
1179                 /* this is possible is client lock has been cancelled but
1180                  * still exists on server. If that lock was found on server
1181                  * as only conflicting lock then the client has already
1182                  * size authority and glimpse is not needed. */
1183                 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1184         } else if (rc < 0) {
1185                 RETURN(rc);
1186         }
1187         rc = ELDLM_LOCK_ABORTED;
1188 fill_mbo:
1189         /* LVB can be without valid data in case of DOM */
1190         if (!mdt_dom_lvb_is_valid(res))
1191                 mdt_dom_lvbo_update(res, lock, NULL, false);
1192         mdt_lvb2reply(res, mbo, lvb);
1193
1194         RETURN(rc);
1195 }
1196
1197 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1198                     struct ldlm_lock **lockp, __u64 flags)
1199 {
1200         struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1201         struct lu_fid *fid = &tsi->tsi_fid;
1202         struct ldlm_lock *lock = *lockp;
1203         struct ldlm_resource *res = lock->l_resource;
1204         struct ldlm_reply *rep;
1205         struct mdt_body *mbo;
1206         struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1207         struct mdt_object *mo;
1208         int rc = 0;
1209
1210         ENTRY;
1211
1212         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1213         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1214         rc = req_capsule_server_pack(mti->mti_pill);
1215         if (rc)
1216                 RETURN(err_serious(rc));
1217
1218         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1219         if (rep == NULL)
1220                 RETURN(-EPROTO);
1221
1222         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1223         if (mbo == NULL)
1224                 RETURN(-EPROTO);
1225
1226         fid_extract_from_res_name(fid, &res->lr_name);
1227         mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1228         if (unlikely(IS_ERR(mo)))
1229                 RETURN(PTR_ERR(mo));
1230
1231         if (!mdt_object_exists(mo))
1232                 GOTO(out, rc = -ENOENT);
1233
1234         if (mdt_object_remote(mo))
1235                 GOTO(out, rc = -EPROTO);
1236
1237         /* Get lock from request for possible resent case. */
1238         mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1239         /* resent case */
1240         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1241                 mdt_lock_handle_init(lhc);
1242                 mdt_lh_reg_init(lhc, *lockp);
1243
1244                 /* This will block MDT thread but it should be fine until
1245                  * client caches small amount of data for DoM, which should be
1246                  * smaller than one BRW RPC and should be able to be
1247                  * piggybacked by lock cancel RPC.
1248                  * If the client could hold the lock too long, this code can be
1249                  * revised to call mdt_object_lock_try(). And if fails, it will
1250                  * return ELDLM_OK here and fall back into normal lock enqueue
1251                  * process.
1252                  */
1253                 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1254                 if (rc)
1255                         GOTO(out, rc);
1256         }
1257
1258         if (!mdt_dom_lvb_is_valid(res)) {
1259                 rc = mdt_dom_lvb_alloc(res);
1260                 if (rc)
1261                         GOTO(out_fail, rc);
1262                 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1263         }
1264         mdt_lvb2reply(res, mbo, NULL);
1265 out_fail:
1266         rep->lock_policy_res2 = clear_serious(rc);
1267         if (rep->lock_policy_res2) {
1268                 lhc->mlh_reg_lh.cookie = 0ull;
1269                 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1270         }
1271
1272         rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1273 out:
1274         if (rc < 0)
1275                 lhc->mlh_reg_lh.cookie = 0ull;
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         __u64 open_flags = info->mti_spec.sp_cr_flags;
1288         struct lustre_handle lockh;
1289         enum ldlm_mode mode;
1290         struct ldlm_lock *lock;
1291         enum ldlm_mode lm;
1292         bool rc;
1293
1294         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1295         fid_build_reg_res_name(fid, res_id);
1296
1297
1298         lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1299         mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1300                                LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1301                                lm, &lockh);
1302
1303         /* There is no other PW lock on this object; finished. */
1304         if (mode == 0)
1305                 return false;
1306
1307         lock = ldlm_handle2lock(&lockh);
1308         if (lock == 0)
1309                 return false;
1310
1311         /* check if lock from the same client */
1312         rc = (lock->l_export->exp_handle.h_cookie ==
1313               info->mti_exp->exp_handle.h_cookie);
1314         LDLM_LOCK_PUT(lock);
1315         return rc;
1316 }
1317
1318 /**
1319  * MDT request handler for OST_GETATTR RPC.
1320  *
1321  * This is data-specific request to get object and layout versions under
1322  * IO lock. It is reliable only for Data-on-MDT files.
1323  *
1324  * \param[in] tsi target session environment for this request
1325  *
1326  * \retval 0 if successful
1327  * \retval negative value on error
1328  */
1329 int mdt_data_version_get(struct tgt_session_info *tsi)
1330 {
1331         struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1332         struct mdt_device *mdt = mti->mti_mdt;
1333         struct mdt_body *repbody;
1334         struct mdt_object *mo = mti->mti_object;
1335         struct lov_comp_md_v1 *comp;
1336         struct lustre_handle lh = { 0 };
1337         __u64 flags = 0;
1338         __s64 version;
1339         enum ldlm_mode lock_mode = LCK_PR;
1340         bool srvlock;
1341         int rc;
1342
1343         ENTRY;
1344
1345         req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1346         req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1347         rc = req_capsule_server_pack(tsi->tsi_pill);
1348         if (unlikely(rc != 0))
1349                 RETURN(err_serious(rc));
1350
1351         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1352         if (repbody == NULL)
1353                 RETURN(-ENOMEM);
1354
1355         srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1356                   tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1357
1358         if (srvlock) {
1359                 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1360                         lock_mode = LCK_PW;
1361
1362                 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1363                 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1364                                        &lh, lock_mode, &flags);
1365                 if (rc != 0)
1366                         RETURN(rc);
1367         }
1368
1369         if (!mdt_object_exists(mo))
1370                 GOTO(out, rc = -ENOENT);
1371         if (mdt_object_remote(mo))
1372                 GOTO(out, rc = -EREMOTE);
1373         if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1374                 GOTO(out, rc = -EBADF);
1375
1376         /* Get version first */
1377         version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1378         if (version && version != -EOPNOTSUPP) {
1379                 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1380                 /* re-use mbo_ioepoch to transfer version */
1381                 repbody->mbo_version = version;
1382         }
1383
1384         /* Read layout to get its version */
1385         rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1386         if (rc == -ENODATA) /* File has no layout yet */
1387                 GOTO(out, rc = 0);
1388         else if (rc < 0)
1389                 GOTO(out, rc);
1390
1391         comp = mti->mti_buf.lb_buf;
1392         if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1393                 CDEBUG(D_INFO, DFID" has no composite layout",
1394                        PFID(&tsi->tsi_fid));
1395                 GOTO(out, rc = -ESTALE);
1396         }
1397
1398         CDEBUG(D_INODE, DFID": layout version: %u\n",
1399                PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1400
1401         repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1402         /* re-use mbo_rdev for that */
1403         repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1404         rc = 0;
1405 out:
1406         if (srvlock)
1407                 tgt_data_unlock(&lh, lock_mode);
1408
1409         repbody->mbo_valid |= OBD_MD_FLFLAGS;
1410         repbody->mbo_flags = OBD_FL_FLUSH;
1411         RETURN(rc);
1412 }
1413
1414 /* read file data to the buffer */
1415 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1416                          struct lustre_handle *lh)
1417 {
1418         const struct lu_env *env = mti->mti_env;
1419         struct tgt_session_info *tsi = tgt_ses_info(env);
1420         struct req_capsule *pill = tsi->tsi_pill;
1421         const struct lu_fid *fid;
1422         struct ptlrpc_request *req = tgt_ses_req(tsi);
1423         struct mdt_body *mbo;
1424         struct dt_device *dt = mdt->mdt_bottom;
1425         struct dt_object *mo;
1426         void *buf;
1427         struct niobuf_remote *rnb = NULL;
1428         struct niobuf_local *lnb;
1429         int rc;
1430         loff_t offset;
1431         unsigned int len, copied = 0;
1432         __u64 real_dom_size;
1433         int lnbs, nr_local, i;
1434         bool dom_lock = false;
1435
1436         ENTRY;
1437
1438         if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1439                 /* There is no reply buffers for this field, this means that
1440                  * client has no support for data in reply.
1441                  */
1442                 RETURN(0);
1443         }
1444
1445         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1446         if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1447                 RETURN(0);
1448
1449         if (!mbo->mbo_dom_size)
1450                 RETURN(0);
1451
1452         if (lustre_handle_is_used(lh)) {
1453                 struct ldlm_lock *lock;
1454
1455                 lock = ldlm_handle2lock(lh);
1456                 if (lock) {
1457                         dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1458                         LDLM_LOCK_PUT(lock);
1459                 }
1460         }
1461
1462         /* return data along with open only along with DoM lock */
1463         if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1464                 RETURN(0);
1465
1466         /* if DoM object holds encrypted content, we need to make sure we
1467          * send whole encryption units, or client will read corrupted content
1468          */
1469         if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1470             mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1471                 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1472                                 LUSTRE_ENCRYPTION_UNIT_SIZE;
1473         else
1474                 real_dom_size = mbo->mbo_dom_size;
1475
1476         CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1477                real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1478         len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1479
1480         /* NB: at this moment we have the following sizes:
1481          * - req->rq_replen: used data in reply
1482          * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1483          *
1484          * Ideal case when file size fits in allocated reply buffer,
1485          * that mean we can return whole data in reply. We can also fit more
1486          * data up to max_reply_size in total reply size, but this will cause
1487          * re-allocation on client and resend with larger buffer. This is still
1488          * faster than separate READ IO.
1489          * Third case if file is too big to fit even in maximum size, in that
1490          * case we return just tail to optimize possible append.
1491          *
1492          * At the moment the following strategy is used:
1493          * 1) try to fit into the buffer we have
1494          * 2) return just file tail otherwise.
1495          */
1496         if (real_dom_size <= len) {
1497                 /* can fit whole data */
1498                 len = real_dom_size;
1499                 offset = 0;
1500         } else if (real_dom_size <
1501                    mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1502                 int tail, pgbits;
1503
1504                 /* File tail offset must be aligned with larger page size
1505                  * between client and server, so the maximum page size is
1506                  * used here to align offset.
1507                  *
1508                  * NB: DOM feature was introduced when server supports pagebits
1509                  * already, so it should be always non-zero value. Report error
1510                  * if it is not for some reason.
1511                  */
1512                 if (!req->rq_export->exp_target_data.ted_pagebits) {
1513                         CERROR("%s: client page bits are not saved on server\n",
1514                                mdt_obd_name(mdt));
1515                         RETURN(0);
1516                 }
1517                 pgbits = max_t(int, PAGE_SHIFT,
1518                                req->rq_export->exp_target_data.ted_pagebits);
1519                 tail = real_dom_size % (1 << pgbits);
1520
1521                 /* no partial tail or tail can't fit in reply */
1522                 if (tail == 0 || len < tail)
1523                         RETURN(0);
1524
1525                 len = tail;
1526                 offset = real_dom_size - len;
1527         } else {
1528                 /* DOM stripe is fully written, so don't expect its tail
1529                  * will be used by append.
1530                  */
1531                 RETURN(0);
1532         }
1533
1534         LASSERT((offset & ~PAGE_MASK) == 0);
1535         rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1536                                      sizeof(*rnb) + len);
1537         if (rc != 0) {
1538                 /* failed to grow data buffer, just exit */
1539                 GOTO(out, rc = -E2BIG);
1540         }
1541
1542         /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1543         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1544         fid = &mbo->mbo_fid1;
1545         if (!fid_is_sane(fid))
1546                 GOTO(out, rc = -EINVAL);
1547
1548         rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1549         if (rnb == NULL)
1550                 GOTO(out, rc = -EPROTO);
1551
1552         buf = (char *)rnb + sizeof(*rnb);
1553         rnb->rnb_len = len;
1554         rnb->rnb_offset = offset;
1555
1556         mo = dt_locate(env, dt, fid);
1557         if (IS_ERR(mo))
1558                 GOTO(out_rnb, rc = PTR_ERR(mo));
1559         LASSERT(mo != NULL);
1560
1561         dt_read_lock(env, mo, 0);
1562         if (!dt_object_exists(mo))
1563                 GOTO(unlock, rc = -ENOENT);
1564
1565         /* parse remote buffers to local buffers and prepare the latter */
1566         lnbs = (len >> PAGE_SHIFT) + 1;
1567         OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1568         if (lnb == NULL)
1569                 GOTO(unlock, rc = -ENOMEM);
1570
1571         rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1572         if (unlikely(rc < 0))
1573                 GOTO(free, rc);
1574         LASSERT(rc <= lnbs);
1575         nr_local = rc;
1576         rc = dt_read_prep(env, mo, lnb, nr_local);
1577         if (unlikely(rc))
1578                 GOTO(buf_put, rc);
1579         /* copy data to the buffer finally */
1580         for (i = 0; i < nr_local; i++) {
1581                 char *p = kmap(lnb[i].lnb_page);
1582                 long off;
1583
1584                 LASSERT(lnb[i].lnb_page_offset == 0);
1585                 off = lnb[i].lnb_len & ~PAGE_MASK;
1586                 if (off > 0)
1587                         memset(p + off, 0, PAGE_SIZE - off);
1588
1589                 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1590                 kunmap(lnb[i].lnb_page);
1591                 copied += lnb[i].lnb_len;
1592                 LASSERT(rc <= len);
1593         }
1594         CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1595                len, offset);
1596         if (copied < len) {
1597                 CWARN("%s: read %i bytes for "DFID
1598                       " but wanted %u, is size wrong?\n",
1599                       tsi->tsi_exp->exp_obd->obd_name, copied,
1600                       PFID(&tsi->tsi_fid), len);
1601                 /* Ignore partially copied data */
1602                 copied = 0;
1603         }
1604         EXIT;
1605 buf_put:
1606         dt_bufs_put(env, mo, lnb, nr_local);
1607 free:
1608         OBD_FREE_PTR_ARRAY(lnb, lnbs);
1609 unlock:
1610         dt_read_unlock(env, mo);
1611         lu_object_put(env, &mo->do_lu);
1612 out_rnb:
1613         rnb->rnb_len = copied;
1614 out:
1615         /* Don't fail OPEN request if read-on-open is failed, but drop
1616          * a message in log about the error.
1617          */
1618         if (rc)
1619                 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1620
1621         RETURN(0);
1622 }
1623
1624 /**
1625  * Completion AST for DOM discard locks:
1626  *
1627  * CP AST an DOM discard lock is called always right after enqueue or from
1628  * reprocess if lock was blocked, in the latest case l_ast_data is set to
1629  * the mdt_object which is kept while there are pending locks on it.
1630  */
1631 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1632 {
1633         struct mdt_object *mo;
1634         struct lustre_handle dom_lh;
1635         struct lu_env *env;
1636
1637         ENTRY;
1638
1639         /* l_ast_data is set when lock was not granted immediately
1640          * in mdt_dom_discard_data() below but put into waiting list,
1641          * so this CP callback means we are finished and corresponding
1642          * MDT object should be released finally as well as lock itself.
1643          */
1644         lock_res_and_lock(lock);
1645         if (!lock->l_ast_data) {
1646                 unlock_res_and_lock(lock);
1647                 RETURN(0);
1648         }
1649
1650         mo = lock->l_ast_data;
1651         lock->l_ast_data = NULL;
1652         unlock_res_and_lock(lock);
1653
1654         ldlm_lock2handle(lock, &dom_lh);
1655         ldlm_lock_decref(&dom_lh, LCK_PW);
1656
1657         env = lu_env_find();
1658         LASSERT(env);
1659         mdt_object_put(env, mo);
1660
1661         RETURN(0);
1662 }
1663
1664 void mdt_dom_discard_data(struct mdt_thread_info *info,
1665                           struct mdt_object *mo)
1666 {
1667         struct ptlrpc_request *req = mdt_info_req(info);
1668         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1669         union ldlm_policy_data policy;
1670         struct ldlm_res_id res_id;
1671         struct lustre_handle dom_lh;
1672         struct ldlm_lock *lock;
1673         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1674         int rc = 0;
1675         bool old_client;
1676
1677         ENTRY;
1678
1679         if (req && req_is_replay(req))
1680                 RETURN_EXIT;
1681
1682         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1683         policy.l_inodebits.try_bits = 0;
1684         fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1685
1686         /* Keep blocking version of discard for an old client to avoid
1687          * crashes on non-patched clients. LU-11359.
1688          */
1689         old_client = req && !(exp_connect_flags2(req->rq_export) &
1690                               OBD_CONNECT2_ASYNC_DISCARD);
1691
1692         /* Tell the clients that the object is gone now and that they should
1693          * throw away any cached pages. */
1694         rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1695                                     LDLM_IBITS, &policy, LCK_PW, &flags,
1696                                     ldlm_blocking_ast, old_client ?
1697                                     ldlm_completion_ast :
1698                                     ldlm_dom_discard_cp_ast,
1699                                     NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1700         if (rc != ELDLM_OK) {
1701                 CDEBUG(D_DLMTRACE,
1702                        "Failed to issue discard lock, rc = %d\n", rc);
1703                 RETURN_EXIT;
1704         }
1705
1706         lock = ldlm_handle2lock(&dom_lh);
1707         lock_res_and_lock(lock);
1708         /* if lock is not granted then there are BL ASTs in progress and
1709          * lock will be granted in result of reprocessing with CP callback
1710          * notifying about that. The mdt object has to be kept until that and
1711          * it is saved in l_ast_data of the lock. Lock reference is kept too
1712          * until that to prevent it from canceling.
1713          */
1714         if (!is_granted_or_cancelled_nolock(lock)) {
1715                 mdt_object_get(info->mti_env, mo);
1716                 lock->l_ast_data = mo;
1717                 unlock_res_and_lock(lock);
1718         } else {
1719                 unlock_res_and_lock(lock);
1720                 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1721         }
1722         LDLM_LOCK_PUT(lock);
1723
1724         RETURN_EXIT;
1725 }