Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
886             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
887                 RETURN(err_serious(-EPROTO));
888
889         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
890         if (repbody == NULL)
891                 RETURN(err_serious(-ENOMEM));
892
893         /* punch start,end are passed in o_size,o_blocks throught wire */
894         start = oa->o_size;
895         end = oa->o_blocks;
896
897         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
898                 RETURN(-EPROTO);
899
900         info = tsi2mdt_info(tsi);
901         la = &info->mti_attr.ma_attr;
902         /* standard truncate optimization: if file body is completely
903          * destroyed, don't send data back to the server. */
904         if (start == 0)
905                 flags |= LDLM_FL_AST_DISCARD_DATA;
906
907         repbody->oa.o_oi = oa->o_oi;
908         repbody->oa.o_valid = OBD_MD_FLID;
909
910         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
911                   oa->o_valid & OBD_MD_FLFLAGS &&
912                   oa->o_flags & OBD_FL_SRVLOCK;
913
914         if (srvlock) {
915                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
916                                        &flags);
917                 if (rc != 0)
918                         GOTO(out, rc);
919         }
920
921         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
922                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
923                oa->o_valid, start, end);
924
925         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
926         if (IS_ERR(mo))
927                 GOTO(out_unlock, rc = PTR_ERR(mo));
928
929         if (!mdt_object_exists(mo))
930                 GOTO(out_put, rc = -ENOENT);
931
932         /* Shouldn't happen on dirs */
933         if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
934                 rc = -EPERM;
935                 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
936                        exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
937                 GOTO(out_put, rc);
938         }
939
940         mdt_dom_write_lock(mo);
941         dob = mdt_obj2dt(mo);
942
943         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
944         la->la_size = start;
945         la->la_valid |= LA_SIZE;
946
947         /* MDT supports FMD for Data-on-MDT needs */
948         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
949                 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
950                                tgt_ses_req(tsi)->rq_xid);
951
952         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
953                               start, end, la);
954         mdt_dom_write_unlock(mo);
955         if (rc)
956                 GOTO(out_put, rc);
957
958         mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
959         mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
960                          ktime_us_delta(ktime_get(), kstart));
961         EXIT;
962 out_put:
963         lu_object_put(tsi->tsi_env, &mo->mot_obj);
964 out_unlock:
965         if (srvlock)
966                 tgt_data_unlock(&lh, LCK_PW);
967 out:
968         mdt_thread_info_fini(info);
969         return rc;
970 }
971
972 /**
973  * MDT glimpse for Data-on-MDT
974  *
975  * If there is write lock on client then function issues glimpse_ast to get
976  * an actual size from that client.
977  *
978  */
979 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
980                    struct ldlm_resource *res)
981 {
982         union ldlm_policy_data policy;
983         struct lustre_handle lockh;
984         enum ldlm_mode mode;
985         struct ldlm_lock *lock;
986         struct ldlm_glimpse_work *gl_work;
987         LIST_HEAD(gl_list);
988         int rc;
989
990         ENTRY;
991
992         /* There can be only one write lock covering data, try to match it. */
993         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
994         mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
995                                &res->lr_name, LDLM_IBITS, &policy,
996                                LCK_PW, &lockh);
997
998         /* There is no PW lock on this object; finished. */
999         if (mode == 0)
1000                 RETURN(0);
1001
1002         lock = ldlm_handle2lock(&lockh);
1003         if (lock == NULL)
1004                 RETURN(0);
1005
1006         /*
1007          * This check is for lock taken in mdt_reint_unlink() that does
1008          * not have l_glimpse_ast set. So the logic is: if there is a lock
1009          * with no l_glimpse_ast set, this object is being destroyed already.
1010          * Hence, if you are grabbing DLM locks on the server, always set
1011          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
1012          */
1013         if (lock->l_glimpse_ast == NULL) {
1014                 LDLM_DEBUG(lock, "no l_glimpse_ast");
1015                 GOTO(out, rc = -ENOENT);
1016         }
1017
1018         OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1019         if (!gl_work)
1020                 GOTO(out, rc = -ENOMEM);
1021
1022         /* Populate the gl_work structure.
1023          * Grab additional reference on the lock which will be released in
1024          * ldlm_work_gl_ast_lock() */
1025         gl_work->gl_lock = LDLM_LOCK_GET(lock);
1026         /* The glimpse callback is sent to one single IO lock. As a result,
1027          * the gl_work list is just composed of one element */
1028         list_add_tail(&gl_work->gl_list, &gl_list);
1029         /* There is actually no need for a glimpse descriptor when glimpsing
1030          * IO locks */
1031         gl_work->gl_desc = NULL;
1032         /* the ldlm_glimpse_work structure is allocated on the stack */
1033         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1034
1035         ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1036
1037         /* If the list is not empty, we failed to glimpse a lock and
1038          * must clean it up. Usually due to a race with unlink.*/
1039         if (!list_empty(&gl_list)) {
1040                 LDLM_LOCK_RELEASE(lock);
1041                 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1042         }
1043         rc = 0;
1044         EXIT;
1045 out:
1046         LDLM_LOCK_PUT(lock);
1047         return rc;
1048 }
1049
1050 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1051                           struct ost_lvb *lvb)
1052 {
1053         struct ost_lvb *res_lvb;
1054
1055         lock_res(res);
1056         res_lvb = res->lr_lvb_data;
1057         if (lvb)
1058                 *lvb = *res_lvb;
1059
1060         if (mb) {
1061                 mb->mbo_dom_size = res_lvb->lvb_size;
1062                 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1063                 mb->mbo_mtime = res_lvb->lvb_mtime;
1064                 mb->mbo_ctime = res_lvb->lvb_ctime;
1065                 mb->mbo_atime = res_lvb->lvb_atime;
1066                 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1067                                  OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1068         }
1069         CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1070         unlock_res(res);
1071 }
1072
1073 /**
1074  * MDT glimpse for Data-on-MDT
1075  *
1076  * This function is called when MDT get attributes for the DoM object.
1077  * If there is write lock on client then function issues glimpse_ast to get
1078  * an actual size from that client.
1079  */
1080 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1081                         const struct lu_fid *fid, struct mdt_body *mb,
1082                         bool dom_lock)
1083 {
1084         struct ldlm_res_id resid;
1085         struct ldlm_resource *res;
1086         int rc = 0;
1087
1088         ENTRY;
1089
1090         fid_build_reg_res_name(fid, &resid);
1091         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1092                                 LDLM_IBITS, 1);
1093         if (IS_ERR(res))
1094                 RETURN(-ENOENT);
1095
1096         /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1097         if (dom_lock || !mdt_dom_lvb_is_valid(res))
1098                 mdt_dom_lvbo_update(res, NULL, NULL, false);
1099
1100         mdt_lvb2reply(res, mb, NULL);
1101         ldlm_resource_putref(res);
1102         RETURN(rc);
1103 }
1104
1105 /**
1106  * MDT DoM lock intent policy (glimpse)
1107  *
1108  * Intent policy is called when lock has an intent, for DoM file that
1109  * means glimpse lock and policy fills Lock Value Block (LVB).
1110  *
1111  * If already granted lock is found it will be placed in \a lockp and
1112  * returned back to caller function.
1113  *
1114  * \param[in] tsi        session info
1115  * \param[in,out] lockp  pointer to the lock
1116  * \param[in] flags      LDLM flags
1117  *
1118  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
1119  *                      and placed in \a lockp
1120  * \retval              ELDLM_LOCK_ABORTED in other cases except error
1121  * \retval              negative value on error
1122  */
1123 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1124                         struct ldlm_lock **lockp, __u64 flags)
1125 {
1126         struct ldlm_lock *lock = *lockp;
1127         struct ldlm_resource *res = lock->l_resource;
1128         ldlm_processing_policy policy;
1129         struct ldlm_reply *rep;
1130         struct mdt_body *mbo;
1131         struct ost_lvb *lvb;
1132         bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1133         int rc;
1134
1135         ENTRY;
1136
1137         policy = ldlm_get_processing_policy(res);
1138         LASSERT(policy != NULL);
1139
1140         if (unlikely(old_client)) {
1141                 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1142                 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1143         } else {
1144                 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1145                                      sizeof(*lvb));
1146         }
1147         rc = req_capsule_server_pack(mti->mti_pill);
1148         if (rc)
1149                 RETURN(err_serious(rc));
1150
1151         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1152
1153         if (unlikely(old_client)) {
1154                 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1155                 LASSERT(mbo);
1156                 lvb = NULL;
1157         } else {
1158                 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1159                 LASSERT(lvb);
1160                 mbo = NULL;
1161         }
1162
1163         lock_res(res);
1164         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1165          * lock was found by ldlm_handle_enqueue(); if so no need to grant
1166          * it again. */
1167         if (flags & LDLM_FL_RESENT) {
1168                 rc = LDLM_ITER_CONTINUE;
1169         } else {
1170                 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1171                 enum ldlm_error err;
1172
1173                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1174                 check_res_locked(res);
1175         }
1176         unlock_res(res);
1177
1178         /* The lock met with no resistance; we're finished. */
1179         if (rc == LDLM_ITER_CONTINUE) {
1180                 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1181         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1182                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1183                  * callback for glimpse size. The real size user will trigger
1184                  * the glimpse callback when necessary. */
1185                 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1186         }
1187
1188         rc = mdt_do_glimpse(mti->mti_env, ns, res);
1189         if (rc == -ENOENT) {
1190                 /* We are racing with unlink(); just return -ENOENT */
1191                 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1192         } else if (rc == -EINVAL) {
1193                 /* this is possible is client lock has been cancelled but
1194                  * still exists on server. If that lock was found on server
1195                  * as only conflicting lock then the client has already
1196                  * size authority and glimpse is not needed. */
1197                 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1198         } else if (rc < 0) {
1199                 RETURN(rc);
1200         }
1201         rc = ELDLM_LOCK_ABORTED;
1202 fill_mbo:
1203         /* LVB can be without valid data in case of DOM */
1204         if (!mdt_dom_lvb_is_valid(res))
1205                 mdt_dom_lvbo_update(res, lock, NULL, false);
1206         mdt_lvb2reply(res, mbo, lvb);
1207
1208         RETURN(rc);
1209 }
1210
1211 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1212                     struct ldlm_lock **lockp, __u64 flags)
1213 {
1214         struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1215         struct lu_fid *fid = &tsi->tsi_fid;
1216         struct ldlm_lock *lock = *lockp;
1217         struct ldlm_resource *res = lock->l_resource;
1218         struct ldlm_reply *rep;
1219         struct mdt_body *mbo;
1220         struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1221         struct mdt_object *mo;
1222         int rc = 0;
1223
1224         ENTRY;
1225
1226         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1227         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1228         rc = req_capsule_server_pack(mti->mti_pill);
1229         if (rc)
1230                 RETURN(err_serious(rc));
1231
1232         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1233         if (rep == NULL)
1234                 RETURN(-EPROTO);
1235
1236         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1237         if (mbo == NULL)
1238                 RETURN(-EPROTO);
1239
1240         fid_extract_from_res_name(fid, &res->lr_name);
1241         mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1242         if (unlikely(IS_ERR(mo)))
1243                 RETURN(PTR_ERR(mo));
1244
1245         if (!mdt_object_exists(mo))
1246                 GOTO(out, rc = -ENOENT);
1247
1248         if (mdt_object_remote(mo))
1249                 GOTO(out, rc = -EPROTO);
1250
1251         /* Get lock from request for possible resent case. */
1252         mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1253         /* resent case */
1254         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1255                 mdt_lock_handle_init(lhc);
1256                 mdt_lh_reg_init(lhc, *lockp);
1257
1258                 /* This will block MDT thread but it should be fine until
1259                  * client caches small amount of data for DoM, which should be
1260                  * smaller than one BRW RPC and should be able to be
1261                  * piggybacked by lock cancel RPC.
1262                  * If the client could hold the lock too long, this code can be
1263                  * revised to call mdt_object_lock_try(). And if fails, it will
1264                  * return ELDLM_OK here and fall back into normal lock enqueue
1265                  * process.
1266                  */
1267                 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1268                 if (rc)
1269                         GOTO(out, rc);
1270         }
1271
1272         if (!mdt_dom_lvb_is_valid(res)) {
1273                 rc = mdt_dom_lvb_alloc(res);
1274                 if (rc)
1275                         GOTO(out_fail, rc);
1276                 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1277         }
1278         mdt_lvb2reply(res, mbo, NULL);
1279 out_fail:
1280         rep->lock_policy_res2 = clear_serious(rc);
1281         if (rep->lock_policy_res2) {
1282                 lhc->mlh_reg_lh.cookie = 0ull;
1283                 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1284         }
1285
1286         rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1287 out:
1288         if (rc < 0)
1289                 lhc->mlh_reg_lh.cookie = 0ull;
1290         mdt_object_put(mti->mti_env, mo);
1291         RETURN(rc);
1292 }
1293
1294 /* check if client has already DoM lock for given resource */
1295 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1296                              const struct lu_fid *fid)
1297 {
1298         struct mdt_device *mdt = info->mti_mdt;
1299         union ldlm_policy_data *policy = &info->mti_policy;
1300         struct ldlm_res_id *res_id = &info->mti_res_id;
1301         __u64 open_flags = info->mti_spec.sp_cr_flags;
1302         struct lustre_handle lockh;
1303         enum ldlm_mode mode;
1304         struct ldlm_lock *lock;
1305         enum ldlm_mode lm;
1306         bool rc;
1307
1308         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1309         fid_build_reg_res_name(fid, res_id);
1310
1311
1312         lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1313         mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1314                                LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1315                                lm, &lockh);
1316
1317         /* There is no other PW lock on this object; finished. */
1318         if (mode == 0)
1319                 return false;
1320
1321         lock = ldlm_handle2lock(&lockh);
1322         if (lock == 0)
1323                 return false;
1324
1325         /* check if lock from the same client */
1326         rc = (lock->l_export->exp_handle.h_cookie ==
1327               info->mti_exp->exp_handle.h_cookie);
1328         LDLM_LOCK_PUT(lock);
1329         return rc;
1330 }
1331
1332 /**
1333  * MDT request handler for OST_GETATTR RPC.
1334  *
1335  * This is data-specific request to get object and layout versions under
1336  * IO lock. It is reliable only for Data-on-MDT files.
1337  *
1338  * \param[in] tsi target session environment for this request
1339  *
1340  * \retval 0 if successful
1341  * \retval negative value on error
1342  */
1343 int mdt_data_version_get(struct tgt_session_info *tsi)
1344 {
1345         struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1346         struct mdt_device *mdt = mti->mti_mdt;
1347         struct mdt_body *repbody;
1348         struct mdt_object *mo = mti->mti_object;
1349         struct lov_comp_md_v1 *comp;
1350         struct lustre_handle lh = { 0 };
1351         __u64 flags = 0;
1352         __s64 version;
1353         enum ldlm_mode lock_mode = LCK_PR;
1354         bool srvlock;
1355         int rc;
1356
1357         ENTRY;
1358
1359         req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1360         req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1361         rc = req_capsule_server_pack(tsi->tsi_pill);
1362         if (unlikely(rc != 0))
1363                 RETURN(err_serious(rc));
1364
1365         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1366         if (repbody == NULL)
1367                 RETURN(-ENOMEM);
1368
1369         srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1370                   tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1371
1372         if (srvlock) {
1373                 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1374                         lock_mode = LCK_PW;
1375
1376                 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1377                 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1378                                        &lh, lock_mode, &flags);
1379                 if (rc != 0)
1380                         RETURN(rc);
1381         }
1382
1383         if (!mdt_object_exists(mo))
1384                 GOTO(out, rc = -ENOENT);
1385         if (mdt_object_remote(mo))
1386                 GOTO(out, rc = -EREMOTE);
1387         if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1388                 GOTO(out, rc = -EBADF);
1389
1390         /* Get version first */
1391         version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1392         if (version && version != -EOPNOTSUPP) {
1393                 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1394                 /* re-use mbo_ioepoch to transfer version */
1395                 repbody->mbo_version = version;
1396         }
1397
1398         /* Read layout to get its version */
1399         rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1400         if (rc == -ENODATA) /* File has no layout yet */
1401                 GOTO(out, rc = 0);
1402         else if (rc < 0)
1403                 GOTO(out, rc);
1404
1405         comp = mti->mti_buf.lb_buf;
1406         if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1407                 CDEBUG(D_INFO, DFID" has no composite layout",
1408                        PFID(&tsi->tsi_fid));
1409                 GOTO(out, rc = -ESTALE);
1410         }
1411
1412         CDEBUG(D_INODE, DFID": layout version: %u\n",
1413                PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1414
1415         repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1416         /* re-use mbo_rdev for that */
1417         repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1418         rc = 0;
1419 out:
1420         if (srvlock)
1421                 tgt_data_unlock(&lh, lock_mode);
1422
1423         repbody->mbo_valid |= OBD_MD_FLFLAGS;
1424         repbody->mbo_flags = OBD_FL_FLUSH;
1425         RETURN(rc);
1426 }
1427
1428 /* read file data to the buffer */
1429 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1430                          struct lustre_handle *lh)
1431 {
1432         const struct lu_env *env = mti->mti_env;
1433         struct tgt_session_info *tsi = tgt_ses_info(env);
1434         struct req_capsule *pill = tsi->tsi_pill;
1435         const struct lu_fid *fid;
1436         struct ptlrpc_request *req = tgt_ses_req(tsi);
1437         struct mdt_body *mbo;
1438         struct dt_device *dt = mdt->mdt_bottom;
1439         struct dt_object *mo;
1440         void *buf;
1441         struct niobuf_remote *rnb = NULL;
1442         struct niobuf_local *lnb;
1443         int rc;
1444         loff_t offset;
1445         unsigned int len, copied = 0;
1446         __u64 real_dom_size;
1447         int lnbs, nr_local, i;
1448         bool dom_lock = false;
1449
1450         ENTRY;
1451
1452         if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1453                 /* There is no reply buffers for this field, this means that
1454                  * client has no support for data in reply.
1455                  */
1456                 RETURN(0);
1457         }
1458
1459         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1460         if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1461                 RETURN(0);
1462
1463         if (!mbo->mbo_dom_size)
1464                 RETURN(0);
1465
1466         if (lustre_handle_is_used(lh)) {
1467                 struct ldlm_lock *lock;
1468
1469                 lock = ldlm_handle2lock(lh);
1470                 if (lock) {
1471                         dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1472                         LDLM_LOCK_PUT(lock);
1473                 }
1474         }
1475
1476         /* return data along with open only along with DoM lock */
1477         if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1478                 RETURN(0);
1479
1480         /* if DoM object holds encrypted content, we need to make sure we
1481          * send whole encryption units, or client will read corrupted content
1482          */
1483         if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1484             mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1485                 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1486                                 LUSTRE_ENCRYPTION_UNIT_SIZE;
1487         else
1488                 real_dom_size = mbo->mbo_dom_size;
1489
1490         CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1491                real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1492         len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1493
1494         /* NB: at this moment we have the following sizes:
1495          * - req->rq_replen: used data in reply
1496          * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1497          *
1498          * Ideal case when file size fits in allocated reply buffer,
1499          * that mean we can return whole data in reply. We can also fit more
1500          * data up to max_reply_size in total reply size, but this will cause
1501          * re-allocation on client and resend with larger buffer. This is still
1502          * faster than separate READ IO.
1503          * Third case if file is too big to fit even in maximum size, in that
1504          * case we return just tail to optimize possible append.
1505          *
1506          * At the moment the following strategy is used:
1507          * 1) try to fit into the buffer we have
1508          * 2) return just file tail otherwise.
1509          */
1510         if (real_dom_size <= len) {
1511                 /* can fit whole data */
1512                 len = real_dom_size;
1513                 offset = 0;
1514         } else if (real_dom_size <
1515                    mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1516                 int tail, pgbits;
1517
1518                 /* File tail offset must be aligned with larger page size
1519                  * between client and server, so the maximum page size is
1520                  * used here to align offset.
1521                  *
1522                  * NB: DOM feature was introduced when server supports pagebits
1523                  * already, so it should be always non-zero value. Report error
1524                  * if it is not for some reason.
1525                  */
1526                 if (!req->rq_export->exp_target_data.ted_pagebits) {
1527                         CERROR("%s: client page bits are not saved on server\n",
1528                                mdt_obd_name(mdt));
1529                         RETURN(0);
1530                 }
1531                 pgbits = max_t(int, PAGE_SHIFT,
1532                                req->rq_export->exp_target_data.ted_pagebits);
1533                 tail = real_dom_size % (1 << pgbits);
1534
1535                 /* no partial tail or tail can't fit in reply */
1536                 if (tail == 0 || len < tail)
1537                         RETURN(0);
1538
1539                 len = tail;
1540                 offset = real_dom_size - len;
1541         } else {
1542                 /* DOM stripe is fully written, so don't expect its tail
1543                  * will be used by append.
1544                  */
1545                 RETURN(0);
1546         }
1547
1548         LASSERT((offset & ~PAGE_MASK) == 0);
1549         rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1550                                      sizeof(*rnb) + len);
1551         if (rc != 0) {
1552                 /* failed to grow data buffer, just exit */
1553                 GOTO(out, rc = -E2BIG);
1554         }
1555
1556         /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1557         mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1558         fid = &mbo->mbo_fid1;
1559         if (!fid_is_sane(fid))
1560                 GOTO(out, rc = -EINVAL);
1561
1562         rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1563         if (rnb == NULL)
1564                 GOTO(out, rc = -EPROTO);
1565
1566         buf = (char *)rnb + sizeof(*rnb);
1567         rnb->rnb_len = len;
1568         rnb->rnb_offset = offset;
1569
1570         mo = dt_locate(env, dt, fid);
1571         if (IS_ERR(mo))
1572                 GOTO(out_rnb, rc = PTR_ERR(mo));
1573         LASSERT(mo != NULL);
1574
1575         dt_read_lock(env, mo, 0);
1576         if (!dt_object_exists(mo))
1577                 GOTO(unlock, rc = -ENOENT);
1578
1579         /* parse remote buffers to local buffers and prepare the latter */
1580         lnbs = (len >> PAGE_SHIFT) + 1;
1581         OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1582         if (lnb == NULL)
1583                 GOTO(unlock, rc = -ENOMEM);
1584
1585         rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1586         if (unlikely(rc < 0))
1587                 GOTO(free, rc);
1588         LASSERT(rc <= lnbs);
1589         nr_local = rc;
1590         rc = dt_read_prep(env, mo, lnb, nr_local);
1591         if (unlikely(rc))
1592                 GOTO(buf_put, rc);
1593         /* copy data to the buffer finally */
1594         for (i = 0; i < nr_local; i++) {
1595                 char *p = kmap(lnb[i].lnb_page);
1596                 long off;
1597
1598                 LASSERT(lnb[i].lnb_page_offset == 0);
1599                 off = lnb[i].lnb_len & ~PAGE_MASK;
1600                 if (off > 0)
1601                         memset(p + off, 0, PAGE_SIZE - off);
1602
1603                 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1604                 kunmap(lnb[i].lnb_page);
1605                 copied += lnb[i].lnb_len;
1606                 LASSERT(rc <= len);
1607         }
1608         CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1609                len, offset);
1610         if (copied < len) {
1611                 CWARN("%s: read %i bytes for "DFID
1612                       " but wanted %u, is size wrong?\n",
1613                       tsi->tsi_exp->exp_obd->obd_name, copied,
1614                       PFID(&tsi->tsi_fid), len);
1615                 /* Ignore partially copied data */
1616                 copied = 0;
1617         }
1618         EXIT;
1619 buf_put:
1620         dt_bufs_put(env, mo, lnb, nr_local);
1621 free:
1622         OBD_FREE_PTR_ARRAY(lnb, lnbs);
1623 unlock:
1624         dt_read_unlock(env, mo);
1625         lu_object_put(env, &mo->do_lu);
1626 out_rnb:
1627         rnb->rnb_len = copied;
1628 out:
1629         /* Don't fail OPEN request if read-on-open is failed, but drop
1630          * a message in log about the error.
1631          */
1632         if (rc)
1633                 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1634
1635         RETURN(0);
1636 }
1637
1638 /**
1639  * Completion AST for DOM discard locks:
1640  *
1641  * CP AST an DOM discard lock is called always right after enqueue or from
1642  * reprocess if lock was blocked, in the latest case l_ast_data is set to
1643  * the mdt_object which is kept while there are pending locks on it.
1644  */
1645 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1646 {
1647         struct mdt_object *mo;
1648         struct lustre_handle dom_lh;
1649         struct lu_env *env;
1650
1651         ENTRY;
1652
1653         /* l_ast_data is set when lock was not granted immediately
1654          * in mdt_dom_discard_data() below but put into waiting list,
1655          * so this CP callback means we are finished and corresponding
1656          * MDT object should be released finally as well as lock itself.
1657          */
1658         lock_res_and_lock(lock);
1659         if (!lock->l_ast_data) {
1660                 unlock_res_and_lock(lock);
1661                 RETURN(0);
1662         }
1663
1664         mo = lock->l_ast_data;
1665         lock->l_ast_data = NULL;
1666         unlock_res_and_lock(lock);
1667
1668         ldlm_lock2handle(lock, &dom_lh);
1669         ldlm_lock_decref(&dom_lh, LCK_PW);
1670
1671         env = lu_env_find();
1672         LASSERT(env);
1673         mdt_object_put(env, mo);
1674
1675         RETURN(0);
1676 }
1677
1678 void mdt_dom_discard_data(struct mdt_thread_info *info,
1679                           struct mdt_object *mo)
1680 {
1681         struct ptlrpc_request *req = mdt_info_req(info);
1682         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1683         union ldlm_policy_data policy;
1684         struct ldlm_res_id res_id;
1685         struct lustre_handle dom_lh;
1686         struct ldlm_lock *lock;
1687         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1688         int rc = 0;
1689         bool old_client;
1690
1691         ENTRY;
1692
1693         if (req && req_is_replay(req))
1694                 RETURN_EXIT;
1695
1696         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1697         policy.l_inodebits.try_bits = 0;
1698         fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1699
1700         /* Keep blocking version of discard for an old client to avoid
1701          * crashes on non-patched clients. LU-11359.
1702          */
1703         old_client = req && !(exp_connect_flags2(req->rq_export) &
1704                               OBD_CONNECT2_ASYNC_DISCARD);
1705
1706         /* Tell the clients that the object is gone now and that they should
1707          * throw away any cached pages. */
1708         rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1709                                     LDLM_IBITS, &policy, LCK_PW, &flags,
1710                                     ldlm_blocking_ast, old_client ?
1711                                     ldlm_completion_ast :
1712                                     ldlm_dom_discard_cp_ast,
1713                                     NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1714         if (rc != ELDLM_OK) {
1715                 CDEBUG(D_DLMTRACE,
1716                        "Failed to issue discard lock, rc = %d\n", rc);
1717                 RETURN_EXIT;
1718         }
1719
1720         lock = ldlm_handle2lock(&dom_lh);
1721         lock_res_and_lock(lock);
1722         /* if lock is not granted then there are BL ASTs in progress and
1723          * lock will be granted in result of reprocessing with CP callback
1724          * notifying about that. The mdt object has to be kept until that and
1725          * it is saved in l_ast_data of the lock. Lock reference is kept too
1726          * until that to prevent it from canceling.
1727          */
1728         if (!is_granted_or_cancelled_nolock(lock)) {
1729                 mdt_object_get(info->mti_env, mo);
1730                 lock->l_ast_data = mo;
1731                 unlock_res_and_lock(lock);
1732         } else {
1733                 unlock_res_and_lock(lock);
1734                 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1735         }
1736         LDLM_LOCK_PUT(lock);
1737
1738         RETURN_EXIT;
1739 }