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