Whamcloud - gitweb
LU-3285 mds: add IO locking to the MDC and MDT
[fs/lustre-release.git] / lustre / mdt / mdt_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 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 /* --------------- MDT grant code ---------------- */
37
38 long mdt_grant_connect(const struct lu_env *env,
39                        struct obd_export *exp,
40                        u64 want, bool conservative)
41 {
42         struct mdt_device *mdt = mdt_exp2dev(exp);
43         u64 left;
44         long grant;
45
46         ENTRY;
47
48         dt_statfs(env, mdt->mdt_bottom, &mdt->mdt_osfs);
49
50         left = (mdt->mdt_osfs.os_bavail * mdt->mdt_osfs.os_bsize) / 2;
51
52         grant = left;
53
54         CDEBUG(D_CACHE, "%s: cli %s/%p ocd_grant: %ld want: %llu left: %llu\n",
55                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
56                exp, grant, want, left);
57
58         return grant;
59 }
60
61 void mdt_grant_prepare_write(const struct lu_env *env,
62                              struct obd_export *exp, struct obdo *oa,
63                              struct niobuf_remote *rnb, int niocount)
64 {
65         struct mdt_device *mdt = mdt_exp2dev(exp);
66         u64 left;
67
68         ENTRY;
69
70         left = (mdt->mdt_osfs.os_bavail * mdt->mdt_osfs.os_bsize) / 2;
71
72         /* grant more space back to the client if possible */
73         oa->o_grant = left;
74 }
75 /* ---------------- end of MDT grant code ---------------- */
76
77 /* functions below are stubs for now, they will be implemented with
78  * grant support on MDT */
79 static inline void mdt_io_counter_incr(struct obd_export *exp, int opcode,
80                                        char *jobid, long amount)
81 {
82         return;
83 }
84
85 void mdt_grant_prepare_read(const struct lu_env *env,
86                             struct obd_export *exp, struct obdo *oa)
87 {
88         return;
89 }
90
91 void mdt_grant_commit(struct obd_export *exp, unsigned long pending,
92                       int rc)
93 {
94         return;
95
96 }
97
98 static inline void mdt_dom_read_lock(struct mdt_object *mo)
99 {
100         down_read(&mo->mot_dom_sem);
101 }
102
103 static inline void mdt_dom_read_unlock(struct mdt_object *mo)
104 {
105         up_read(&mo->mot_dom_sem);
106 }
107
108 static inline void mdt_dom_write_lock(struct mdt_object *mo)
109 {
110         down_write(&mo->mot_dom_sem);
111 }
112
113 static inline void mdt_dom_write_unlock(struct mdt_object *mo)
114 {
115         up_write(&mo->mot_dom_sem);
116 }
117
118 static int mdt_preprw_read(const struct lu_env *env, struct obd_export *exp,
119                            struct mdt_device *mdt, struct mdt_object *mo,
120                            struct lu_attr *la, int niocount,
121                            struct niobuf_remote *rnb, int *nr_local,
122                            struct niobuf_local *lnb, char *jobid)
123 {
124         struct dt_object *dob;
125         int i, j, rc, tot_bytes = 0;
126
127         ENTRY;
128
129         mdt_dom_read_lock(mo);
130         if (!mdt_object_exists(mo))
131                 GOTO(unlock, rc = -ENOENT);
132
133         dob = mdt_obj2dt(mo);
134         /* parse remote buffers to local buffers and prepare the latter */
135         *nr_local = 0;
136         for (i = 0, j = 0; i < niocount; i++) {
137                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 0);
138                 if (unlikely(rc < 0))
139                         GOTO(buf_put, rc);
140                 /* correct index for local buffers to continue with */
141                 j += rc;
142                 *nr_local += rc;
143                 tot_bytes += rnb[i].rnb_len;
144         }
145
146         rc = dt_attr_get(env, dob, la);
147         if (unlikely(rc))
148                 GOTO(buf_put, rc);
149
150         rc = dt_read_prep(env, dob, lnb, *nr_local);
151         if (unlikely(rc))
152                 GOTO(buf_put, rc);
153
154         mdt_io_counter_incr(exp, LPROC_MDT_IO_READ, jobid, tot_bytes);
155         RETURN(0);
156 buf_put:
157         dt_bufs_put(env, dob, lnb, *nr_local);
158 unlock:
159         mdt_dom_read_unlock(mo);
160         return rc;
161 }
162
163 static int mdt_preprw_write(const struct lu_env *env, struct obd_export *exp,
164                             struct mdt_device *mdt, struct mdt_object *mo,
165                             struct lu_attr *la, struct obdo *oa,
166                             int objcount, struct obd_ioobj *obj,
167                             struct niobuf_remote *rnb, int *nr_local,
168                             struct niobuf_local *lnb, char *jobid)
169 {
170         struct dt_object *dob;
171         int i, j, k, rc = 0, tot_bytes = 0;
172
173         ENTRY;
174
175         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
176          * space back if possible */
177         mdt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
178
179         mdt_dom_read_lock(mo);
180         if (!mdt_object_exists(mo)) {
181                 CDEBUG(D_ERROR, "%s: BRW to missing obj "DFID"\n",
182                        exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)));
183                 GOTO(unlock, rc = -ENOENT);
184         }
185
186         dob = mdt_obj2dt(mo);
187         /* parse remote buffers to local buffers and prepare the latter */
188         *nr_local = 0;
189         for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
190                 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 1);
191                 if (unlikely(rc < 0))
192                         GOTO(err, rc);
193                 /* correct index for local buffers to continue with */
194                 for (k = 0; k < rc; k++)
195                         lnb[j+k].lnb_flags = rnb[i].rnb_flags;
196                 j += rc;
197                 *nr_local += rc;
198                 tot_bytes += rnb[i].rnb_len;
199         }
200
201         rc = dt_write_prep(env, dob, lnb, *nr_local);
202         if (likely(rc))
203                 GOTO(err, rc);
204
205         mdt_io_counter_incr(exp, LPROC_MDT_IO_WRITE, jobid, tot_bytes);
206         RETURN(0);
207 err:
208         dt_bufs_put(env, dob, lnb, *nr_local);
209 unlock:
210         mdt_dom_read_unlock(mo);
211         /* tgt_grant_prepare_write() was called, so we must commit */
212         mdt_grant_commit(exp, oa->o_grant_used, rc);
213         /* let's still process incoming grant information packed in the oa,
214          * but without enforcing grant since we won't proceed with the write.
215          * Just like a read request actually. */
216         mdt_grant_prepare_read(env, exp, oa);
217         return rc;
218 }
219
220 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
221                    struct obdo *oa, int objcount, struct obd_ioobj *obj,
222                    struct niobuf_remote *rnb, int *nr_local,
223                    struct niobuf_local *lnb)
224 {
225         struct tgt_session_info *tsi = tgt_ses_info(env);
226         struct mdt_thread_info *info = tsi2mdt_info(tsi);
227         struct lu_attr *la = &info->mti_attr.ma_attr;
228         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
229         struct mdt_object *mo;
230         char *jobid;
231         int rc = 0;
232
233         /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
234          * but for MDT it is different, correct it here. */
235         if (*nr_local > MD_MAX_BRW_PAGES)
236                 *nr_local = MD_MAX_BRW_PAGES;
237
238         jobid = tsi->tsi_jobid;
239
240         if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
241                 CERROR("%s: bad parameters %p/%i/%i\n",
242                        exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
243                 rc = -EPROTO;
244         }
245
246         mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
247         if (IS_ERR(mo))
248                 GOTO(out, rc = PTR_ERR(mo));
249
250         LASSERT(info->mti_object == NULL);
251         info->mti_object = mo;
252
253         if (cmd == OBD_BRW_WRITE) {
254                 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
255                 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
256                                       objcount, obj, rnb, nr_local, lnb,
257                                       jobid);
258         } else if (cmd == OBD_BRW_READ) {
259                 mdt_grant_prepare_read(env, exp, oa);
260                 rc = mdt_preprw_read(env, exp, mdt, mo, la,
261                                      obj->ioo_bufcnt, rnb, nr_local, lnb,
262                                      jobid);
263                 obdo_from_la(oa, la, LA_ATIME);
264         } else {
265                 CERROR("%s: wrong cmd %d received!\n",
266                        exp->exp_obd->obd_name, cmd);
267                 rc = -EPROTO;
268         }
269         if (rc) {
270                 lu_object_put(env, &mo->mot_obj);
271                 info->mti_object = NULL;
272         }
273 out:
274         RETURN(rc);
275 }
276
277 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
278                              struct mdt_object *mo, int objcount, int niocount,
279                              struct niobuf_local *lnb)
280 {
281         struct dt_object *dob;
282         int rc = 0;
283
284         ENTRY;
285
286         LASSERT(niocount > 0);
287
288         dob = mdt_obj2dt(mo);
289
290         dt_bufs_put(env, dob, lnb, niocount);
291
292         mdt_dom_read_unlock(mo);
293         RETURN(rc);
294 }
295
296 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
297                               struct mdt_device *mdt, struct mdt_object *mo,
298                               struct lu_attr *la, int objcount, int niocount,
299                               struct niobuf_local *lnb, unsigned long granted,
300                               int old_rc)
301 {
302         struct dt_device *dt = mdt->mdt_bottom;
303         struct dt_object *dob;
304         struct thandle *th;
305         int rc = 0;
306         int retries = 0;
307         int i;
308
309         ENTRY;
310
311         dob = mdt_obj2dt(mo);
312
313         if (old_rc)
314                 GOTO(out, rc = old_rc);
315
316         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
317 retry:
318         if (!dt_object_exists(dob))
319                 GOTO(out, rc = -ENOENT);
320
321         th = dt_trans_create(env, dt);
322         if (IS_ERR(th))
323                 GOTO(out, rc = PTR_ERR(th));
324
325         for (i = 0; i < niocount; i++) {
326                 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
327                         th->th_sync = 1;
328                         break;
329                 }
330         }
331
332         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
333                 GOTO(out_stop, rc = -EINPROGRESS);
334
335         rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
336         if (rc)
337                 GOTO(out_stop, rc);
338
339         if (la->la_valid) {
340                 /* update [mac]time if needed */
341                 rc = dt_declare_attr_set(env, dob, la, th);
342                 if (rc)
343                         GOTO(out_stop, rc);
344         }
345
346         rc = dt_trans_start(env, dt, th);
347         if (rc)
348                 GOTO(out_stop, rc);
349
350         dt_write_lock(env, dob, 0);
351         rc = dt_write_commit(env, dob, lnb, niocount, th);
352         if (rc)
353                 GOTO(unlock, rc);
354
355         if (la->la_valid) {
356                 rc = dt_attr_set(env, dob, la, th);
357                 if (rc)
358                         GOTO(unlock, rc);
359         }
360         /* get attr to return */
361         rc = dt_attr_get(env, dob, la);
362 unlock:
363         dt_write_unlock(env, dob);
364
365 out_stop:
366         /* Force commit to make the just-deleted blocks
367          * reusable. LU-456 */
368         if (rc == -ENOSPC)
369                 th->th_sync = 1;
370
371         th->th_result = rc;
372         dt_trans_stop(env, dt, th);
373         if (rc == -ENOSPC && retries++ < 3) {
374                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
375                        retries);
376                 goto retry;
377         }
378
379 out:
380         dt_bufs_put(env, dob, lnb, niocount);
381         mdt_dom_read_unlock(mo);
382         mdt_grant_commit(exp, granted, old_rc);
383         RETURN(rc);
384 }
385
386 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
387                             bool increase_only)
388 {
389         struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
390         struct ldlm_res_id resid;
391         struct ldlm_resource *res;
392
393         fid_build_reg_res_name(mdt_object_fid(mo), &resid);
394         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
395                                 LDLM_IBITS, 1);
396         if (IS_ERR(res))
397                 return;
398
399         /* Update lvbo data if exists. */
400         if (mdt_dom_lvb_is_valid(res))
401                 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
402         ldlm_resource_putref(res);
403 }
404
405 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
406                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
407                      struct niobuf_remote *rnb, int npages,
408                      struct niobuf_local *lnb, int old_rc)
409 {
410         struct mdt_thread_info *info = mdt_th_info(env);
411         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
412         struct mdt_object *mo = info->mti_object;
413         struct lu_attr *la = &info->mti_attr.ma_attr;
414         __u64 valid;
415         int rc = 0;
416
417         if (npages == 0) {
418                 CERROR("%s: no pages to commit\n",
419                        exp->exp_obd->obd_name);
420                 rc = -EPROTO;
421         }
422
423         LASSERT(mo);
424
425         if (cmd == OBD_BRW_WRITE) {
426                 /* Don't update timestamps if this write is older than a
427                  * setattr which modifies the timestamps. b=10150 */
428
429                 /* XXX when we start having persistent reservations this needs
430                  * to be changed to ofd_fmd_get() to create the fmd if it
431                  * doesn't already exist so we can store the reservation handle
432                  * there. */
433                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
434                 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
435
436                 la_from_obdo(la, oa, valid);
437
438                 rc = mdt_commitrw_write(env, exp, mdt, mo, la, objcount,
439                                         npages, lnb, oa->o_grant_used, old_rc);
440                 if (rc == 0)
441                         obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
442                 else
443                         obdo_from_la(oa, la, LA_GID | LA_UID);
444
445                 mdt_dom_obj_lvb_update(env, mo, false);
446                 /* don't report overquota flag if we failed before reaching
447                  * commit */
448                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
449                         /* return the overquota flags to client */
450                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
451                                 if (oa->o_valid & OBD_MD_FLFLAGS)
452                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
453                                 else
454                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
455                         }
456
457                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
458                                 if (oa->o_valid & OBD_MD_FLFLAGS)
459                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
460                                 else
461                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
462                         }
463
464                         oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
465                                        OBD_MD_FLGRPQUOTA;
466                 }
467         } else if (cmd == OBD_BRW_READ) {
468                 /* If oa != NULL then mdt_preprw_read updated the inode
469                  * atime and we should update the lvb so that other glimpses
470                  * will also get the updated value. bug 5972 */
471                 if (oa)
472                         mdt_dom_obj_lvb_update(env, mo, true);
473                 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
474                 if (old_rc)
475                         rc = old_rc;
476         } else {
477                 rc = -EPROTO;
478         }
479         /* this put is pair to object_get in ofd_preprw_write */
480         mdt_thread_info_fini(info);
481         RETURN(rc);
482 }
483
484 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
485                      struct dt_object *dob, __u64 start, __u64 end,
486                      struct lu_attr *la)
487 {
488         struct thandle *th;
489         int rc;
490
491         ENTRY;
492
493         /* we support truncate, not punch yet */
494         LASSERT(end == OBD_OBJECT_EOF);
495
496         if (!dt_object_exists(dob))
497                 RETURN(-ENOENT);
498
499         th = dt_trans_create(env, dt);
500         if (IS_ERR(th))
501                 RETURN(PTR_ERR(th));
502
503         rc = dt_declare_attr_set(env, dob, la, th);
504         if (rc)
505                 GOTO(stop, rc);
506
507         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
508         if (rc)
509                 GOTO(stop, rc);
510
511         tgt_vbr_obj_set(env, dob);
512         rc = dt_trans_start(env, dt, th);
513         if (rc)
514                 GOTO(stop, rc);
515
516         dt_write_lock(env, dob, 0);
517         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
518         if (rc)
519                 GOTO(unlock, rc);
520         rc = dt_attr_set(env, dob, la, th);
521         if (rc)
522                 GOTO(unlock, rc);
523 unlock:
524         dt_write_unlock(env, dob);
525 stop:
526         th->th_result = rc;
527         dt_trans_stop(env, dt, th);
528         RETURN(rc);
529 }
530
531 int mdt_punch_hdl(struct tgt_session_info *tsi)
532 {
533         const struct obdo *oa = &tsi->tsi_ost_body->oa;
534         struct ost_body *repbody;
535         struct mdt_thread_info *info;
536         struct lu_attr *la;
537         struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
538         struct obd_export *exp = tsi->tsi_exp;
539         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
540         struct mdt_object *mo;
541         struct dt_object *dob;
542         __u64 flags = 0;
543         struct lustre_handle lh = { 0, };
544         __u64 start, end;
545         int rc;
546         bool srvlock;
547
548         ENTRY;
549
550         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
551         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
552
553         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
554             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
555                 RETURN(err_serious(-EPROTO));
556
557         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
558         if (repbody == NULL)
559                 RETURN(err_serious(-ENOMEM));
560
561         /* punch start,end are passed in o_size,o_blocks throught wire */
562         start = oa->o_size;
563         end = oa->o_blocks;
564
565         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
566                 RETURN(-EPROTO);
567
568         info = tsi2mdt_info(tsi);
569         la = &info->mti_attr.ma_attr;
570         /* standard truncate optimization: if file body is completely
571          * destroyed, don't send data back to the server. */
572         if (start == 0)
573                 flags |= LDLM_FL_AST_DISCARD_DATA;
574
575         repbody->oa.o_oi = oa->o_oi;
576         repbody->oa.o_valid = OBD_MD_FLID;
577
578         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
579                   oa->o_valid & OBD_MD_FLFLAGS &&
580                   oa->o_flags & OBD_FL_SRVLOCK;
581
582         if (srvlock) {
583                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
584                                        &flags);
585                 if (rc != 0)
586                         GOTO(out, rc);
587         }
588
589         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
590                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
591                oa->o_valid, start, end);
592
593         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
594         if (IS_ERR(mo))
595                 GOTO(out_unlock, rc = PTR_ERR(mo));
596
597         mdt_dom_write_lock(mo);
598         if (!mdt_object_exists(mo))
599                 GOTO(out_put, rc = -ENOENT);
600         dob = mdt_obj2dt(mo);
601
602         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
603         la->la_size = start;
604         la->la_valid |= LA_SIZE;
605
606         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
607                               start, end, la);
608         mdt_dom_write_unlock(mo);
609         if (rc)
610                 GOTO(out_put, rc);
611
612         mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
613         mdt_io_counter_incr(tsi->tsi_exp, LPROC_MDT_IO_PUNCH,
614                             tsi->tsi_jobid, 1);
615         EXIT;
616 out_put:
617         lu_object_put(tsi->tsi_env, &mo->mot_obj);
618 out_unlock:
619         if (srvlock)
620                 mdt_save_lock(info, &lh, LCK_PW, rc);
621 out:
622         mdt_thread_info_fini(info);
623         return rc;
624 }
625
626 /**
627  * MDT glimpse for Data-on-MDT
628  *
629  * If there is write lock on client then function issues glimpse_ast to get
630  * an actual size from that client.
631  *
632  */
633 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
634                    struct ldlm_resource *res)
635 {
636         union ldlm_policy_data policy;
637         struct lustre_handle lockh;
638         enum ldlm_mode mode;
639         struct ldlm_lock *lock;
640         struct ldlm_glimpse_work *gl_work;
641         struct list_head gl_list;
642         int rc;
643
644         ENTRY;
645
646         /* There can be only one write lock covering data, try to match it. */
647         policy.l_inodebits.bits = MDS_INODELOCK_DOM;
648         mode = ldlm_lock_match(ns, LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK,
649                                &res->lr_name, LDLM_IBITS, &policy,
650                                LCK_PW, &lockh, 0);
651
652         /* There is no PW lock on this object; finished. */
653         if (mode == 0)
654                 RETURN(0);
655
656         lock = ldlm_handle2lock(&lockh);
657         if (lock == NULL)
658                 RETURN(0);
659
660         /*
661          * This check is for lock taken in mdt_reint_unlink() that does
662          * not have l_glimpse_ast set. So the logic is: if there is a lock
663          * with no l_glimpse_ast set, this object is being destroyed already.
664          * Hence, if you are grabbing DLM locks on the server, always set
665          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
666          */
667         if (lock->l_glimpse_ast == NULL) {
668                 LDLM_DEBUG(lock, "no l_glimpse_ast");
669                 GOTO(out, rc = -ENOENT);
670         }
671
672         OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
673         if (!gl_work)
674                 GOTO(out, rc = -ENOMEM);
675
676         /* Populate the gl_work structure.
677          * Grab additional reference on the lock which will be released in
678          * ldlm_work_gl_ast_lock() */
679         gl_work->gl_lock = LDLM_LOCK_GET(lock);
680         /* The glimpse callback is sent to one single IO lock. As a result,
681          * the gl_work list is just composed of one element */
682         INIT_LIST_HEAD(&gl_list);
683         list_add_tail(&gl_work->gl_list, &gl_list);
684         /* There is actually no need for a glimpse descriptor when glimpsing
685          * IO locks */
686         gl_work->gl_desc = NULL;
687         /* the ldlm_glimpse_work structure is allocated on the stack */
688         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
689
690         ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
691
692         /* If the list is not empty, we failed to glimpse a lock and
693          * must clean it up. Usually due to a race with unlink.*/
694         if (!list_empty(&gl_list)) {
695                 LDLM_LOCK_RELEASE(lock);
696                 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
697         }
698         rc = 0;
699         EXIT;
700 out:
701         LDLM_LOCK_PUT(lock);
702         return rc;
703 }
704
705 static void mdt_lvb2body(struct ldlm_resource *res, struct mdt_body *mb)
706 {
707         struct ost_lvb *res_lvb;
708
709         lock_res(res);
710         res_lvb = res->lr_lvb_data;
711         mb->mbo_size = res_lvb->lvb_size;
712         mb->mbo_blocks = res_lvb->lvb_blocks;
713         mb->mbo_mtime = res_lvb->lvb_mtime;
714         mb->mbo_ctime = res_lvb->lvb_ctime;
715         mb->mbo_atime = res_lvb->lvb_atime;
716
717         CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
718
719         mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME | OBD_MD_FLMTIME |
720                          OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
721         unlock_res(res);
722 }
723
724 /**
725  * MDT glimpse for Data-on-MDT
726  *
727  * This function is called when MDT get attributes for the DoM object.
728  * If there is write lock on client then function issues glimpse_ast to get
729  * an actual size from that client.
730  */
731 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
732                         const struct lu_fid *fid, struct mdt_body *mb,
733                         bool dom_lock)
734 {
735         struct ldlm_res_id resid;
736         struct ldlm_resource *res;
737         int rc = 0;
738
739         ENTRY;
740
741         fid_build_reg_res_name(fid, &resid);
742         res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
743                                 LDLM_IBITS, 1);
744         if (IS_ERR(res) || res->lr_lvb_data == NULL)
745                 RETURN(-ENOENT);
746
747         /* if there is no DOM bit in the lock then glimpse is needed
748          * to return valid size */
749         if (!dom_lock) {
750                 rc = mdt_do_glimpse(env, mdt->mdt_namespace, res);
751                 if (rc < 0)
752                         GOTO(out, rc);
753         }
754
755         /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
756         if (dom_lock || !mdt_dom_lvb_is_valid(res))
757                 mdt_dom_lvbo_update(res, NULL, NULL, false);
758
759         mdt_lvb2body(res, mb);
760 out:
761         ldlm_resource_putref(res);
762         RETURN(rc);
763 }
764
765 /**
766  * MDT DoM lock intent policy (glimpse)
767  *
768  * Intent policy is called when lock has an intent, for DoM file that
769  * means glimpse lock and policy fills Lock Value Block (LVB).
770  *
771  * If already granted lock is found it will be placed in \a lockp and
772  * returned back to caller function.
773  *
774  * \param[in] tsi        session info
775  * \param[in,out] lockp  pointer to the lock
776  * \param[in] flags      LDLM flags
777  *
778  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
779  *                      and placed in \a lockp
780  * \retval              ELDLM_LOCK_ABORTED in other cases except error
781  * \retval              negative value on error
782  */
783 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
784                         struct ldlm_lock **lockp, __u64 flags)
785 {
786         struct ldlm_lock *lock = *lockp;
787         struct ldlm_resource *res = lock->l_resource;
788         ldlm_processing_policy policy;
789         struct ldlm_reply *rep;
790         struct mdt_body *mbo;
791         int rc;
792
793         ENTRY;
794
795         policy = ldlm_get_processing_policy(res);
796         LASSERT(policy != NULL);
797
798         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
799         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
800         rc = req_capsule_server_pack(mti->mti_pill);
801         if (rc)
802                 RETURN(err_serious(rc));
803
804         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
805         if (rep == NULL)
806                 RETURN(-EPROTO);
807
808         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
809         if (mbo == NULL)
810                 RETURN(-EPROTO);
811
812         lock_res(res);
813         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
814          * lock was found by ldlm_handle_enqueue(); if so no need to grant
815          * it again. */
816         if (flags & LDLM_FL_RESENT) {
817                 rc = LDLM_ITER_CONTINUE;
818         } else {
819                 __u64 tmpflags = 0;
820                 enum ldlm_error err;
821
822                 rc = policy(lock, &tmpflags, 0, &err, NULL);
823                 check_res_locked(res);
824         }
825         unlock_res(res);
826
827         /* The lock met with no resistance; we're finished. */
828         if (rc == LDLM_ITER_CONTINUE) {
829                 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
830         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
831                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
832                  * callback for glimpse size. The real size user will trigger
833                  * the glimpse callback when necessary. */
834                 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
835         }
836
837         rc = mdt_do_glimpse(mti->mti_env, ns, res);
838         if (rc == -ENOENT) {
839                 /* We are racing with unlink(); just return -ENOENT */
840                 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
841                 rc = 0;
842         } else if (rc == -EINVAL) {
843                 /* this is possible is client lock has been cancelled but
844                  * still exists on server. If that lock was found on server
845                  * as only conflicting lock then the client has already
846                  * size authority and glimpse is not needed. */
847                 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
848                 rc = 0;
849         } else if (rc < 0) {
850                 RETURN(rc);
851         }
852         rc = ELDLM_LOCK_ABORTED;
853 fill_mbo:
854         /* LVB can be without valid data in case of DOM */
855         if (!mdt_dom_lvb_is_valid(res))
856                 mdt_dom_lvbo_update(res, lock, NULL, false);
857         mdt_lvb2body(res, mbo);
858         RETURN(rc);
859 }
860
861 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
862                     struct ldlm_lock **lockp, __u64 flags)
863 {
864         struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
865         struct lu_fid *fid = &tsi->tsi_fid;
866         struct ldlm_lock *lock = *lockp;
867         struct ldlm_resource *res = lock->l_resource;
868         struct ldlm_reply *rep;
869         struct mdt_body *mbo;
870         struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
871         struct mdt_object *mo;
872         int rc = 0;
873
874         ENTRY;
875
876         /* Get lock from request for possible resent case. */
877         mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
878         req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
879         req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
880         rc = req_capsule_server_pack(mti->mti_pill);
881         if (rc)
882                 RETURN(err_serious(rc));
883
884         rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
885         if (rep == NULL)
886                 RETURN(-EPROTO);
887
888         mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
889         if (mbo == NULL)
890                 RETURN(-EPROTO);
891
892         fid_extract_from_res_name(fid, &res->lr_name);
893         mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
894         if (unlikely(IS_ERR(mo)))
895                 RETURN(PTR_ERR(mo));
896
897         if (!mdt_object_exists(mo))
898                 GOTO(out, rc = -ENOENT);
899
900         if (mdt_object_remote(mo))
901                 GOTO(out, rc = -EPROTO);
902
903         /* resent case */
904         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
905                 mdt_lock_handle_init(lhc);
906                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
907                 /* This will block MDT thread but it should be fine until
908                  * client caches small amount of data for DoM, which should be
909                  * smaller than one BRW RPC and should be able to be
910                  * piggybacked by lock cancel RPC.
911                  * If the client could hold the lock too long, this code can be
912                  * revised to call mdt_object_lock_try(). And if fails, it will
913                  * return ELDLM_OK here and fall back into normal lock enqueue
914                  * process.
915                  */
916                 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
917                 if (rc)
918                         GOTO(out, rc);
919         }
920
921         if (!mdt_dom_lvb_is_valid(res)) {
922                 rc = mdt_dom_lvb_alloc(res);
923                 if (rc)
924                         GOTO(out_fail, rc);
925                 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
926         }
927         mdt_lvb2body(res, mbo);
928 out_fail:
929         rep->lock_policy_res2 = clear_serious(rc);
930         if (rep->lock_policy_res2) {
931                 lhc->mlh_reg_lh.cookie = 0ull;
932                 GOTO(out, rc = ELDLM_LOCK_ABORTED);
933         }
934
935         rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
936 out:
937         mdt_object_put(mti->mti_env, mo);
938         RETURN(rc);
939 }
940
941 void mdt_dom_discard_data(struct mdt_thread_info *info,
942                           const struct lu_fid *fid)
943 {
944         struct mdt_device *mdt = info->mti_mdt;
945         union ldlm_policy_data *policy = &info->mti_policy;
946         struct ldlm_res_id *res_id = &info->mti_res_id;
947         struct lustre_handle dom_lh;
948         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
949         __u64 rc = 0;
950
951         policy->l_inodebits.bits = MDS_INODELOCK_DOM;
952         policy->l_inodebits.try_bits = 0;
953         fid_build_reg_res_name(fid, res_id);
954
955         /* Tell the clients that the object is gone now and that they should
956          * throw away any cached pages. */
957         rc = ldlm_cli_enqueue_local(mdt->mdt_namespace, res_id, LDLM_IBITS,
958                                     policy, LCK_PW, &flags, ldlm_blocking_ast,
959                                     ldlm_completion_ast, NULL, NULL, 0,
960                                     LVB_T_NONE, NULL, &dom_lh);
961
962         /* We only care about the side-effects, just drop the lock. */
963         if (rc == ELDLM_OK)
964                 ldlm_lock_decref(&dom_lh, LCK_PW);
965 }
966