Whamcloud - gitweb
LU-4609 ofd: auto resume LFSCK after the recovery
[fs/lustre-release.git] / lustre / ofd / ofd_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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ofd/ofd_io.c
37  *
38  * Author: Alex Tomas <bzzz@whamcloud.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include "ofd_internal.h"
44
45 struct ofd_inconsistency_item {
46         struct list_head         oii_list;
47         struct ofd_object       *oii_obj;
48         struct lu_fid            oii_pfid;
49 };
50
51 static void ofd_inconsistency_verify_one(const struct lu_env *env,
52                                          struct ofd_device *ofd,
53                                          struct ofd_inconsistency_item *oii,
54                                          struct lfsck_request *lr)
55 {
56         struct ofd_object       *fo     = oii->oii_obj;
57         struct lu_fid           *pfid   = &fo->ofo_pfid;
58         int                      rc;
59
60         LASSERT(fo->ofo_pfid_checking);
61         LASSERT(!fo->ofo_pfid_verified);
62
63         lr->lr_fid = fo->ofo_header.loh_fid; /* OST-object itself FID. */
64         lr->lr_fid2 = oii->oii_pfid; /* client given PFID. */
65         lr->lr_fid3 = *pfid; /* OST local stored PFID. */
66
67         rc = lfsck_in_notify(env, ofd->ofd_osd, lr);
68         ofd_write_lock(env, fo);
69         switch (lr->lr_status) {
70         case LPVS_INIT:
71                 LASSERT(rc <= 0);
72
73                 if (rc < 0)
74                         CDEBUG(D_LFSCK, "%s: fail to verify OST local stored "
75                                "PFID xattr for "DFID", the client given PFID "
76                                DFID", OST local stored PFID "DFID": rc = %d\n",
77                                ofd_obd(ofd)->obd_name,
78                                PFID(&fo->ofo_header.loh_fid),
79                                PFID(&oii->oii_pfid), PFID(pfid), rc);
80                 else
81                         fo->ofo_pfid_verified = 1;
82                 break;
83         case LPVS_INCONSISTENT:
84                 LASSERT(rc != 0);
85
86                 ofd->ofd_inconsistency_self_detected++;
87                 if (rc < 0)
88                         CDEBUG(D_LFSCK, "%s: fail to verify the client given "
89                                "PFID for "DFID", the client given PFID "DFID
90                                ", local stored PFID "DFID": rc = %d\n",
91                                ofd_obd(ofd)->obd_name,
92                                PFID(&fo->ofo_header.loh_fid),
93                                PFID(&oii->oii_pfid), PFID(pfid), rc);
94                 else
95                         CDEBUG(D_LFSCK, "%s: both the client given PFID and "
96                                "the OST local stored PFID are stale for the "
97                                "OST-object "DFID", client given PFID is "DFID
98                                ", local stored PFID is "DFID"\n",
99                                ofd_obd(ofd)->obd_name,
100                                PFID(&fo->ofo_header.loh_fid),
101                                PFID(&oii->oii_pfid), PFID(pfid));
102                 break;
103         case LPVS_INCONSISTENT_TOFIX:
104                 ofd->ofd_inconsistency_self_detected++;
105                 if (rc == 0) {
106                         ofd->ofd_inconsistency_self_repaired++;
107                         CDEBUG(D_LFSCK, "%s: fixed the staled OST PFID xattr "
108                                "for "DFID", with the client given PFID "DFID
109                                ", the old stored PFID "DFID"\n",
110                                ofd_obd(ofd)->obd_name,
111                                PFID(&fo->ofo_header.loh_fid),
112                                PFID(&oii->oii_pfid), PFID(pfid));
113                 } else {
114                         CDEBUG(D_LFSCK, "%s: fail to fix the OST PFID xattr "
115                                "for "DFID", client given PFID "DFID", local "
116                                "stored PFID "DFID": rc = %d\n",
117                                ofd_obd(ofd)->obd_name,
118                                PFID(&fo->ofo_header.loh_fid),
119                                PFID(&oii->oii_pfid), PFID(pfid), rc);
120                 }
121                 *pfid = oii->oii_pfid;
122                 fo->ofo_pfid_verified = 1;
123                 break;
124         default:
125                 break;
126         }
127         fo->ofo_pfid_checking = 0;
128         ofd_write_unlock(env, fo);
129
130         lu_object_put(env, &fo->ofo_obj.do_lu);
131         OBD_FREE_PTR(oii);
132 }
133
134 static int ofd_inconsistency_verification_main(void *args)
135 {
136         struct lu_env                  env;
137         struct ofd_device             *ofd    = args;
138         struct ptlrpc_thread          *thread = &ofd->ofd_inconsistency_thread;
139         struct ofd_inconsistency_item *oii;
140         struct lfsck_request          *lr     = NULL;
141         struct l_wait_info             lwi    = { 0 };
142         int                            rc;
143         ENTRY;
144
145         rc = lu_env_init(&env, LCT_DT_THREAD);
146         spin_lock(&ofd->ofd_inconsistency_lock);
147         thread_set_flags(thread, rc != 0 ? SVC_STOPPED : SVC_RUNNING);
148         wake_up_all(&thread->t_ctl_waitq);
149         spin_unlock(&ofd->ofd_inconsistency_lock);
150         if (rc != 0)
151                 RETURN(rc);
152
153         OBD_ALLOC_PTR(lr);
154         if (unlikely(lr == NULL))
155                 GOTO(out, rc = -ENOMEM);
156
157         lr->lr_event = LE_PAIRS_VERIFY;
158         lr->lr_active = LT_LAYOUT;
159
160         spin_lock(&ofd->ofd_inconsistency_lock);
161         while (1) {
162                 if (unlikely(!thread_is_running(thread)))
163                         break;
164
165                 while (!list_empty(&ofd->ofd_inconsistency_list)) {
166                         oii = list_entry(ofd->ofd_inconsistency_list.next,
167                                          struct ofd_inconsistency_item,
168                                          oii_list);
169                         list_del_init(&oii->oii_list);
170                         spin_unlock(&ofd->ofd_inconsistency_lock);
171                         ofd_inconsistency_verify_one(&env, ofd, oii, lr);
172                         spin_lock(&ofd->ofd_inconsistency_lock);
173                 }
174
175                 spin_unlock(&ofd->ofd_inconsistency_lock);
176                 l_wait_event(thread->t_ctl_waitq,
177                              !list_empty(&ofd->ofd_inconsistency_list) ||
178                              !thread_is_running(thread),
179                              &lwi);
180                 spin_lock(&ofd->ofd_inconsistency_lock);
181         }
182
183         while (!list_empty(&ofd->ofd_inconsistency_list)) {
184                 struct ofd_object *fo;
185
186                 oii = list_entry(ofd->ofd_inconsistency_list.next,
187                                  struct ofd_inconsistency_item,
188                                  oii_list);
189                 list_del_init(&oii->oii_list);
190                 fo = oii->oii_obj;
191                 spin_unlock(&ofd->ofd_inconsistency_lock);
192
193                 ofd_write_lock(&env, fo);
194                 fo->ofo_pfid_checking = 0;
195                 ofd_write_unlock(&env, fo);
196
197                 lu_object_put(&env, &fo->ofo_obj.do_lu);
198                 OBD_FREE_PTR(oii);
199                 spin_lock(&ofd->ofd_inconsistency_lock);
200         }
201
202         OBD_FREE_PTR(lr);
203
204         GOTO(out, rc = 0);
205
206 out:
207         thread_set_flags(thread, SVC_STOPPED);
208         wake_up_all(&thread->t_ctl_waitq);
209         spin_unlock(&ofd->ofd_inconsistency_lock);
210         lu_env_fini(&env);
211
212         return rc;
213 }
214
215 int ofd_start_inconsistency_verification_thread(struct ofd_device *ofd)
216 {
217         struct ptlrpc_thread    *thread = &ofd->ofd_inconsistency_thread;
218         struct l_wait_info       lwi    = { 0 };
219         long                     rc;
220
221         spin_lock(&ofd->ofd_inconsistency_lock);
222         if (unlikely(thread_is_running(thread))) {
223                 spin_unlock(&ofd->ofd_inconsistency_lock);
224
225                 return -EALREADY;
226         }
227
228         thread_set_flags(thread, 0);
229         spin_unlock(&ofd->ofd_inconsistency_lock);
230         rc = PTR_ERR(kthread_run(ofd_inconsistency_verification_main, ofd,
231                                  "inconsistency_verification"));
232         if (IS_ERR_VALUE(rc)) {
233                 CERROR("%s: cannot start self_repair thread: rc = %ld\n",
234                        ofd_obd(ofd)->obd_name, rc);
235         } else {
236                 rc = 0;
237                 l_wait_event(thread->t_ctl_waitq,
238                              thread_is_running(thread) ||
239                              thread_is_stopped(thread),
240                              &lwi);
241         }
242
243         return rc;
244 }
245
246 int ofd_stop_inconsistency_verification_thread(struct ofd_device *ofd)
247 {
248         struct ptlrpc_thread    *thread = &ofd->ofd_inconsistency_thread;
249         struct l_wait_info       lwi    = { 0 };
250
251         spin_lock(&ofd->ofd_inconsistency_lock);
252         if (thread_is_init(thread) || thread_is_stopped(thread)) {
253                 spin_unlock(&ofd->ofd_inconsistency_lock);
254
255                 return -EALREADY;
256         }
257
258         thread_set_flags(thread, SVC_STOPPING);
259         spin_unlock(&ofd->ofd_inconsistency_lock);
260         wake_up_all(&thread->t_ctl_waitq);
261         l_wait_event(thread->t_ctl_waitq,
262                      thread_is_stopped(thread),
263                      &lwi);
264
265         return 0;
266 }
267
268 static void ofd_add_inconsistency_item(const struct lu_env *env,
269                                        struct ofd_object *fo, struct obdo *oa)
270 {
271         struct ofd_device               *ofd    = ofd_obj2dev(fo);
272         struct ofd_inconsistency_item   *oii;
273         bool                             wakeup = false;
274
275         OBD_ALLOC_PTR(oii);
276         if (oii == NULL) {
277                 CERROR("%s: cannot alloc memory for verify OST-object "
278                        "consistency for "DFID", client given PFID "DFID
279                        ", local stored PFID "DFID"\n",
280                        ofd_obd(ofd)->obd_name, PFID(&fo->ofo_header.loh_fid),
281                        oa->o_parent_seq, oa->o_parent_oid, oa->o_stripe_idx,
282                        PFID(&fo->ofo_pfid));
283
284                 return;
285         }
286
287         INIT_LIST_HEAD(&oii->oii_list);
288         lu_object_get(&fo->ofo_obj.do_lu);
289         oii->oii_obj = fo;
290         oii->oii_pfid.f_seq = oa->o_parent_seq;
291         oii->oii_pfid.f_oid = oa->o_parent_oid;
292         oii->oii_pfid.f_stripe_idx = oa->o_stripe_idx;
293
294         spin_lock(&ofd->ofd_inconsistency_lock);
295         if (fo->ofo_pfid_checking || fo->ofo_pfid_verified) {
296                 spin_unlock(&ofd->ofd_inconsistency_lock);
297                 OBD_FREE_PTR(oii);
298
299                 return;
300         }
301
302         fo->ofo_pfid_checking = 1;
303         if (list_empty(&ofd->ofd_inconsistency_list))
304                 wakeup = true;
305         list_add_tail(&oii->oii_list, &ofd->ofd_inconsistency_list);
306         spin_unlock(&ofd->ofd_inconsistency_lock);
307         if (wakeup)
308                 wake_up_all(&ofd->ofd_inconsistency_thread.t_ctl_waitq);
309
310         /* XXX: When the found inconsistency exceeds some threshold,
311          *      we can trigger the LFSCK to scan part of the system
312          *      or the whole system, which depends on how to define
313          *      the threshold, a simple way maybe like that: define
314          *      the absolute value of how many inconsisteny allowed
315          *      to be repaired via self detect/repair mechanism, if
316          *      exceeded, then trigger the LFSCK to scan the layout
317          *      inconsistency within the whole system. */
318 }
319
320 int ofd_verify_ff(const struct lu_env *env, struct ofd_object *fo,
321                   struct obdo *oa)
322 {
323         struct lu_fid   *pfid   = &fo->ofo_pfid;
324         int              rc     = 0;
325         ENTRY;
326
327         if (fid_is_sane(pfid)) {
328                 if (likely(oa->o_parent_seq == pfid->f_seq &&
329                            oa->o_parent_oid == pfid->f_oid &&
330                            oa->o_stripe_idx == pfid->f_stripe_idx))
331                         RETURN(0);
332
333                 if (fo->ofo_pfid_verified)
334                         RETURN(-EPERM);
335         }
336
337         /* The OST-object may be inconsistent, and we need further verification.
338          * To avoid block the RPC service thread, return -EINPROGRESS to client
339          * and make it retry later. */
340         if (fo->ofo_pfid_checking)
341                 RETURN(-EINPROGRESS);
342
343         rc = ofd_object_ff_load(env, fo);
344         if (rc == -ENODATA)
345                 RETURN(0);
346
347         if (rc < 0)
348                 RETURN(rc);
349
350         if (likely(oa->o_parent_seq == pfid->f_seq &&
351                    oa->o_parent_oid == pfid->f_oid &&
352                    oa->o_stripe_idx == pfid->f_stripe_idx))
353                 RETURN(0);
354
355         /* Push it to the dedicated thread for further verification. */
356         ofd_add_inconsistency_item(env, fo, oa);
357
358         RETURN(-EINPROGRESS);
359 }
360
361 static int ofd_preprw_read(const struct lu_env *env, struct obd_export *exp,
362                            struct ofd_device *ofd, const struct lu_fid *fid,
363                            struct lu_attr *la, struct obdo *oa, int niocount,
364                            struct niobuf_remote *rnb, int *nr_local,
365                            struct niobuf_local *lnb, char *jobid)
366 {
367         struct ofd_object       *fo;
368         int                      i, j, rc, tot_bytes = 0;
369
370         ENTRY;
371         LASSERT(env != NULL);
372
373         fo = ofd_object_find(env, ofd, fid);
374         if (IS_ERR(fo))
375                 RETURN(PTR_ERR(fo));
376         LASSERT(fo != NULL);
377
378         ofd_read_lock(env, fo);
379         if (!ofd_object_exists(fo))
380                 GOTO(unlock, rc = -ENOENT);
381
382         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
383                 rc = ofd_verify_ff(env, fo, oa);
384                 if (rc != 0)
385                         GOTO(unlock, rc);
386         }
387
388         /* parse remote buffers to local buffers and prepare the latter */
389         *nr_local = 0;
390         for (i = 0, j = 0; i < niocount; i++) {
391                 rc = dt_bufs_get(env, ofd_object_child(fo), rnb + i,
392                                  lnb + j, 0, ofd_object_capa(env, fo));
393                 if (unlikely(rc < 0))
394                         GOTO(buf_put, rc);
395                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
396                 /* correct index for local buffers to continue with */
397                 j += rc;
398                 *nr_local += rc;
399                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
400                 tot_bytes += rnb[i].rnb_len;
401         }
402
403         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
404         rc = dt_attr_get(env, ofd_object_child(fo), la,
405                          ofd_object_capa(env, fo));
406         if (unlikely(rc))
407                 GOTO(buf_put, rc);
408
409         rc = dt_read_prep(env, ofd_object_child(fo), lnb, *nr_local);
410         if (unlikely(rc))
411                 GOTO(buf_put, rc);
412
413         ofd_counter_incr(exp, LPROC_OFD_STATS_READ, jobid, tot_bytes);
414         RETURN(0);
415
416 buf_put:
417         dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
418 unlock:
419         ofd_read_unlock(env, fo);
420         ofd_object_put(env, fo);
421         return rc;
422 }
423
424 static int ofd_preprw_write(const struct lu_env *env, struct obd_export *exp,
425                             struct ofd_device *ofd, const struct lu_fid *fid,
426                             struct lu_attr *la, struct obdo *oa,
427                             int objcount, struct obd_ioobj *obj,
428                             struct niobuf_remote *rnb, int *nr_local,
429                             struct niobuf_local *lnb, char *jobid)
430 {
431         struct ofd_object       *fo;
432         int                      i, j, k, rc = 0, tot_bytes = 0;
433
434         ENTRY;
435         LASSERT(env != NULL);
436         LASSERT(objcount == 1);
437
438         if (unlikely(exp->exp_obd->obd_recovering)) {
439                 obd_seq seq = fid_seq(fid);
440                 obd_id  oid = fid_oid(fid);
441                 struct ofd_seq *oseq;
442
443                 oseq = ofd_seq_load(env, ofd, seq);
444                 if (IS_ERR(oseq)) {
445                         CERROR("%s: Can't find FID Sequence "LPX64": rc = %d\n",
446                                ofd_name(ofd), seq, (int)PTR_ERR(oseq));
447                         GOTO(out, rc = -EINVAL);
448                 }
449
450                 if (oid > ofd_seq_last_oid(oseq)) {
451                         int sync = 0;
452                         int diff;
453
454                         mutex_lock(&oseq->os_create_lock);
455                         diff = oid - ofd_seq_last_oid(oseq);
456
457                         /* Do sync create if the seq is about to used up */
458                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
459                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
460                                         sync = 1;
461                         } else if (fid_seq_is_norm(seq)) {
462                                 if (unlikely(oid >=
463                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
464                                         sync = 1;
465                         } else {
466                                 CERROR("%s : invalid o_seq "DOSTID"\n",
467                                        ofd_name(ofd), POSTID(&oa->o_oi));
468                                 mutex_unlock(&oseq->os_create_lock);
469                                 ofd_seq_put(env, oseq);
470                                 GOTO(out, rc = -EINVAL);
471                         }
472
473                         while (diff > 0) {
474                                 obd_id next_id = ofd_seq_last_oid(oseq) + 1;
475                                 int count = ofd_precreate_batch(ofd, diff);
476
477                                 rc = ofd_precreate_objects(env, ofd, next_id,
478                                                            oseq, count, sync);
479                                 if (rc < 0) {
480                                         mutex_unlock(&oseq->os_create_lock);
481                                         ofd_seq_put(env, oseq);
482                                         GOTO(out, rc);
483                                 }
484
485                                 diff -= rc;
486                         }
487
488                         mutex_unlock(&oseq->os_create_lock);
489                 }
490
491                 ofd_seq_put(env, oseq);
492         }
493
494         fo = ofd_object_find(env, ofd, fid);
495         if (IS_ERR(fo))
496                 GOTO(out, rc = PTR_ERR(fo));
497         LASSERT(fo != NULL);
498
499         ofd_read_lock(env, fo);
500         if (!ofd_object_exists(fo)) {
501                 CERROR("%s: BRW to missing obj "DOSTID"\n",
502                        exp->exp_obd->obd_name, POSTID(&obj->ioo_oid));
503                 ofd_read_unlock(env, fo);
504                 ofd_object_put(env, fo);
505                 GOTO(out, rc = -ENOENT);
506         }
507
508         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
509                 rc = ofd_verify_ff(env, fo, oa);
510                 if (rc != 0) {
511                         ofd_read_unlock(env, fo);
512                         ofd_object_put(env, fo);
513                         GOTO(out, rc);
514                 }
515         }
516
517         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
518          * space back if possible */
519         ofd_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
520
521         /* parse remote buffers to local buffers and prepare the latter */
522         *nr_local = 0;
523         for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
524                 rc = dt_bufs_get(env, ofd_object_child(fo),
525                                  rnb + i, lnb + j, 1,
526                                  ofd_object_capa(env, fo));
527                 if (unlikely(rc < 0))
528                         GOTO(err, rc);
529                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
530                 /* correct index for local buffers to continue with */
531                 for (k = 0; k < rc; k++) {
532                         lnb[j+k].lnb_flags = rnb[i].rnb_flags;
533                         if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
534                                 lnb[j+k].lnb_rc = -ENOSPC;
535
536                         /* remote client can't break through quota */
537                         if (exp_connect_rmtclient(exp))
538                                 lnb[j+k].lnb_flags &= ~OBD_BRW_NOQUOTA;
539                 }
540                 j += rc;
541                 *nr_local += rc;
542                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
543                 tot_bytes += rnb[i].rnb_len;
544         }
545         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
546
547         rc = dt_write_prep(env, ofd_object_child(fo), lnb, *nr_local);
548         if (unlikely(rc != 0))
549                 GOTO(err, rc);
550
551         ofd_counter_incr(exp, LPROC_OFD_STATS_WRITE, jobid, tot_bytes);
552         RETURN(0);
553 err:
554         dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
555         ofd_read_unlock(env, fo);
556         /* ofd_grant_prepare_write() was called, so we must commit */
557         ofd_grant_commit(env, exp, rc);
558 out:
559         /* let's still process incoming grant information packed in the oa,
560          * but without enforcing grant since we won't proceed with the write.
561          * Just like a read request actually. */
562         ofd_grant_prepare_read(env, exp, oa);
563         return rc;
564 }
565
566 int ofd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
567                struct obdo *oa, int objcount, struct obd_ioobj *obj,
568                struct niobuf_remote *rnb, int *nr_local,
569                struct niobuf_local *lnb, struct obd_trans_info *oti,
570                struct lustre_capa *capa)
571 {
572         struct tgt_session_info *tsi = tgt_ses_info(env);
573         struct ofd_device       *ofd = ofd_exp(exp);
574         struct ofd_thread_info  *info;
575         char                    *jobid;
576         const struct lu_fid     *fid = &oa->o_oi.oi_fid;
577         int                      rc = 0;
578
579         if (*nr_local > PTLRPC_MAX_BRW_PAGES) {
580                 CERROR("%s: bulk has too many pages %d, which exceeds the"
581                        "maximum pages per RPC of %d\n",
582                        exp->exp_obd->obd_name, *nr_local, PTLRPC_MAX_BRW_PAGES);
583                 RETURN(-EPROTO);
584         }
585
586         if (tgt_ses_req(tsi) == NULL) { /* echo client case */
587                 LASSERT(oti != NULL);
588                 info = ofd_info_init(env, exp);
589                 ofd_oti2info(info, oti);
590                 jobid = oti->oti_jobid;
591         } else {
592                 info = tsi2ofd_info(tsi);
593                 jobid = tsi->tsi_jobid;
594         }
595
596         LASSERT(oa != NULL);
597
598         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT)) {
599                 struct ofd_seq          *oseq;
600
601                 oseq = ofd_seq_load(env, ofd, ostid_seq(&oa->o_oi));
602                 if (IS_ERR(oseq)) {
603                         CERROR("%s: Can not find seq for "DOSTID
604                                ": rc = %ld\n", ofd_name(ofd), POSTID(&oa->o_oi),
605                                PTR_ERR(oseq));
606                         RETURN(-EINVAL);
607                 }
608
609                 if (oseq->os_destroys_in_progress == 0) {
610                         /* don't fail lookups for orphan recovery, it causes
611                          * later LBUGs when objects still exist during
612                          * precreate */
613                         ofd_seq_put(env, oseq);
614                         RETURN(-ENOENT);
615                 }
616                 ofd_seq_put(env, oseq);
617         }
618
619         LASSERT(objcount == 1);
620         LASSERT(obj->ioo_bufcnt > 0);
621
622         if (cmd == OBD_BRW_WRITE) {
623                 rc = ofd_auth_capa(exp, fid, ostid_seq(&oa->o_oi),
624                                    capa, CAPA_OPC_OSS_WRITE);
625                 if (rc == 0) {
626                         la_from_obdo(&info->fti_attr, oa, OBD_MD_FLGETATTR);
627                         rc = ofd_preprw_write(env, exp, ofd, fid,
628                                               &info->fti_attr, oa, objcount,
629                                               obj, rnb, nr_local, lnb, jobid);
630                 }
631         } else if (cmd == OBD_BRW_READ) {
632                 rc = ofd_auth_capa(exp, fid, ostid_seq(&oa->o_oi),
633                                    capa, CAPA_OPC_OSS_READ);
634                 if (rc == 0) {
635                         ofd_grant_prepare_read(env, exp, oa);
636                         rc = ofd_preprw_read(env, exp, ofd, fid,
637                                              &info->fti_attr, oa,
638                                              obj->ioo_bufcnt, rnb, nr_local,
639                                              lnb, jobid);
640                         obdo_from_la(oa, &info->fti_attr, LA_ATIME);
641                 }
642         } else {
643                 CERROR("%s: wrong cmd %d received!\n",
644                        exp->exp_obd->obd_name, cmd);
645                 rc = -EPROTO;
646         }
647         RETURN(rc);
648 }
649
650 static int
651 ofd_commitrw_read(const struct lu_env *env, struct ofd_device *ofd,
652                   const struct lu_fid *fid, int objcount, int niocount,
653                   struct niobuf_local *lnb)
654 {
655         struct ofd_object *fo;
656
657         ENTRY;
658
659         LASSERT(niocount > 0);
660
661         fo = ofd_object_find(env, ofd, fid);
662         if (IS_ERR(fo))
663                 RETURN(PTR_ERR(fo));
664         LASSERT(fo != NULL);
665         LASSERT(ofd_object_exists(fo));
666         dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
667
668         ofd_read_unlock(env, fo);
669         ofd_object_put(env, fo);
670         /* second put is pair to object_get in ofd_preprw_read */
671         ofd_object_put(env, fo);
672
673         RETURN(0);
674 }
675
676 static int
677 ofd_write_attr_set(const struct lu_env *env, struct ofd_device *ofd,
678                    struct ofd_object *ofd_obj, struct lu_attr *la,
679                    struct filter_fid *ff)
680 {
681         struct ofd_thread_info  *info = ofd_info(env);
682         __u64                    valid = la->la_valid;
683         int                      rc;
684         struct thandle          *th;
685         struct dt_object        *dt_obj;
686         int                      ff_needed = 0;
687
688         ENTRY;
689
690         LASSERT(la);
691
692         dt_obj = ofd_object_child(ofd_obj);
693         LASSERT(dt_obj != NULL);
694
695         la->la_valid &= LA_UID | LA_GID;
696
697         rc = ofd_attr_handle_ugid(env, ofd_obj, la, 0 /* !is_setattr */);
698         if (rc != 0)
699                 GOTO(out, rc);
700
701         if (ff != NULL) {
702                 rc = ofd_object_ff_load(env, ofd_obj);
703                 if (rc == -ENODATA)
704                         ff_needed = 1;
705                 else if (rc < 0)
706                         GOTO(out, rc);
707         }
708
709         if (!la->la_valid && !ff_needed)
710                 /* no attributes to set */
711                 GOTO(out, rc = 0);
712
713         th = ofd_trans_create(env, ofd);
714         if (IS_ERR(th))
715                 GOTO(out, rc = PTR_ERR(th));
716
717         if (la->la_valid) {
718                 rc = dt_declare_attr_set(env, dt_obj, la, th);
719                 if (rc)
720                         GOTO(out_tx, rc);
721         }
722
723         if (ff_needed) {
724                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
725                         ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
726                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
727                         ff->ff_parent.f_oid =
728                         cpu_to_le32(le32_to_cpu(ff->ff_parent.f_oid) - 1);
729
730                 info->fti_buf.lb_buf = ff;
731                 info->fti_buf.lb_len = sizeof(*ff);
732                 rc = dt_declare_xattr_set(env, dt_obj, &info->fti_buf,
733                                           XATTR_NAME_FID, 0, th);
734                 if (rc)
735                         GOTO(out_tx, rc);
736         }
737
738         /* We don't need a transno for this operation which will be re-executed
739          * anyway when the OST_WRITE (with a transno assigned) is replayed */
740         rc = dt_trans_start_local(env, ofd->ofd_osd , th);
741         if (rc)
742                 GOTO(out_tx, rc);
743
744         /* set uid/gid */
745         if (la->la_valid) {
746                 rc = dt_attr_set(env, dt_obj, la, th,
747                                  ofd_object_capa(env, ofd_obj));
748                 if (rc)
749                         GOTO(out_tx, rc);
750         }
751
752         /* set filter fid EA */
753         if (ff_needed) {
754                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
755                         GOTO(out_tx, rc);
756
757                 rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID,
758                                   0, th, BYPASS_CAPA);
759                 if (rc == 0) {
760                         ofd_obj->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
761                         ofd_obj->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
762                         /* Currently, the filter_fid::ff_parent::f_ver is not
763                          * the real parent MDT-object's FID::f_ver, instead it
764                          * is the OST-object index in its parent MDT-object's
765                          * layout EA. */
766                         ofd_obj->ofo_pfid.f_stripe_idx =
767                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
768                 }
769         }
770
771         GOTO(out_tx, rc);
772
773 out_tx:
774         dt_trans_stop(env, ofd->ofd_osd, th);
775 out:
776         la->la_valid = valid;
777         return rc;
778 }
779
780 struct ofd_soft_sync_callback {
781         struct dt_txn_commit_cb  ossc_cb;
782         struct obd_export       *ossc_exp;
783 };
784
785 static void ofd_cb_soft_sync(struct lu_env *env, struct thandle *th,
786                              struct dt_txn_commit_cb *cb, int err)
787 {
788         struct ofd_soft_sync_callback   *ossc;
789
790         ossc = container_of(cb, struct ofd_soft_sync_callback, ossc_cb);
791
792         CDEBUG(D_INODE, "export %p soft sync count is reset\n", ossc->ossc_exp);
793         atomic_set(&ossc->ossc_exp->exp_filter_data.fed_soft_sync_count, 0);
794
795         class_export_cb_put(ossc->ossc_exp);
796         OBD_FREE_PTR(ossc);
797 }
798
799 static int ofd_soft_sync_cb_add(struct thandle *th, struct obd_export *exp)
800 {
801         struct ofd_soft_sync_callback           *ossc;
802         struct dt_txn_commit_cb                 *dcb;
803         int                                      rc;
804
805         OBD_ALLOC_PTR(ossc);
806         if (ossc == NULL)
807                 return -ENOMEM;
808
809         ossc->ossc_exp = class_export_cb_get(exp);
810
811         dcb = &ossc->ossc_cb;
812         dcb->dcb_func = ofd_cb_soft_sync;
813         CFS_INIT_LIST_HEAD(&dcb->dcb_linkage);
814         strncpy(dcb->dcb_name, "ofd_cb_soft_sync", MAX_COMMIT_CB_STR_LEN);
815         dcb->dcb_name[MAX_COMMIT_CB_STR_LEN - 1] = '\0';
816
817         rc = dt_trans_cb_add(th, dcb);
818         if (rc) {
819                 class_export_cb_put(exp);
820                 OBD_FREE_PTR(ossc);
821         }
822
823         return rc;
824 }
825
826 static int
827 ofd_commitrw_write(const struct lu_env *env, struct obd_export *exp,
828                    struct ofd_device *ofd, const struct lu_fid *fid,
829                    struct lu_attr *la, struct filter_fid *ff, int objcount,
830                    int niocount, struct niobuf_local *lnb, int old_rc)
831 {
832         struct ofd_thread_info  *info = ofd_info(env);
833         struct ofd_object       *fo;
834         struct dt_object        *o;
835         struct thandle          *th;
836         int                      rc = 0;
837         int                      retries = 0;
838         int                      i;
839         struct filter_export_data *fed = &exp->exp_filter_data;
840         bool                     soft_sync = false;
841         bool                     cb_registered = false;
842
843         ENTRY;
844
845         LASSERT(objcount == 1);
846
847         fo = ofd_object_find(env, ofd, fid);
848         LASSERT(fo != NULL);
849         LASSERT(ofd_object_exists(fo));
850
851         o = ofd_object_child(fo);
852         LASSERT(o != NULL);
853
854         if (old_rc)
855                 GOTO(out, rc = old_rc);
856
857         /*
858          * The first write to each object must set some attributes.  It is
859          * important to set the uid/gid before calling
860          * dt_declare_write_commit() since quota enforcement is now handled in
861          * declare phases.
862          */
863         rc = ofd_write_attr_set(env, ofd, fo, la, ff);
864         if (rc)
865                 GOTO(out, rc);
866
867         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
868
869 retry:
870         th = ofd_trans_create(env, ofd);
871         if (IS_ERR(th))
872                 GOTO(out, rc = PTR_ERR(th));
873
874         th->th_sync |= ofd->ofd_syncjournal;
875         if (th->th_sync == 0) {
876                 for (i = 0; i < niocount; i++) {
877                         if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
878                                 th->th_sync = 1;
879                                 break;
880                         }
881                         if (lnb[i].lnb_flags & OBD_BRW_SOFT_SYNC)
882                                 soft_sync = true;
883                 }
884         }
885
886         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
887                 GOTO(out_stop, rc = -EINPROGRESS);
888
889         rc = dt_declare_write_commit(env, o, lnb, niocount, th);
890         if (rc)
891                 GOTO(out_stop, rc);
892
893         if (la->la_valid) {
894                 /* update [mac]time if needed */
895                 rc = dt_declare_attr_set(env, o, la, th);
896                 if (rc)
897                         GOTO(out_stop, rc);
898         }
899
900         rc = ofd_trans_start(env, ofd, fo, th);
901         if (rc)
902                 GOTO(out_stop, rc);
903
904         rc = dt_write_commit(env, o, lnb, niocount, th);
905         if (rc)
906                 GOTO(out_stop, rc);
907
908         if (la->la_valid) {
909                 rc = dt_attr_set(env, o, la, th, ofd_object_capa(env, fo));
910                 if (rc)
911                         GOTO(out_stop, rc);
912         }
913
914         /* get attr to return */
915         rc = dt_attr_get(env, o, la, ofd_object_capa(env, fo));
916
917 out_stop:
918         /* Force commit to make the just-deleted blocks
919          * reusable. LU-456 */
920         if (rc == -ENOSPC)
921                 th->th_sync = 1;
922
923         /* do this before trans stop in case commit has finished */
924         if (!th->th_sync && soft_sync && !cb_registered) {
925                 ofd_soft_sync_cb_add(th, exp);
926                 cb_registered = true;
927         }
928
929         ofd_trans_stop(env, ofd, th, rc);
930         if (rc == -ENOSPC && retries++ < 3) {
931                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
932                        retries);
933                 goto retry;
934         }
935
936         if (!soft_sync)
937                 /* reset fed_soft_sync_count upon non-SOFT_SYNC RPC */
938                 atomic_set(&fed->fed_soft_sync_count, 0);
939         else if (atomic_inc_return(&fed->fed_soft_sync_count) ==
940                  ofd->ofd_soft_sync_limit)
941                 dt_commit_async(env, ofd->ofd_osd);
942
943 out:
944         dt_bufs_put(env, o, lnb, niocount);
945         ofd_read_unlock(env, fo);
946         ofd_object_put(env, fo);
947         /* second put is pair to object_get in ofd_preprw_write */
948         ofd_object_put(env, fo);
949         ofd_grant_commit(env, info->fti_exp, old_rc);
950         RETURN(rc);
951 }
952
953 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
954                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
955                  struct niobuf_remote *rnb, int npages,
956                  struct niobuf_local *lnb, struct obd_trans_info *oti,
957                  int old_rc)
958 {
959         struct ofd_thread_info  *info = ofd_info(env);
960         struct ofd_mod_data     *fmd;
961         __u64                    valid;
962         struct ofd_device       *ofd = ofd_exp(exp);
963         struct filter_fid       *ff = NULL;
964         const struct lu_fid     *fid = &oa->o_oi.oi_fid;
965         int                      rc = 0;
966
967         LASSERT(npages > 0);
968
969         if (cmd == OBD_BRW_WRITE) {
970                 /* Don't update timestamps if this write is older than a
971                  * setattr which modifies the timestamps. b=10150 */
972
973                 /* XXX when we start having persistent reservations this needs
974                  * to be changed to ofd_fmd_get() to create the fmd if it
975                  * doesn't already exist so we can store the reservation handle
976                  * there. */
977                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
978                 fmd = ofd_fmd_find(exp, fid);
979                 if (!fmd || fmd->fmd_mactime_xid < info->fti_xid)
980                         valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
981                                  OBD_MD_FLCTIME;
982                 ofd_fmd_put(exp, fmd);
983                 la_from_obdo(&info->fti_attr, oa, valid);
984
985                 if (oa->o_valid & OBD_MD_FLFID) {
986                         ff = &info->fti_mds_fid;
987                         ofd_prepare_fidea(ff, oa);
988                 }
989
990                 rc = ofd_commitrw_write(env, exp, ofd, fid, &info->fti_attr,
991                                         ff, objcount, npages, lnb, old_rc);
992                 if (rc == 0)
993                         obdo_from_la(oa, &info->fti_attr,
994                                      OFD_VALID_FLAGS | LA_GID | LA_UID);
995                 else
996                         obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID);
997
998                 /* don't report overquota flag if we failed before reaching
999                  * commit */
1000                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
1001                         /* return the overquota flags to client */
1002                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
1003                                 if (oa->o_valid & OBD_MD_FLFLAGS)
1004                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
1005                                 else
1006                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
1007                         }
1008
1009                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
1010                                 if (oa->o_valid & OBD_MD_FLFLAGS)
1011                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
1012                                 else
1013                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
1014                         }
1015
1016                         oa->o_valid |= OBD_MD_FLFLAGS;
1017                         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
1018                 }
1019         } else if (cmd == OBD_BRW_READ) {
1020                 struct ldlm_namespace *ns = ofd->ofd_namespace;
1021
1022                 /* If oa != NULL then ofd_preprw_read updated the inode
1023                  * atime and we should update the lvb so that other glimpses
1024                  * will also get the updated value. bug 5972 */
1025                 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
1026                          struct ldlm_resource *rs = NULL;
1027
1028                         ost_fid_build_resid(fid, &info->fti_resid);
1029                         rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
1030                                                LDLM_EXTENT, 0);
1031                         if (rs != NULL) {
1032                                 ns->ns_lvbo->lvbo_update(rs, NULL, 1);
1033                                 ldlm_resource_putref(rs);
1034                         }
1035                 }
1036                 rc = ofd_commitrw_read(env, ofd, fid, objcount,
1037                                        npages, lnb);
1038                 if (old_rc)
1039                         rc = old_rc;
1040         } else {
1041                 LBUG();
1042                 rc = -EPROTO;
1043         }
1044
1045         if (oti != NULL)
1046                 ofd_info2oti(info, oti);
1047         RETURN(rc);
1048 }