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