Whamcloud - gitweb
954713eb608831caa56fa104c61f8c7b439d8b6a
[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 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
387                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
388                      struct niobuf_remote *rnb, int npages,
389                      struct niobuf_local *lnb, int old_rc)
390 {
391         struct mdt_thread_info *info = mdt_th_info(env);
392         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
393         struct mdt_object *mo = info->mti_object;
394         struct lu_attr *la = &info->mti_attr.ma_attr;
395         __u64 valid;
396         int rc = 0;
397
398         if (npages == 0) {
399                 CERROR("%s: no pages to commit\n",
400                        exp->exp_obd->obd_name);
401                 rc = -EPROTO;
402         }
403
404         LASSERT(mo);
405
406         if (cmd == OBD_BRW_WRITE) {
407                 /* Don't update timestamps if this write is older than a
408                  * setattr which modifies the timestamps. b=10150 */
409
410                 /* XXX when we start having persistent reservations this needs
411                  * to be changed to ofd_fmd_get() to create the fmd if it
412                  * doesn't already exist so we can store the reservation handle
413                  * there. */
414                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
415                 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
416
417                 la_from_obdo(la, oa, valid);
418
419                 rc = mdt_commitrw_write(env, exp, mdt, mo, la, objcount,
420                                         npages, lnb, oa->o_grant_used, old_rc);
421                 if (rc == 0)
422                         obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
423                 else
424                         obdo_from_la(oa, la, LA_GID | LA_UID);
425
426                 /* don't report overquota flag if we failed before reaching
427                  * commit */
428                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
429                         /* return the overquota flags to client */
430                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
431                                 if (oa->o_valid & OBD_MD_FLFLAGS)
432                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
433                                 else
434                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
435                         }
436
437                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
438                                 if (oa->o_valid & OBD_MD_FLFLAGS)
439                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
440                                 else
441                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
442                         }
443
444                         oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
445                                        OBD_MD_FLGRPQUOTA;
446                 }
447         } else if (cmd == OBD_BRW_READ) {
448                 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
449                 if (old_rc)
450                         rc = old_rc;
451         } else {
452                 rc = -EPROTO;
453         }
454         /* this put is pair to object_get in ofd_preprw_write */
455         mdt_thread_info_fini(info);
456         RETURN(rc);
457 }
458
459 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
460                      struct dt_object *dob, __u64 start, __u64 end,
461                      struct lu_attr *la)
462 {
463         struct thandle *th;
464         int rc;
465
466         ENTRY;
467
468         /* we support truncate, not punch yet */
469         LASSERT(end == OBD_OBJECT_EOF);
470
471         if (!dt_object_exists(dob))
472                 RETURN(-ENOENT);
473
474         th = dt_trans_create(env, dt);
475         if (IS_ERR(th))
476                 RETURN(PTR_ERR(th));
477
478         rc = dt_declare_attr_set(env, dob, la, th);
479         if (rc)
480                 GOTO(stop, rc);
481
482         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
483         if (rc)
484                 GOTO(stop, rc);
485
486         tgt_vbr_obj_set(env, dob);
487         rc = dt_trans_start(env, dt, th);
488         if (rc)
489                 GOTO(stop, rc);
490
491         dt_write_lock(env, dob, 0);
492         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
493         if (rc)
494                 GOTO(unlock, rc);
495         rc = dt_attr_set(env, dob, la, th);
496         if (rc)
497                 GOTO(unlock, rc);
498 unlock:
499         dt_write_unlock(env, dob);
500 stop:
501         th->th_result = rc;
502         dt_trans_stop(env, dt, th);
503         RETURN(rc);
504 }
505
506 int mdt_punch_hdl(struct tgt_session_info *tsi)
507 {
508         const struct obdo *oa = &tsi->tsi_ost_body->oa;
509         struct ost_body *repbody;
510         struct mdt_thread_info *info;
511         struct lu_attr *la;
512         struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
513         struct obd_export *exp = tsi->tsi_exp;
514         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
515         struct mdt_object *mo;
516         struct dt_object *dob;
517         __u64 flags = 0;
518         struct lustre_handle lh = { 0, };
519         __u64 start, end;
520         int rc;
521         bool srvlock;
522
523         ENTRY;
524
525         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
526         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
527
528         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
529             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
530                 RETURN(err_serious(-EPROTO));
531
532         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
533         if (repbody == NULL)
534                 RETURN(err_serious(-ENOMEM));
535
536         /* punch start,end are passed in o_size,o_blocks throught wire */
537         start = oa->o_size;
538         end = oa->o_blocks;
539
540         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
541                 RETURN(-EPROTO);
542
543         info = tsi2mdt_info(tsi);
544         la = &info->mti_attr.ma_attr;
545         /* standard truncate optimization: if file body is completely
546          * destroyed, don't send data back to the server. */
547         if (start == 0)
548                 flags |= LDLM_FL_AST_DISCARD_DATA;
549
550         repbody->oa.o_oi = oa->o_oi;
551         repbody->oa.o_valid = OBD_MD_FLID;
552
553         srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
554                   oa->o_valid & OBD_MD_FLFLAGS &&
555                   oa->o_flags & OBD_FL_SRVLOCK;
556
557         if (srvlock) {
558                 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
559                                        &flags);
560                 if (rc != 0)
561                         GOTO(out, rc);
562         }
563
564         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
565                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
566                oa->o_valid, start, end);
567
568         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
569         if (IS_ERR(mo))
570                 GOTO(out_unlock, rc = PTR_ERR(mo));
571
572         mdt_dom_write_lock(mo);
573         if (!mdt_object_exists(mo))
574                 GOTO(out_put, rc = -ENOENT);
575         dob = mdt_obj2dt(mo);
576
577         la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
578         la->la_size = start;
579         la->la_valid |= LA_SIZE;
580
581         rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
582                               start, end, la);
583         mdt_dom_write_unlock(mo);
584         if (rc)
585                 GOTO(out_put, rc);
586
587         mdt_io_counter_incr(tsi->tsi_exp, LPROC_MDT_IO_PUNCH,
588                             tsi->tsi_jobid, 1);
589         EXIT;
590 out_put:
591         lu_object_put(tsi->tsi_env, &mo->mot_obj);
592 out_unlock:
593         if (srvlock)
594                 mdt_save_lock(info, &lh, LCK_PW, rc);
595 out:
596         mdt_thread_info_fini(info);
597         if (rc == 0) {
598                 struct ldlm_resource *res;
599
600                 /* we do not call this before to avoid lu_object_find() in
601                  *  ->lvbo_update() holding another reference on the object.
602                  * otherwise concurrent destroy can make the object unavailable
603                  * for 2nd lu_object_find() waiting for the first reference
604                  * to go... deadlock! */
605                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
606                                         LDLM_IBITS, 0);
607                 if (!IS_ERR(res)) {
608                         ldlm_res_lvbo_update(res, NULL, 0);
609                         ldlm_resource_putref(res);
610                 }
611         }
612         return rc;
613 }
614