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