Whamcloud - gitweb
4ae58a1a86861b727c4565a838fbfccd8e0f8546
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ofd/ofd_io.c
32  *
33  * This file provides functions to handle IO requests from clients and
34  * also LFSCK routines to check parent file identifier (PFID) consistency.
35  *
36  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
37  * Author: Fan Yong <fan.yong@intel.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_FILTER
41
42 #include <linux/kthread.h>
43 #include "ofd_internal.h"
44 #include <lustre_nodemap.h>
45
46 struct ofd_inconsistency_item {
47         struct list_head         oii_list;
48         struct ofd_object       *oii_obj;
49         struct filter_fid        oii_ff;
50 };
51
52 /**
53  * Verify single object for parent FID consistency.
54  *
55  * Part of LFSCK processing which checks single object PFID stored in extended
56  * attribute (XATTR) against real FID of MDT parent object received by LFSCK.
57  * This verifies that the OST object is being referenced by only a single MDT
58  * object.
59  *
60  * \param[in] env       execution environment
61  * \param[in] ofd       OFD device
62  * \param[in] oii       object-related local data
63  * \param[in] lrl       LFSCK request data
64  */
65 static void ofd_inconsistency_verify_one(const struct lu_env *env,
66                                          struct ofd_device *ofd,
67                                          struct ofd_inconsistency_item *oii,
68                                          struct lfsck_req_local *lrl)
69 {
70         struct ofd_object *fo = oii->oii_obj;
71         struct filter_fid *client_ff = &oii->oii_ff;
72         struct filter_fid *local_ff = &fo->ofo_ff;
73         int rc;
74
75         LASSERT(fo->ofo_pfid_checking);
76         LASSERT(!fo->ofo_pfid_verified);
77
78         lrl->lrl_fid = fo->ofo_header.loh_fid; /* OST-object itself FID. */
79         lrl->lrl_ff_client = *client_ff; /* client given PFID. */
80         lrl->lrl_ff_local = *local_ff; /* OST local stored PFID. */
81
82         rc = lfsck_in_notify_local(env, ofd->ofd_osd, lrl, NULL);
83         ofd_write_lock(env, fo);
84         switch (lrl->lrl_status) {
85         case LPVS_INIT:
86                 LASSERT(rc <= 0);
87
88                 if (rc < 0)
89                         CDEBUG(D_LFSCK, "%s: fail to verify OST local stored "
90                                "PFID xattr for "DFID", the client given PFID "
91                                DFID", OST local stored PFID "DFID": rc = %d\n",
92                                ofd_name(ofd), PFID(&fo->ofo_header.loh_fid),
93                                PFID(&client_ff->ff_parent),
94                                PFID(&local_ff->ff_parent), rc);
95                 else
96                         fo->ofo_pfid_verified = 1;
97                 break;
98         case LPVS_INCONSISTENT:
99                 LASSERT(rc != 0);
100
101                 ofd->ofd_inconsistency_self_detected++;
102                 if (rc < 0)
103                         CDEBUG(D_LFSCK, "%s: fail to verify the client given "
104                                "PFID for "DFID", the client given PFID "DFID
105                                ", local stored PFID "DFID": rc = %d\n",
106                                ofd_name(ofd), PFID(&fo->ofo_header.loh_fid),
107                                PFID(&client_ff->ff_parent),
108                                PFID(&local_ff->ff_parent), rc);
109                 else
110                         CDEBUG(D_LFSCK, "%s: both the client given PFID and "
111                                "the OST local stored PFID are stale for the "
112                                "OST-object "DFID", client given PFID is "DFID
113                                ", local stored PFID is "DFID"\n",
114                                ofd_name(ofd), PFID(&fo->ofo_header.loh_fid),
115                                PFID(&client_ff->ff_parent),
116                                PFID(&local_ff->ff_parent));
117                 break;
118         case LPVS_INCONSISTENT_TOFIX:
119                 ofd->ofd_inconsistency_self_detected++;
120                 if (rc == 0) {
121                         ofd->ofd_inconsistency_self_repaired++;
122                         CDEBUG(D_LFSCK, "%s: fixed the staled OST PFID xattr "
123                                "for "DFID", with the client given PFID "DFID
124                                ", the old stored PFID "DFID"\n",
125                                ofd_name(ofd), PFID(&fo->ofo_header.loh_fid),
126                                PFID(&client_ff->ff_parent),
127                                PFID(&local_ff->ff_parent));
128                 } else if (rc < 0) {
129                         CDEBUG(D_LFSCK, "%s: fail to fix the OST PFID xattr "
130                                "for "DFID", client given PFID "DFID", local "
131                                "stored PFID "DFID": rc = %d\n",
132                                ofd_name(ofd), PFID(&fo->ofo_header.loh_fid),
133                                PFID(&client_ff->ff_parent),
134                                PFID(&local_ff->ff_parent), rc);
135                 }
136                 local_ff->ff_parent = client_ff->ff_parent;
137                 fo->ofo_pfid_verified = 1;
138                 break;
139         default:
140                 break;
141         }
142         fo->ofo_pfid_checking = 0;
143         ofd_write_unlock(env, fo);
144
145         ofd_object_put(env, fo);
146         OBD_FREE_PTR(oii);
147 }
148
149 struct oivm_args {
150         struct ofd_device       *od_ofd;
151         struct lu_env           od_env;
152         struct lfsck_req_local  od_lrl;
153         struct completion       *od_started;
154 };
155
156 #ifndef TASK_IDLE
157 #define TASK_IDLE TASK_INTERRUPTIBLE
158 #endif
159
160 /**
161  * Verification thread to check parent FID consistency.
162  *
163  * Kernel thread to check consistency of parent FID for any
164  * new item added for checking by ofd_add_inconsistency_item().
165  *
166  * \param[in] args      OFD device
167  *
168  * \retval              0 on successful thread termination
169  * \retval              negative value if thread can't start
170  */
171 static int ofd_inconsistency_verification_main(void *_args)
172 {
173         struct oivm_args *args = _args;
174         struct lu_env *env = &args->od_env;
175         struct ofd_device *ofd = args->od_ofd;
176         struct ofd_inconsistency_item *oii;
177         struct lfsck_req_local *lrl = &args->od_lrl;
178         ENTRY;
179
180         lrl->lrl_event = LEL_PAIRS_VERIFY_LOCAL;
181         lrl->lrl_active = LFSCK_TYPE_LAYOUT;
182         complete(args->od_started);
183
184         spin_lock(&ofd->ofd_inconsistency_lock);
185         while (({set_current_state(TASK_IDLE);
186                  !kthread_should_stop(); })) {
187
188                 while (!list_empty(&ofd->ofd_inconsistency_list)) {
189                         __set_current_state(TASK_RUNNING);
190                         oii = list_entry(ofd->ofd_inconsistency_list.next,
191                                          struct ofd_inconsistency_item,
192                                          oii_list);
193                         list_del_init(&oii->oii_list);
194                         spin_unlock(&ofd->ofd_inconsistency_lock);
195                         ofd_inconsistency_verify_one(env, ofd, oii, lrl);
196                         spin_lock(&ofd->ofd_inconsistency_lock);
197                 }
198
199                 spin_unlock(&ofd->ofd_inconsistency_lock);
200                 schedule();
201                 spin_lock(&ofd->ofd_inconsistency_lock);
202         }
203         __set_current_state(TASK_RUNNING);
204
205         while (!list_empty(&ofd->ofd_inconsistency_list)) {
206                 struct ofd_object *fo;
207
208                 oii = list_entry(ofd->ofd_inconsistency_list.next,
209                                  struct ofd_inconsistency_item,
210                                  oii_list);
211                 list_del_init(&oii->oii_list);
212                 fo = oii->oii_obj;
213                 spin_unlock(&ofd->ofd_inconsistency_lock);
214
215                 ofd_write_lock(env, fo);
216                 fo->ofo_pfid_checking = 0;
217                 ofd_write_unlock(env, fo);
218
219                 ofd_object_put(env, fo);
220                 OBD_FREE_PTR(oii);
221                 spin_lock(&ofd->ofd_inconsistency_lock);
222         }
223
224         spin_unlock(&ofd->ofd_inconsistency_lock);
225
226         lu_env_fini(&args->od_env);
227         OBD_FREE_PTR(args);
228         return 0;
229 }
230
231 /**
232  * Start parent FID verification thread.
233  *
234  * See ofd_inconsistency_verification_main().
235  *
236  * \param[in] ofd       OFD device
237  *
238  * \retval              0 on successful start of thread
239  * \retval              negative value on error
240  */
241 int ofd_start_inconsistency_verification_thread(struct ofd_device *ofd)
242 {
243         struct task_struct      *task;
244         struct oivm_args        *args;
245         DECLARE_COMPLETION_ONSTACK(started);
246         int                      rc;
247
248         if (ofd->ofd_inconsistency_task)
249                 return -EALREADY;
250
251         OBD_ALLOC_PTR(args);
252         if (!args)
253                 return -ENOMEM;
254         rc = lu_env_init(&args->od_env, LCT_DT_THREAD);
255         if (rc) {
256                 OBD_FREE_PTR(args);
257                 return rc;
258         }
259
260         args->od_ofd = ofd;
261         args->od_started = &started;
262         task = kthread_create(ofd_inconsistency_verification_main, args,
263                               "inconsistency_verification");
264         if (IS_ERR(task)) {
265                 rc = PTR_ERR(task);
266                 CERROR("%s: cannot start self_repair thread: rc = %d\n",
267                        ofd_name(ofd), rc);
268         } else {
269                 rc = 0;
270                 spin_lock(&ofd->ofd_inconsistency_lock);
271                 if (ofd->ofd_inconsistency_task)
272                         rc = -EALREADY;
273                 else
274                         ofd->ofd_inconsistency_task = task;
275                 spin_unlock(&ofd->ofd_inconsistency_lock);
276
277                 if (rc)
278                         kthread_stop(task);
279                 else {
280                         wake_up_process(task);
281                         wait_for_completion(&started);
282                 }
283         }
284         if (rc) {
285                 lu_env_fini(&args->od_env);
286                 OBD_FREE_PTR(args);
287         }
288
289         return rc;
290 }
291
292 /**
293  * Stop parent FID verification thread.
294  *
295  * \param[in] ofd       OFD device
296  *
297  * \retval              0 on successful start of thread
298  * \retval              -EALREADY if thread is already stopped
299  */
300 int ofd_stop_inconsistency_verification_thread(struct ofd_device *ofd)
301 {
302         struct task_struct *task;
303
304         spin_lock(&ofd->ofd_inconsistency_lock);
305         task = ofd->ofd_inconsistency_task;
306         ofd->ofd_inconsistency_task = NULL;
307         spin_unlock(&ofd->ofd_inconsistency_lock);
308
309         if (!task)
310                 return -EALREADY;
311         kthread_stop(task);
312
313         return 0;
314 }
315
316 /**
317  * Add new item for parent FID verification.
318  *
319  * Prepare new verification item and pass it to the dedicated
320  * verification thread for further processing.
321  *
322  * \param[in] env       execution environment
323  * \param[in] fo        OFD object
324  * \param[in] oa        OBDO structure with PFID
325  */
326 static void ofd_add_inconsistency_item(const struct lu_env *env,
327                                        struct ofd_object *fo, struct obdo *oa)
328 {
329         struct ofd_device *ofd = ofd_obj2dev(fo);
330         struct ofd_inconsistency_item *oii;
331         struct filter_fid *ff;
332         bool wakeup = false;
333
334         OBD_ALLOC_PTR(oii);
335         if (oii == NULL)
336                 return;
337
338         INIT_LIST_HEAD(&oii->oii_list);
339         lu_object_get(&fo->ofo_obj.do_lu);
340         oii->oii_obj = fo;
341         ff = &oii->oii_ff;
342         ff->ff_parent.f_seq = oa->o_parent_seq;
343         ff->ff_parent.f_oid = oa->o_parent_oid;
344         ff->ff_parent.f_stripe_idx = oa->o_stripe_idx;
345         ff->ff_layout = oa->o_layout;
346
347         spin_lock(&ofd->ofd_inconsistency_lock);
348         if (fo->ofo_pfid_checking || fo->ofo_pfid_verified) {
349                 spin_unlock(&ofd->ofd_inconsistency_lock);
350                 OBD_FREE_PTR(oii);
351
352                 return;
353         }
354
355         fo->ofo_pfid_checking = 1;
356         if (list_empty(&ofd->ofd_inconsistency_list))
357                 wakeup = true;
358         list_add_tail(&oii->oii_list, &ofd->ofd_inconsistency_list);
359         if (wakeup && ofd->ofd_inconsistency_task)
360                 wake_up_process(ofd->ofd_inconsistency_task);
361         spin_unlock(&ofd->ofd_inconsistency_lock);
362
363         /* XXX: When the found inconsistency exceeds some threshold,
364          *      we can trigger the LFSCK to scan part of the system
365          *      or the whole system, which depends on how to define
366          *      the threshold, a simple way maybe like that: define
367          *      the absolute value of how many inconsisteny allowed
368          *      to be repaired via self detect/repair mechanism, if
369          *      exceeded, then trigger the LFSCK to scan the layout
370          *      inconsistency within the whole system. */
371 }
372
373 /**
374  * Verify parent FID of an object.
375  *
376  * Check the parent FID is sane and start extended
377  * verification procedure otherwise.
378  *
379  * \param[in] env       execution environment
380  * \param[in] fo        OFD object
381  * \param[in] oa        OBDO structure with PFID
382  *
383  * \retval              0 on successful verification
384  * \retval              -EINPROGRESS if PFID is being repaired
385  * \retval              -EPERM if PFID was verified but still insane
386  */
387 int ofd_verify_ff(const struct lu_env *env, struct ofd_object *fo,
388                   struct obdo *oa)
389 {
390         struct lu_fid *pfid = &fo->ofo_ff.ff_parent;
391         int rc = 0;
392         ENTRY;
393
394         if (fid_is_sane(pfid)) {
395                 if (likely(oa->o_parent_seq == pfid->f_seq &&
396                            oa->o_parent_oid == pfid->f_oid &&
397                            oa->o_stripe_idx == pfid->f_stripe_idx))
398                         RETURN(0);
399
400                 if (fo->ofo_pfid_verified)
401                         RETURN(-EPERM);
402         }
403
404         /* The OST-object may be inconsistent, and we need further verification.
405          * To avoid block the RPC service thread, return -EINPROGRESS to client
406          * and make it retry later. */
407         if (fo->ofo_pfid_checking)
408                 RETURN(-EINPROGRESS);
409
410         rc = ofd_object_ff_load(env, fo);
411         if (rc == -ENODATA)
412                 RETURN(0);
413
414         if (rc < 0)
415                 RETURN(rc);
416
417         if (likely(oa->o_parent_seq == pfid->f_seq &&
418                    oa->o_parent_oid == pfid->f_oid &&
419                    oa->o_stripe_idx == pfid->f_stripe_idx))
420                 RETURN(0);
421
422         /* Push it to the dedicated thread for further verification. */
423         ofd_add_inconsistency_item(env, fo, oa);
424
425         RETURN(-EINPROGRESS);
426 }
427
428 /**
429  * FLR: verify the layout version of object.
430  *
431  * \param[in] env       execution environment
432  * \param[in] fo        OFD object
433  * \param[in] oa        OBDO structure with layout version
434  *
435  * \retval              0 on successful verification
436  * \retval              -EINPROGRESS layout version is in transfer
437  * \retval              -ESTALE the layout version on client is stale
438  */
439 int ofd_verify_layout_version(const struct lu_env *env,
440                               struct ofd_object *fo, const struct obdo *oa)
441 {
442         __u32 layout_version;
443         int rc;
444         ENTRY;
445
446         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_OST_SKIP_LV_CHECK)))
447                 GOTO(out, rc = 0);
448
449         rc = ofd_object_ff_load(env, fo);
450         if (rc < 0) {
451                 if (rc == -ENODATA)
452                         rc = -EINPROGRESS;
453                 GOTO(out, rc);
454         }
455
456         layout_version = fo->ofo_ff.ff_layout_version;
457         if (oa->o_layout_version >= layout_version &&
458             oa->o_layout_version <= layout_version + fo->ofo_ff.ff_range)
459                 GOTO(out, rc = 0);
460
461         /* normal traffic, decide if to return ESTALE or EINPROGRESS */
462         layout_version &= ~LU_LAYOUT_RESYNC;
463
464         /* this update is not legitimate */
465         if ((oa->o_layout_version & ~LU_LAYOUT_RESYNC) <= layout_version)
466                 GOTO(out, rc = -ESTALE);
467
468         /* layout version may not be transmitted yet */
469         if ((oa->o_layout_version & ~LU_LAYOUT_RESYNC) > layout_version)
470                 GOTO(out, rc = -EINPROGRESS);
471
472         EXIT;
473
474 out:
475         CDEBUG(D_INODE, DFID " verify layout version: %u vs. %u/%u, rc: %d\n",
476                PFID(lu_object_fid(&fo->ofo_obj.do_lu)),
477                oa->o_layout_version, fo->ofo_ff.ff_layout_version,
478                fo->ofo_ff.ff_range, rc);
479         return rc;
480
481 }
482
483 /*
484  * Lazy ATIME update to refresh atime every ofd_atime_diff
485  * seconds so that external scanning tool can see it actual
486  * within that period and be able to identify accessed files
487  */
488 static void ofd_handle_atime(const struct lu_env *env, struct ofd_device *ofd,
489                              struct ofd_object *fo, time64_t atime)
490 {
491         struct lu_attr *la;
492         struct dt_object *o;
493         struct thandle *th;
494         int rc;
495
496         if (ofd->ofd_atime_diff == 0)
497                 return;
498
499         la = &ofd_info(env)->fti_attr2;
500         o = ofd_object_child(fo);
501
502         if (unlikely(fo->ofo_atime_ondisk == 0)) {
503                 rc = dt_attr_get(env, o, la);
504                 if (unlikely(rc))
505                         return;
506                 LASSERT(la->la_valid & LA_ATIME);
507                 if (la->la_atime == 0)
508                         la->la_atime = la->la_mtime;
509                 fo->ofo_atime_ondisk = la->la_atime;
510         }
511         if (atime - fo->ofo_atime_ondisk < ofd->ofd_atime_diff)
512                 return;
513
514         /* atime hasn't been updated too long, update it */
515         fo->ofo_atime_ondisk = atime;
516
517         th = ofd_trans_create(env, ofd);
518         if (IS_ERR(th)) {
519                 CERROR("%s: cannot create transaction: rc = %d\n",
520                        ofd_name(ofd), (int)PTR_ERR(th));
521                 return;
522         }
523
524         la->la_valid = LA_ATIME;
525         rc = dt_declare_attr_set(env, o, la, th);
526         if (rc)
527                 GOTO(out_tx, rc);
528
529         rc = dt_trans_start_local(env, ofd->ofd_osd , th);
530         if (rc) {
531                 CERROR("%s: cannot start transaction: rc = %d\n",
532                        ofd_name(ofd), rc);
533                 GOTO(out_tx, rc);
534         }
535
536         ofd_read_lock(env, fo);
537         if (ofd_object_exists(fo)) {
538                 la->la_atime = fo->ofo_atime_ondisk;
539                 rc = dt_attr_set(env, o, la, th);
540         }
541
542         ofd_read_unlock(env, fo);
543
544 out_tx:
545         ofd_trans_stop(env, ofd, th, rc);
546 }
547
548 /**
549  * Prepare buffers for read request processing.
550  *
551  * This function converts remote buffers from client to local buffers
552  * and prepares the latter.
553  *
554  * \param[in] env       execution environment
555  * \param[in] exp       OBD export of client
556  * \param[in] ofd       OFD device
557  * \param[in] fid       FID of object
558  * \param[in] la        object attributes
559  * \param[in] oa        OBDO structure from client
560  * \param[in] niocount  number of remote buffers
561  * \param[in] rnb       remote buffers
562  * \param[in] nr_local  number of local buffers
563  * \param[in] lnb       local buffers
564  * \param[in] jobid     job ID name
565  *
566  * \retval              0 on successful prepare
567  * \retval              negative value on error
568  */
569 static int ofd_preprw_read(const struct lu_env *env, struct obd_export *exp,
570                            struct ofd_device *ofd, const struct lu_fid *fid,
571                            struct lu_attr *la, struct obdo *oa, int niocount,
572                            struct niobuf_remote *rnb, int *nr_local,
573                            struct niobuf_local *lnb, char *jobid)
574 {
575         struct ofd_object *fo;
576         int i, j, rc, tot_bytes = 0;
577         enum dt_bufs_type dbt = DT_BUFS_TYPE_READ;
578         int maxlnb = *nr_local;
579         __u64 begin, end;
580         ktime_t kstart = ktime_get();
581
582         ENTRY;
583         LASSERT(env != NULL);
584
585         fo = ofd_object_find(env, ofd, fid);
586         if (IS_ERR(fo))
587                 RETURN(PTR_ERR(fo));
588         LASSERT(fo != NULL);
589
590         ofd_info(env)->fti_obj = fo;
591
592         if (oa->o_valid & OBD_MD_FLATIME)
593                 ofd_handle_atime(env, ofd, fo, oa->o_atime);
594
595         ofd_read_lock(env, fo);
596         if (!ofd_object_exists(fo))
597                 GOTO(unlock, rc = -ENOENT);
598
599         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
600                 rc = ofd_verify_ff(env, fo, oa);
601                 if (rc != 0)
602                         GOTO(unlock, rc);
603         }
604
605         if (ptlrpc_connection_is_local(exp->exp_connection))
606                 dbt |= DT_BUFS_TYPE_LOCAL;
607
608         begin = -1;
609         end = 0;
610
611         for (*nr_local = 0, i = 0, j = 0; i < niocount; i++) {
612                 begin = min_t(__u64, begin, rnb[i].rnb_offset);
613                 end = max_t(__u64, end, rnb[i].rnb_offset + rnb[i].rnb_len);
614
615                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_2BIG_NIOBUF))
616                         rnb[i].rnb_len = 100 * 1024 * 1024;
617
618                 rc = dt_bufs_get(env, ofd_object_child(fo), rnb + i,
619                                  lnb + j, maxlnb, dbt);
620                 if (unlikely(rc < 0))
621                         GOTO(buf_put, rc);
622                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
623                 /* correct index for local buffers to continue with */
624                 j += rc;
625                 *nr_local += rc;
626                 maxlnb -= rc;
627                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
628                 tot_bytes += rnb[i].rnb_len;
629         }
630
631         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
632         rc = dt_read_prep(env, ofd_object_child(fo), lnb, *nr_local);
633         if (unlikely(rc))
634                 GOTO(buf_put, rc);
635
636         ofd_access(env, ofd,
637                 &(struct lu_fid) {
638                         .f_seq = oa->o_parent_seq,
639                         .f_oid = oa->o_parent_oid,
640                         .f_ver = oa->o_stripe_idx,
641                 },
642                 begin, end,
643                 tot_bytes,
644                 niocount,
645                 READ);
646
647         ofd_counter_incr(exp, LPROC_OFD_STATS_READ_BYTES, jobid, tot_bytes);
648         ofd_counter_incr(exp, LPROC_OFD_STATS_READ, jobid,
649                          ktime_us_delta(ktime_get(), kstart));
650         RETURN(0);
651
652 buf_put:
653         dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
654 unlock:
655         ofd_read_unlock(env, fo);
656         ofd_object_put(env, fo);
657         return rc;
658 }
659
660 /**
661  * Prepare buffers for write request processing.
662  *
663  * This function converts remote buffers from client to local buffers
664  * and prepares the latter. If there is recovery in progress and required
665  * object is missing then it can be re-created before write.
666  *
667  * \param[in] env       execution environment
668  * \param[in] exp       OBD export of client
669  * \param[in] ofd       OFD device
670  * \param[in] fid       FID of object
671  * \param[in] la        object attributes
672  * \param[in] oa        OBDO structure from client
673  * \param[in] objcount  always 1
674  * \param[in] obj       object data
675  * \param[in] rnb       remote buffers
676  * \param[in] nr_local  number of local buffers
677  * \param[in] lnb       local buffers
678  * \param[in] jobid     job ID name
679  *
680  * \retval              0 on successful prepare
681  * \retval              negative value on error
682  */
683 static int ofd_preprw_write(const struct lu_env *env, struct obd_export *exp,
684                             struct ofd_device *ofd, const struct lu_fid *fid,
685                             struct lu_attr *la, struct obdo *oa,
686                             int objcount, struct obd_ioobj *obj,
687                             struct niobuf_remote *rnb, int *nr_local,
688                             struct niobuf_local *lnb, char *jobid)
689 {
690         struct ofd_object *fo;
691         int i, j, k, rc = 0, tot_bytes = 0;
692         enum dt_bufs_type dbt = DT_BUFS_TYPE_WRITE;
693         int maxlnb = *nr_local;
694         __u64 begin, end;
695         ktime_t kstart = ktime_get();
696         struct range_lock *range = &ofd_info(env)->fti_write_range;
697
698         ENTRY;
699         LASSERT(env != NULL);
700         LASSERT(objcount == 1);
701
702         if (unlikely(exp->exp_obd->obd_recovering)) {
703                 u64 seq = fid_seq(fid);
704                 u64 oid = fid_oid(fid);
705                 struct ofd_seq *oseq;
706
707                 oseq = ofd_seq_load(env, ofd, seq);
708                 if (IS_ERR(oseq)) {
709                         CERROR("%s: Can't find FID Sequence %#llx: rc = %d\n",
710                                ofd_name(ofd), seq, (int)PTR_ERR(oseq));
711                         GOTO(out, rc = -EINVAL);
712                 }
713
714                 if (oid > ofd_seq_last_oid(oseq)) {
715                         int sync = 0;
716                         int diff;
717
718                         mutex_lock(&oseq->os_create_lock);
719                         diff = oid - ofd_seq_last_oid(oseq);
720
721                         /* Do sync create if the seq is about to used up */
722                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
723                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
724                                         sync = 1;
725                         } else if (fid_seq_is_norm(seq)) {
726                                 if (unlikely(oid >=
727                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
728                                         sync = 1;
729                         } else {
730                                 CERROR("%s : invalid o_seq "DOSTID"\n",
731                                        ofd_name(ofd), POSTID(&oa->o_oi));
732                                 mutex_unlock(&oseq->os_create_lock);
733                                 ofd_seq_put(env, oseq);
734                                 GOTO(out, rc = -EINVAL);
735                         }
736
737                         while (diff > 0) {
738                                 u64 next_id = ofd_seq_last_oid(oseq) + 1;
739                                 int count = ofd_precreate_batch(ofd, diff);
740
741                                 rc = ofd_precreate_objects(env, ofd, next_id,
742                                                            oseq, count, sync);
743                                 if (rc < 0) {
744                                         mutex_unlock(&oseq->os_create_lock);
745                                         ofd_seq_put(env, oseq);
746                                         GOTO(out, rc);
747                                 }
748
749                                 diff -= rc;
750                         }
751
752                         mutex_unlock(&oseq->os_create_lock);
753                 }
754
755                 ofd_seq_put(env, oseq);
756         }
757
758         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
759          * space back if possible, we have to do this outside of the lock as
760          * grant preparation may need to sync whole fs thus wait for all the
761          * transactions to complete. */
762         tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
763
764         fo = ofd_object_find(env, ofd, fid);
765         if (IS_ERR(fo))
766                 GOTO(out, rc = PTR_ERR(fo));
767         LASSERT(fo != NULL);
768
769         ofd_info(env)->fti_obj = fo;
770
771         ofd_read_lock(env, fo);
772         if (!ofd_object_exists(fo)) {
773                 CERROR("%s: BRW to missing obj "DOSTID"\n",
774                        exp->exp_obd->obd_name, POSTID(&obj->ioo_oid));
775                 ofd_read_unlock(env, fo);
776                 ofd_object_put(env, fo);
777                 GOTO(out, rc = -ENOENT);
778         }
779
780         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
781                 rc = ofd_verify_ff(env, fo, oa);
782                 if (rc != 0) {
783                         ofd_read_unlock(env, fo);
784                         ofd_object_put(env, fo);
785                         GOTO(out, rc);
786                 }
787         }
788
789         /* need to verify layout version */
790         if (oa->o_valid & OBD_MD_LAYOUT_VERSION) {
791                 rc = ofd_verify_layout_version(env, fo, oa);
792                 if (rc) {
793                         ofd_read_unlock(env, fo);
794                         ofd_object_put(env, fo);
795                         GOTO(out, rc);
796                 }
797
798                 oa->o_valid &= ~OBD_MD_LAYOUT_VERSION;
799         }
800
801         if (ptlrpc_connection_is_local(exp->exp_connection))
802                 dbt |= DT_BUFS_TYPE_LOCAL;
803
804         begin = -1;
805         end = 0;
806
807         /* parse remote buffers to local buffers and prepare the latter */
808         for (*nr_local = 0, i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
809                 begin = min_t(__u64, begin, rnb[i].rnb_offset);
810                 end = max_t(__u64, end, rnb[i].rnb_offset + rnb[i].rnb_len);
811
812                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_2BIG_NIOBUF))
813                         rnb[i].rnb_len += PAGE_SIZE;
814                 rc = dt_bufs_get(env, ofd_object_child(fo),
815                                  rnb + i, lnb + j, maxlnb, dbt);
816                 if (unlikely(rc < 0))
817                         GOTO(err, rc);
818                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
819                 /* correct index for local buffers to continue with */
820                 for (k = 0; k < rc; k++) {
821                         lnb[j+k].lnb_flags = rnb[i].rnb_flags;
822                         lnb[j+k].lnb_flags &= ~OBD_BRW_LOCALS;
823                         if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
824                                 lnb[j+k].lnb_rc = -ENOSPC;
825                 }
826                 j += rc;
827                 *nr_local += rc;
828                 maxlnb -= rc;
829                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
830                 tot_bytes += rnb[i].rnb_len;
831         }
832         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
833
834         rc = dt_write_prep(env, ofd_object_child(fo), lnb, *nr_local);
835         if (unlikely(rc != 0))
836                 GOTO(err, rc);
837
838         ofd_read_unlock(env, fo);
839
840         ofd_access(env, ofd,
841                 &(struct lu_fid) {
842                         .f_seq = oa->o_parent_seq,
843                         .f_oid = oa->o_parent_oid,
844                         .f_ver = oa->o_stripe_idx,
845                 },
846                 begin, end,
847                 tot_bytes,
848                 obj->ioo_bufcnt,
849                 WRITE);
850
851         /*
852          * Reordering precautions: make sure that request processing that
853          * was able to receive its bulk data should not get reordered with
854          * overlapping BRW requests, e.g.
855          *  1) BRW1 sent, bulk data received, but disk I/O delayed
856          *  2) BRW1 resent and fully processed
857          *  3) the page was unlocked on the client and its writeback bit reset
858          *  4) BRW2 sent and fully processed
859          *  5) BRW1 processing wakes up and writes stale data to disk
860          * If on step 1 bulk data was not received, client resend will invalidate
861          * its bulk descriptor and the RPC will be dropped due to failed bulk
862          * transfer, which is just fine.
863          */
864         range_lock_init(range,
865                         rnb[0].rnb_offset,
866                         rnb[obj->ioo_bufcnt - 1].rnb_offset +
867                         rnb[obj->ioo_bufcnt - 1].rnb_len - 1);
868         range_lock(&fo->ofo_write_tree, range);
869         ofd_info(env)->fti_range_locked = 1;
870
871         ofd_counter_incr(exp, LPROC_OFD_STATS_WRITE_BYTES, jobid, tot_bytes);
872         ofd_counter_incr(exp, LPROC_OFD_STATS_WRITE, jobid,
873                          ktime_us_delta(ktime_get(), kstart));
874         RETURN(0);
875 err:
876         dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
877         ofd_read_unlock(env, fo);
878         ofd_object_put(env, fo);
879         /* tgt_grant_prepare_write() was called, so we must commit */
880         tgt_grant_commit(exp, oa->o_grant_used, rc);
881 out:
882         /* let's still process incoming grant information packed in the oa,
883          * but without enforcing grant since we won't proceed with the write.
884          * Just like a read request actually. */
885         tgt_grant_prepare_read(env, exp, oa);
886         return rc;
887 }
888
889 /**
890  * Prepare bulk IO requests for processing.
891  *
892  * This function does initial checks of IO and calls corresponding
893  * functions for read/write processing.
894  *
895  * \param[in] env       execution environment
896  * \param[in] cmd       IO type (read/write)
897  * \param[in] exp       OBD export of client
898  * \param[in] oa        OBDO structure from request
899  * \param[in] objcount  always 1
900  * \param[in] obj       object data
901  * \param[in] rnb       remote buffers
902  * \param[in] nr_local  number of local buffers
903  * \param[in] lnb       local buffers
904  *
905  * \retval              0 on successful prepare
906  * \retval              negative value on error
907  */
908 int ofd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
909                struct obdo *oa, int objcount, struct obd_ioobj *obj,
910                struct niobuf_remote *rnb, int *nr_local,
911                struct niobuf_local *lnb)
912 {
913         struct tgt_session_info *tsi = tgt_ses_info(env);
914         struct ofd_device       *ofd = ofd_exp(exp);
915         struct ofd_thread_info  *info;
916         char                    *jobid;
917         const struct lu_fid     *fid = &oa->o_oi.oi_fid;
918         int                      rc = 0;
919
920         if (*nr_local > PTLRPC_MAX_BRW_PAGES) {
921                 CERROR("%s: bulk has too many pages %d, which exceeds the maximum pages per RPC of %d\n",
922                        exp->exp_obd->obd_name, *nr_local, PTLRPC_MAX_BRW_PAGES);
923                 RETURN(-EPROTO);
924         }
925
926         if (tgt_ses_req(tsi) == NULL) { /* echo client case */
927                 info = ofd_info_init(env, exp);
928                 jobid = NULL;
929         } else {
930                 info = tsi2ofd_info(tsi);
931                 jobid = tsi->tsi_jobid;
932         }
933
934         LASSERT(oa != NULL);
935
936         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT)) {
937                 struct ofd_seq          *oseq;
938
939                 oseq = ofd_seq_load(env, ofd, ostid_seq(&oa->o_oi));
940                 if (IS_ERR(oseq)) {
941                         CERROR("%s: Can not find seq for "DOSTID
942                                ": rc = %ld\n", ofd_name(ofd), POSTID(&oa->o_oi),
943                                PTR_ERR(oseq));
944                         RETURN(-EINVAL);
945                 }
946
947                 if (oseq->os_destroys_in_progress == 0) {
948                         /* don't fail lookups for orphan recovery, it causes
949                          * later LBUGs when objects still exist during
950                          * precreate */
951                         ofd_seq_put(env, oseq);
952                         RETURN(-ENOENT);
953                 }
954                 ofd_seq_put(env, oseq);
955         }
956
957         LASSERT(objcount == 1);
958         LASSERT(obj->ioo_bufcnt > 0);
959
960         if (cmd == OBD_BRW_WRITE) {
961                 la_from_obdo(&info->fti_attr, oa, OBD_MD_FLGETATTR);
962                 rc = ofd_preprw_write(env, exp, ofd, fid, &info->fti_attr, oa,
963                                       objcount, obj, rnb, nr_local, lnb, jobid);
964         } else if (cmd == OBD_BRW_READ) {
965                 tgt_grant_prepare_read(env, exp, oa);
966                 rc = ofd_preprw_read(env, exp, ofd, fid, &info->fti_attr, oa,
967                                      obj->ioo_bufcnt, rnb, nr_local, lnb,
968                                      jobid);
969         } else {
970                 CERROR("%s: wrong cmd %d received!\n",
971                        exp->exp_obd->obd_name, cmd);
972                 rc = -EPROTO;
973         }
974         RETURN(rc);
975 }
976
977 /**
978  * Drop reference on local buffers for read bulk IO.
979  *
980  * This will free all local buffers use by this read request.
981  *
982  * \param[in] env       execution environment
983  * \param[in] ofd       OFD device
984  * \param[in] fid       object FID
985  * \param[in] objcount  always 1
986  * \param[in] niocount  number of local buffers
987  * \param[in] lnb       local buffers
988  *
989  * \retval              0 on successful execution
990  * \retval              negative value on error
991  */
992 static int
993 ofd_commitrw_read(const struct lu_env *env, struct ofd_device *ofd,
994                   const struct lu_fid *fid, int objcount, int niocount,
995                   struct niobuf_local *lnb)
996 {
997         struct ofd_object *fo;
998
999         ENTRY;
1000
1001         LASSERT(niocount > 0);
1002
1003         fo = ofd_info(env)->fti_obj;
1004         LASSERT(fo != NULL);
1005         LASSERT(ofd_object_exists(fo));
1006         dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
1007
1008         ofd_read_unlock(env, fo);
1009         ofd_object_put(env, fo);
1010
1011         RETURN(0);
1012 }
1013
1014 /**
1015  * Set attributes of object during write bulk IO processing.
1016  *
1017  * Change object attributes and write parent FID into extended
1018  * attributes when needed.
1019  *
1020  * \param[in] env       execution environment
1021  * \param[in] ofd       OFD device
1022  * \param[in] ofd_obj   OFD object
1023  * \param[in] la        object attributes
1024  * \param[in] oa        obdo
1025  *
1026  * \retval              0 on successful attributes update
1027  * \retval              negative value on error
1028  */
1029 static int
1030 ofd_write_attr_set(const struct lu_env *env, struct ofd_device *ofd,
1031                    struct ofd_object *ofd_obj, struct lu_attr *la,
1032                    struct obdo *oa)
1033 {
1034         struct ofd_thread_info  *info = ofd_info(env);
1035         struct filter_fid       *ff = &info->fti_mds_fid;
1036         __u64                    valid = la->la_valid;
1037         struct thandle          *th;
1038         struct dt_object        *dt_obj;
1039         int                      fl = 0;
1040         int                      rc;
1041
1042         ENTRY;
1043
1044         LASSERT(la);
1045
1046         dt_obj = ofd_object_child(ofd_obj);
1047         LASSERT(dt_obj != NULL);
1048
1049         la->la_valid &= LA_UID | LA_GID | LA_PROJID;
1050
1051         rc = ofd_attr_handle_id(env, ofd_obj, la, 0 /* !is_setattr */);
1052         if (rc != 0)
1053                 GOTO(out, rc);
1054
1055         if (!la->la_valid && !(oa->o_valid &
1056             (OBD_MD_FLFID | OBD_MD_FLOSTLAYOUT | OBD_MD_LAYOUT_VERSION)))
1057                 /* no attributes to set */
1058                 GOTO(out, rc = 0);
1059
1060         th = ofd_trans_create(env, ofd);
1061         if (IS_ERR(th))
1062                 GOTO(out, rc = PTR_ERR(th));
1063
1064         if (la->la_valid) {
1065                 rc = dt_declare_attr_set(env, dt_obj, la, th);
1066                 if (rc)
1067                         GOTO(out_tx, rc);
1068         }
1069
1070         if (oa->o_valid & (OBD_MD_FLFID | OBD_MD_FLOSTLAYOUT |
1071                            OBD_MD_LAYOUT_VERSION)) {
1072                 rc = dt_declare_xattr_set(env, dt_obj, &info->fti_buf,
1073                                           XATTR_NAME_FID, 0, th);
1074                 if (rc)
1075                         GOTO(out_tx, rc);
1076         }
1077         /* We don't need a transno for this operation which will be re-executed
1078          * anyway when the OST_WRITE (with a transno assigned) is replayed */
1079         rc = dt_trans_start_local(env, ofd->ofd_osd , th);
1080         if (rc)
1081                 GOTO(out_tx, rc);
1082
1083         ofd_read_lock(env, ofd_obj);
1084
1085         rc = ofd_attr_handle_id(env, ofd_obj, la, 0 /* !is_setattr */);
1086         if (rc != 0)
1087                 GOTO(out_unlock, rc);
1088
1089         if (!la->la_valid && !(oa->o_valid &
1090             (OBD_MD_FLFID | OBD_MD_FLOSTLAYOUT | OBD_MD_LAYOUT_VERSION)))
1091                 /* no attributes to set */
1092                 GOTO(out_unlock, rc = 0);
1093
1094
1095
1096         /* set uid/gid/projid */
1097         if (la->la_valid) {
1098                 rc = dt_attr_set(env, dt_obj, la, th);
1099                 if (rc)
1100                         GOTO(out_unlock, rc);
1101         }
1102
1103         fl = ofd_object_ff_update(env, ofd_obj, oa, ff);
1104         if (fl <= 0)
1105                 GOTO(out_unlock, rc = fl);
1106
1107         /* set filter fid EA.
1108          * FIXME: it holds read lock of ofd object to modify the XATTR_NAME_FID
1109          * while the write lock should be held. However, it should work because
1110          * write RPCs only modify ff_{parent,layout} and those information will
1111          * be the same from all the write RPCs. The reason that fl is not used
1112          * in dt_xattr_set() is to allow this race. */
1113         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
1114                 GOTO(out_unlock, rc);
1115         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
1116                 ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
1117         else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
1118                 le32_add_cpu(&ff->ff_parent.f_oid, -1);
1119
1120         info->fti_buf.lb_buf = ff;
1121         info->fti_buf.lb_len = sizeof(*ff);
1122         rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID, 0, th);
1123         if (rc == 0)
1124                 filter_fid_le_to_cpu(&ofd_obj->ofo_ff, ff, sizeof(*ff));
1125
1126         GOTO(out_unlock, rc);
1127
1128 out_unlock:
1129         ofd_read_unlock(env, ofd_obj);
1130 out_tx:
1131         dt_trans_stop(env, ofd->ofd_osd, th);
1132 out:
1133         la->la_valid = valid;
1134         return rc;
1135 }
1136
1137 struct ofd_soft_sync_callback {
1138         struct dt_txn_commit_cb  ossc_cb;
1139         struct obd_export       *ossc_exp;
1140 };
1141
1142 /**
1143  * Callback function for "soft sync" update.
1144  *
1145  * Reset fed_soft_sync_count upon committing the "soft_sync" update.
1146  * See ofd_soft_sync_cb_add() below for more details on soft sync.
1147  *
1148  * \param[in] env       execution environment
1149  * \param[in] th        transaction handle
1150  * \param[in] cb        callback data
1151  * \param[in] err       error code
1152  */
1153 static void ofd_cb_soft_sync(struct lu_env *env, struct thandle *th,
1154                              struct dt_txn_commit_cb *cb, int err)
1155 {
1156         struct ofd_soft_sync_callback   *ossc;
1157
1158         ossc = container_of(cb, struct ofd_soft_sync_callback, ossc_cb);
1159
1160         CDEBUG(D_INODE, "export %p soft sync count is reset\n", ossc->ossc_exp);
1161         atomic_set(&ossc->ossc_exp->exp_filter_data.fed_soft_sync_count, 0);
1162
1163         class_export_cb_put(ossc->ossc_exp);
1164         OBD_FREE_PTR(ossc);
1165 }
1166
1167 /**
1168  * Add callback for "soft sync" processing.
1169  *
1170  * The "soft sync" mechanism does asynchronous commit when OBD_BRW_SOFT_SYNC
1171  * flag is set in client buffers. The intention is for this operation to
1172  * commit pages belonging to a client which has "too many" outstanding
1173  * unstable pages in its cache. See LU-2139 for details.
1174  *
1175  * This function adds callback to be called when commit is done.
1176  *
1177  * \param[in] th        transaction handle
1178  * \param[in] exp       OBD export of client
1179  *
1180  * \retval              0 on successful callback adding
1181  * \retval              negative value on error
1182  */
1183 static int ofd_soft_sync_cb_add(struct thandle *th, struct obd_export *exp)
1184 {
1185         struct ofd_soft_sync_callback           *ossc;
1186         struct dt_txn_commit_cb                 *dcb;
1187         int                                      rc;
1188
1189         OBD_ALLOC_PTR(ossc);
1190         if (ossc == NULL)
1191                 return -ENOMEM;
1192
1193         ossc->ossc_exp = class_export_cb_get(exp);
1194
1195         dcb = &ossc->ossc_cb;
1196         dcb->dcb_func = ofd_cb_soft_sync;
1197         INIT_LIST_HEAD(&dcb->dcb_linkage);
1198         strlcpy(dcb->dcb_name, "ofd_cb_soft_sync", sizeof(dcb->dcb_name));
1199
1200         rc = dt_trans_cb_add(th, dcb);
1201         if (rc) {
1202                 class_export_cb_put(exp);
1203                 OBD_FREE_PTR(ossc);
1204         }
1205
1206         return rc;
1207 }
1208
1209 /**
1210  * Commit bulk IO buffers to the storage.
1211  *
1212  * This function finalizes write IO processing by writing data to the disk.
1213  * That write can be synchronous or asynchronous depending on buffers flags.
1214  *
1215  * \param[in] env       execution environment
1216  * \param[in] exp       OBD export of client
1217  * \param[in] ofd       OFD device
1218  * \param[in] fid       FID of object
1219  * \param[in] la        object attributes
1220  * \param[in] ff        parent FID of object
1221  * \param[in] objcount  always 1
1222  * \param[in] niocount  number of local buffers
1223  * \param[in] lnb       local buffers
1224  * \param[in] granted   grant space consumed for the bulk I/O
1225  * \param[in] old_rc    result of processing at this point
1226  *
1227  * \retval              0 on successful commit
1228  * \retval              negative value on error
1229  */
1230 static int
1231 ofd_commitrw_write(const struct lu_env *env, struct obd_export *exp,
1232                    struct ofd_device *ofd, const struct lu_fid *fid,
1233                    struct lu_attr *la, struct obdo *oa, int objcount,
1234                    int niocount, struct niobuf_local *lnb,
1235                    unsigned long granted, int old_rc)
1236 {
1237         struct ofd_thread_info *info = ofd_info(env);
1238         struct filter_export_data *fed = &exp->exp_filter_data;
1239         struct ofd_object *fo;
1240         struct dt_object *o;
1241         struct thandle *th;
1242         int rc = 0;
1243         int rc2 = 0;
1244         int retries = 0;
1245         int i, restart = 0;
1246         bool soft_sync = false;
1247         bool cb_registered = false;
1248         bool fake_write = false;
1249         struct range_lock *range = &ofd_info(env)->fti_write_range;
1250
1251         ENTRY;
1252
1253         LASSERT(objcount == 1);
1254
1255         fo = ofd_info(env)->fti_obj;
1256         LASSERT(fo != NULL);
1257
1258         o = ofd_object_child(fo);
1259         LASSERT(o != NULL);
1260
1261         if (old_rc)
1262                 GOTO(out, rc = old_rc);
1263         if (!ofd_object_exists(fo))
1264                 GOTO(out, rc = -ENOENT);
1265
1266         /*
1267          * The first write to each object must set some attributes.  It is
1268          * important to set the uid/gid before calling
1269          * dt_declare_write_commit() since quota enforcement is now handled in
1270          * declare phases.
1271          */
1272         rc = ofd_write_attr_set(env, ofd, fo, la, oa);
1273         if (rc)
1274                 GOTO(out, rc);
1275
1276         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
1277
1278         /* do fake write, to simulate the write case for performance testing */
1279         if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW)) {
1280                 struct niobuf_local *last = &lnb[niocount - 1];
1281                 __u64 file_size = last->lnb_file_offset + last->lnb_len;
1282                 __u64 valid = la->la_valid;
1283
1284                 la->la_valid = LA_SIZE;
1285                 la->la_size = 0;
1286                 rc = dt_attr_get(env, o, la);
1287                 if (rc < 0 && rc != -ENOENT)
1288                         GOTO(out, rc);
1289
1290                 if (file_size < la->la_size)
1291                         file_size = la->la_size;
1292
1293                 /* dirty inode by setting file size */
1294                 la->la_valid = valid | LA_SIZE;
1295                 la->la_size = file_size;
1296
1297                 fake_write = true;
1298         }
1299
1300 retry:
1301         th = ofd_trans_create(env, ofd);
1302         if (IS_ERR(th))
1303                 GOTO(out, rc = PTR_ERR(th));
1304
1305         th->th_sync |= ofd->ofd_sync_journal;
1306         if (th->th_sync == 0) {
1307                 for (i = 0; i < niocount; i++) {
1308                         if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
1309                                 th->th_sync = 1;
1310                                 break;
1311                         }
1312                         if (lnb[i].lnb_flags & OBD_BRW_SOFT_SYNC)
1313                                 soft_sync = true;
1314                 }
1315         }
1316
1317         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
1318                 GOTO(out_stop, rc = -EINPROGRESS);
1319
1320         if (likely(!fake_write)) {
1321                 rc = dt_declare_write_commit(env, o, lnb, niocount, th);
1322                 if (rc)
1323                         GOTO(out_stop, rc);
1324         }
1325
1326         /* don't update atime on disk if it is older */
1327         if (la->la_valid & LA_ATIME && la->la_atime <= fo->ofo_atime_ondisk)
1328                 la->la_valid &= ~LA_ATIME;
1329
1330         if (la->la_valid) {
1331                 /* update [mac]time if needed */
1332                 rc = dt_declare_attr_set(env, o, la, th);
1333                 if (rc)
1334                         GOTO(out_stop, rc);
1335         }
1336
1337         rc = ofd_trans_start(env, ofd, fo, th);
1338         if (rc)
1339                 GOTO(out_stop, rc);
1340
1341         ofd_read_lock(env, fo);
1342         if (!ofd_object_exists(fo))
1343                 GOTO(out_unlock, rc = -ENOENT);
1344
1345         /* Don't update timestamps if this write is older than a
1346          * setattr which modifies the timestamps. b=10150 */
1347         if (la->la_valid && tgt_fmd_check(exp, fid, info->fti_xid)) {
1348                 rc = dt_attr_set(env, o, la, th);
1349                 if (rc)
1350                         GOTO(out_unlock, rc);
1351                 if (la->la_valid & LA_ATIME)
1352                         fo->ofo_atime_ondisk = la->la_atime;
1353         }
1354
1355         if (likely(!fake_write)) {
1356                 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_OST_WR_ATTR_DELAY,
1357                                        OBD_FAIL_ONCE, cfs_fail_val);
1358                 rc = dt_write_commit(env, o, lnb, niocount, th, oa->o_size);
1359                 if (rc) {
1360                         restart = th->th_restart_tran;
1361                         GOTO(out_unlock, rc);
1362                 }
1363         }
1364
1365         /* get attr to return */
1366         rc = dt_attr_get(env, o, la);
1367
1368 out_unlock:
1369         ofd_read_unlock(env, fo);
1370 out_stop:
1371         /* Force commit to make the just-deleted blocks
1372          * reusable. LU-456 */
1373         if (rc == -ENOSPC)
1374                 th->th_sync = 1;
1375
1376         /* do this before trans stop in case commit has finished */
1377         if (!th->th_sync && soft_sync && !cb_registered) {
1378                 ofd_soft_sync_cb_add(th, exp);
1379                 cb_registered = true;
1380         }
1381
1382         if (rc == 0 && granted > 0) {
1383                 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
1384                         granted = 0;
1385         }
1386
1387         rc2 = ofd_trans_stop(env, ofd, th, restart ? 0 : rc);
1388         if (!rc)
1389                 rc = rc2;
1390         if (rc == -ENOSPC && retries++ < 3) {
1391                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
1392                        retries);
1393                 goto retry;
1394         }
1395
1396         if (restart) {
1397                 retries++;
1398                 restart = 0;
1399                 if (retries % 10000 == 0)
1400                         CERROR("%s: restart IO write too many times: %d\n",
1401                                 ofd_name(ofd), retries);
1402                 CDEBUG(D_INODE, "retry transaction, retries:%d\n",
1403                        retries);
1404                 goto retry;
1405         }
1406         if (!soft_sync)
1407                 /* reset fed_soft_sync_count upon non-SOFT_SYNC RPC */
1408                 atomic_set(&fed->fed_soft_sync_count, 0);
1409         else if (atomic_inc_return(&fed->fed_soft_sync_count) ==
1410                  ofd->ofd_soft_sync_limit)
1411                 dt_commit_async(env, ofd->ofd_osd);
1412
1413 out:
1414         if (info->fti_range_locked) {
1415                 range_unlock(&fo->ofo_write_tree, range);
1416                 info->fti_range_locked = 0;
1417         }
1418         dt_bufs_put(env, o, lnb, niocount);
1419         ofd_object_put(env, fo);
1420         if (granted > 0)
1421                 tgt_grant_commit(exp, granted, old_rc);
1422         RETURN(rc);
1423 }
1424
1425 /**
1426  * Commit bulk IO to the storage.
1427  *
1428  * This is companion function to the ofd_preprw(). It finishes bulk IO
1429  * request processing by committing buffers to the storage (WRITE) and/or
1430  * freeing those buffers (read/write). See ofd_commitrw_read() and
1431  * ofd_commitrw_write() for details about each type of IO.
1432  *
1433  * \param[in] env       execution environment
1434  * \param[in] cmd       IO type (READ/WRITE)
1435  * \param[in] exp       OBD export of client
1436  * \param[in] oa        OBDO structure from client
1437  * \param[in] objcount  always 1
1438  * \param[in] obj       object data
1439  * \param[in] rnb       remote buffers
1440  * \param[in] npages    number of local buffers
1441  * \param[in] lnb       local buffers
1442  * \param[in] old_rc    result of processing at this point
1443  *
1444  * \retval              0 on successful commit
1445  * \retval              negative value on error
1446  */
1447 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
1448                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
1449                  struct niobuf_remote *rnb, int npages,
1450                  struct niobuf_local *lnb, int old_rc)
1451 {
1452         struct ofd_thread_info *info = ofd_info(env);
1453         struct ofd_device *ofd = ofd_exp(exp);
1454         const struct lu_fid *fid = &oa->o_oi.oi_fid;
1455         struct ldlm_namespace *ns = ofd->ofd_namespace;
1456         struct ldlm_resource *rs = NULL;
1457         __u64 valid;
1458         int rc = 0;
1459
1460         LASSERT(npages > 0);
1461
1462         if (cmd == OBD_BRW_WRITE) {
1463                 struct lu_nodemap *nodemap;
1464
1465                 valid = OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLPROJID |
1466                         OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
1467                 la_from_obdo(&info->fti_attr, oa, valid);
1468
1469                 rc = ofd_commitrw_write(env, exp, ofd, fid, &info->fti_attr,
1470                                         oa, objcount, npages, lnb,
1471                                         oa->o_grant_used, old_rc);
1472                 if (rc == 0)
1473                         obdo_from_la(oa, &info->fti_attr,
1474                                      OFD_VALID_FLAGS | LA_GID | LA_UID |
1475                                      LA_PROJID);
1476                 else
1477                         obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID |
1478                                      LA_PROJID);
1479
1480                 /* don't report overquota flag if we failed before reaching
1481                  * commit */
1482                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
1483                         /* return the overquota flags to client */
1484                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
1485                                 if (oa->o_valid & OBD_MD_FLFLAGS)
1486                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
1487                                 else
1488                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
1489                         }
1490
1491                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
1492                                 if (oa->o_valid & OBD_MD_FLFLAGS)
1493                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
1494                                 else
1495                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
1496                         }
1497                         if (lnb[0].lnb_flags & OBD_BRW_OVER_PRJQUOTA) {
1498                                 if (oa->o_valid & OBD_MD_FLFLAGS)
1499                                         oa->o_flags |= OBD_FL_NO_PRJQUOTA;
1500                                 else
1501                                         oa->o_flags = OBD_FL_NO_PRJQUOTA;
1502                         }
1503
1504                         oa->o_valid |= OBD_MD_FLFLAGS;
1505                         oa->o_valid |= OBD_MD_FLALLQUOTA;
1506                 }
1507
1508                 /**
1509                  * Update LVB after writing finish for server lock, see
1510                  * comments in ldlm_lock_decref_internal(), If this is a
1511                  * local lock on a server namespace and this was the last
1512                  * reference, lock will be destroyed directly thus there
1513                  * is no chance for ldlm_request_cancel() to update lvb.
1514                  */
1515                 if (rc == 0 && (rnb[0].rnb_flags & OBD_BRW_SRVLOCK)) {
1516                         ost_fid_build_resid(fid, &info->fti_resid);
1517                         rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
1518                                                LDLM_EXTENT, 0);
1519                         if (!IS_ERR(rs)) {
1520                                 ldlm_res_lvbo_update(rs, NULL, 1);
1521                                 ldlm_resource_putref(rs);
1522                         }
1523                 }
1524
1525                 /* Convert back to client IDs. LU-9671.
1526                  * nodemap_get_from_exp() may fail due to nodemap deactivated,
1527                  * server ID will be returned back to client in that case. */
1528                 nodemap = nodemap_get_from_exp(exp);
1529                 if (nodemap != NULL && !IS_ERR(nodemap)) {
1530                         oa->o_uid = nodemap_map_id(nodemap, NODEMAP_UID,
1531                                                    NODEMAP_FS_TO_CLIENT,
1532                                                    oa->o_uid);
1533                         oa->o_gid = nodemap_map_id(nodemap, NODEMAP_GID,
1534                                                    NODEMAP_FS_TO_CLIENT,
1535                                                    oa->o_gid);
1536                         nodemap_putref(nodemap);
1537                 }
1538         } else if (cmd == OBD_BRW_READ) {
1539                 rc = ofd_commitrw_read(env, ofd, fid, objcount,
1540                                        npages, lnb);
1541                 if (old_rc)
1542                         rc = old_rc;
1543         } else {
1544                 LBUG();
1545                 rc = -EPROTO;
1546         }
1547
1548         RETURN(rc);
1549 }