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