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