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