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