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