Whamcloud - gitweb
LU-6864 mdc: move RPC semaphore code to lustre/osp
[fs/lustre-release.git] / lustre / osp / osp_internal.h
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) 2007, 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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osp/osp_internal.h
33  *
34  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
35  */
36
37 #ifndef _OSP_INTERNAL_H
38 #define _OSP_INTERNAL_H
39
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <dt_object.h>
43 #include <md_object.h>
44 #include <lustre_fid.h>
45 #include <lustre_update.h>
46 #include <lu_target.h>
47
48 /*
49  * Infrastructure to support tracking of last committed llog record
50  */
51 struct osp_id_tracker {
52         spinlock_t               otr_lock;
53         __u64                    otr_next_id;
54         __u64                    otr_committed_id;
55         /* callback is register once per diskfs -- that's the whole point */
56         struct dt_txn_callback   otr_tx_cb;
57         /* single node can run many clusters */
58         struct list_head         otr_wakeup_list;
59         struct list_head         otr_list;
60         /* underlying shared device */
61         struct dt_device        *otr_dev;
62         /* how many users of this tracker */
63         atomic_t                 otr_refcount;
64 };
65
66 struct osp_precreate {
67         /*
68          * Precreation pool
69          */
70
71         /* last fid to assign in creation */
72         struct lu_fid                    osp_pre_used_fid;
73         /* last created id OST reported, next-created - available id's */
74         struct lu_fid                    osp_pre_last_created_fid;
75         /* how many ids are reserved in declare, we shouldn't block in create */
76         __u64                            osp_pre_reserved;
77         /* consumers (who needs new ids) wait here */
78         wait_queue_head_t                osp_pre_user_waitq;
79         /* current precreation status: working, failed, stopping? */
80         int                              osp_pre_status;
81         /* how many objects to precreate next time */
82         int                              osp_pre_create_count;
83         int                              osp_pre_min_create_count;
84         int                              osp_pre_max_create_count;
85         /* whether to increase precreation window next time or not */
86         int                              osp_pre_create_slow;
87         /* cleaning up orphans or recreating missing objects */
88         int                              osp_pre_recovering;
89 };
90
91 struct osp_update_request_sub {
92         struct object_update_request    *ours_req; /* may be vmalloc'd */
93         size_t                          ours_req_size;
94         /* Linked to osp_update_request->our_req_list */
95         struct list_head                ours_list;
96 };
97
98 struct osp_update_request {
99         int                             our_flags;
100         /* update request result */
101         int                             our_rc;
102
103         /* List of osp_update_request_sub */
104         struct list_head                our_req_list;
105         int                             our_req_nr;
106         int                             our_update_nr;
107
108         struct list_head                our_cb_items;
109         struct list_head                our_invalidate_cb_list;
110
111         /* points to thandle if this update request belongs to one */
112         struct osp_thandle              *our_th;
113
114         __u64                           our_version;
115         __u64                           our_generation;
116         /* protect our_list and flag */
117         spinlock_t                      our_list_lock;
118         /* linked to the list(ou_list) in osp_updates */
119         struct list_head                our_list;
120         __u32                           our_batchid;
121         __u32                           our_req_ready:1;
122
123 };
124
125 struct osp_updates {
126         struct list_head        ou_list;
127         spinlock_t              ou_lock;
128         wait_queue_head_t       ou_waitq;
129
130         /* The next rpc version which supposed to be sent in
131          * osp_send_update_thread().*/
132         __u64                   ou_rpc_version;
133
134         /* The rpc version assigned to the osp thandle during (osp_md_write()),
135          * which will be sent by this order. Note: the osp_thandle has be sent
136          * by this order to make sure the remote update log will follow the
137          * llog format rule. XXX: these probably should be removed once we
138          * invent new llog format */
139         __u64                   ou_version;
140
141         /* The generation of current osp update RPC, which is used to make sure
142          * those stale RPC(with older generation) will not be sent, otherwise it
143          * will cause update lllog corruption */
144         __u64                   ou_generation;
145 };
146
147 struct osp_rpc_lock {
148         /** Lock protecting in-flight RPC concurrency. */
149         struct mutex            rpcl_mutex;
150         /** Used for MDS/RPC load testing purposes. */
151         unsigned int            rpcl_fakes;
152 };
153
154 struct osp_device {
155         struct dt_device                 opd_dt_dev;
156         /* corresponded OST index */
157         int                              opd_index;
158
159         /* corrsponded MDT index, which will be used when connecting to OST
160          * for validating the connection (see ofd_parse_connect_data) */
161         int                              opd_group;
162         /* device used to store persistent state (llogs, last ids) */
163         struct obd_export               *opd_storage_exp;
164         struct dt_device                *opd_storage;
165         struct dt_object                *opd_last_used_oid_file;
166         struct dt_object                *opd_last_used_seq_file;
167
168         /* stored persistently in LE format, updated directly to/from disk
169          * and required le64_to_cpu() conversion before use.
170          * Protected by opd_pre_lock */
171         struct lu_fid                   opd_last_used_fid;
172         struct lu_fid                   opd_gap_start_fid;
173         int                              opd_gap_count;
174         /* connection to OST */
175         struct osp_rpc_lock              opd_rpc_lock;
176         struct obd_device               *opd_obd;
177         struct obd_export               *opd_exp;
178         struct obd_uuid                  opd_cluuid;
179         struct obd_connect_data         *opd_connect_data;
180         int                              opd_connects;
181         /* connection status. */
182         unsigned int                     opd_new_connection:1,
183                                          opd_got_disconnected:1,
184                                          opd_imp_connected:1,
185                                          opd_imp_active:1,
186                                          opd_imp_seen_connected:1,
187                                          opd_connect_mdt:1;
188
189         /* whether local recovery is completed:
190          * reported via ->ldo_recovery_complete() */
191         int                              opd_recovery_completed;
192
193         /* precreate structure for OSP */
194         struct osp_precreate            *opd_pre;
195         /* dedicate precreate thread */
196         struct ptlrpc_thread             opd_pre_thread;
197         spinlock_t                       opd_pre_lock;
198         /* thread waits for signals about pool going empty */
199         wait_queue_head_t                opd_pre_waitq;
200
201         /* send update thread */
202         struct osp_updates              *opd_update;
203         /* dedicate update thread */
204         struct ptlrpc_thread             opd_update_thread;
205
206         /*
207          * OST synchronization thread
208          */
209         spinlock_t                       opd_sync_lock;
210         /* unique generation, to recognize start of new records in the llog */
211         struct llog_gen                  opd_sync_generation;
212         /* number of changes to sync, used to wake up sync thread */
213         atomic_t                         opd_sync_changes;
214         /* processing of changes from previous mount is done? */
215         int                              opd_sync_prev_done;
216         /* found records */
217         struct ptlrpc_thread             opd_sync_thread;
218         wait_queue_head_t                opd_sync_waitq;
219         /* list of in flight rpcs */
220         struct list_head                 opd_sync_in_flight_list;
221         /* list of remotely committed rpc */
222         struct list_head                 opd_sync_committed_there;
223         /* number of RPCs in flight - flow control */
224         atomic_t                         opd_sync_rpcs_in_flight;
225         int                              opd_sync_max_rpcs_in_flight;
226         /* number of RPC in processing (including non-committed by OST) */
227         atomic_t                         opd_sync_rpcs_in_progress;
228         int                              opd_sync_max_rpcs_in_progress;
229         /* osd api's commit cb control structure */
230         struct dt_txn_callback           opd_sync_txn_cb;
231         /* last used change number -- semantically similar to transno */
232         unsigned long                    opd_sync_last_used_id;
233         /* last committed change number -- semantically similar to
234          * last_committed */
235         __u64                            opd_sync_last_committed_id;
236         /* last processed catalog index */
237         int                              opd_sync_last_catalog_idx;
238         /* number of processed records */
239         atomic64_t                       opd_sync_processed_recs;
240         /* stop processing new requests until barrier=0 */
241         atomic_t                         opd_sync_barrier;
242         wait_queue_head_t                opd_sync_barrier_waitq;
243         /* last generated id */
244         ktime_t                          opd_sync_next_commit_cb;
245         atomic_t                         opd_commits_registered;
246
247         /*
248          * statfs related fields: OSP maintains it on its own
249          */
250         struct obd_statfs                opd_statfs;
251         ktime_t                          opd_statfs_fresh_till;
252         struct timer_list                opd_statfs_timer;
253         int                              opd_statfs_update_in_progress;
254         /* how often to update statfs data */
255         time64_t                         opd_statfs_maxage;
256
257         struct dentry                   *opd_debugfs;
258
259         /* If the caller wants to do some idempotent async operations on
260          * remote server, it can append the async remote requests on the
261          * osp_device::opd_async_requests via declare() functions, these
262          * requests can be packed together and sent to the remote server
263          * via single OUT RPC later. */
264         struct osp_update_request       *opd_async_requests;
265         /* Protect current operations on opd_async_requests. */
266         struct mutex                     opd_async_requests_mutex;
267         struct list_head                 opd_async_updates;
268         struct rw_semaphore              opd_async_updates_rwsem;
269         atomic_t                         opd_async_updates_count;
270
271         /*
272          * Limit the object allocation using ENOSPC for opd_pre_status
273          */
274         int                             opd_reserved_mb_high;
275         int                             opd_reserved_mb_low;
276 };
277
278 #define opd_pre_used_fid                opd_pre->osp_pre_used_fid
279 #define opd_pre_last_created_fid        opd_pre->osp_pre_last_created_fid
280 #define opd_pre_reserved                opd_pre->osp_pre_reserved
281 #define opd_pre_user_waitq              opd_pre->osp_pre_user_waitq
282 #define opd_pre_status                  opd_pre->osp_pre_status
283 #define opd_pre_create_count            opd_pre->osp_pre_create_count
284 #define opd_pre_min_create_count        opd_pre->osp_pre_min_create_count
285 #define opd_pre_max_create_count        opd_pre->osp_pre_max_create_count
286 #define opd_pre_create_slow             opd_pre->osp_pre_create_slow
287 #define opd_pre_recovering              opd_pre->osp_pre_recovering
288
289 extern struct kmem_cache *osp_object_kmem;
290
291 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
292  * The left part is for value, binary mode. */
293 struct osp_xattr_entry {
294         struct list_head         oxe_list;
295         atomic_t                 oxe_ref;
296         void                    *oxe_value;
297         size_t                   oxe_buflen;
298         size_t                   oxe_namelen;
299         size_t                   oxe_vallen;
300         unsigned int             oxe_exist:1,
301                                  oxe_ready:1;
302         char                     oxe_buf[0];
303 };
304
305 /* this is a top object */
306 struct osp_object {
307         struct lu_object_header opo_header;
308         struct dt_object        opo_obj;
309         unsigned int            opo_reserved:1,
310                                 opo_non_exist:1,
311                                 opo_stale:1;
312
313         /* read/write lock for md osp object */
314         struct rw_semaphore     opo_sem;
315         const struct lu_env     *opo_owner;
316         struct lu_attr          opo_attr;
317         struct list_head        opo_xattr_list;
318         struct list_head        opo_invalidate_cb_list;
319         /* Protect opo_ooa. */
320         spinlock_t              opo_lock;
321 };
322
323 extern struct lu_object_operations osp_lu_obj_ops;
324 extern const struct dt_device_operations osp_dt_ops;
325 extern struct dt_object_operations osp_md_obj_ops;
326 extern struct dt_body_operations osp_md_body_ops;
327
328 struct osp_thread_info {
329         struct lu_buf            osi_lb;
330         struct lu_buf            osi_lb2;
331         struct lu_fid            osi_fid;
332         struct lu_attr           osi_attr;
333         struct ost_id            osi_oi;
334         struct ost_id            osi_oi2;
335         u64                      osi_id;
336         loff_t                   osi_off;
337         union {
338                 struct llog_rec_hdr             osi_hdr;
339                 struct llog_unlink64_rec        osi_unlink;
340                 struct llog_setattr64_rec_v2    osi_setattr;
341                 struct llog_gen_rec             osi_gen;
342         };
343         struct llog_cookie       osi_cookie;
344         struct llog_catid        osi_cid;
345         struct lu_seq_range      osi_seq;
346         struct ldlm_res_id       osi_resid;
347         struct obdo              osi_obdo;
348 };
349
350 /* Iterator for OSP */
351 struct osp_it {
352         __u32                     ooi_pos_page;
353         __u32                     ooi_pos_lu_page;
354         __u32                     ooi_attr;
355         __u32                     ooi_rec_size;
356         int                       ooi_pos_ent;
357         int                       ooi_total_npages;
358         int                       ooi_valid_npages;
359         unsigned int              ooi_swab:1;
360         __u64                     ooi_next;
361         struct dt_object         *ooi_obj;
362         void                     *ooi_ent;
363         struct page              *ooi_cur_page;
364         struct lu_idxpage        *ooi_cur_idxpage;
365         struct page              **ooi_pages;
366 };
367
368 #define OSP_THANDLE_MAGIC       0x20141214
369 struct osp_thandle {
370         struct thandle           ot_super;
371
372         /* OSP will use this thandle to update last oid*/
373         struct thandle          *ot_storage_th;
374         __u32                    ot_magic;
375         struct list_head         ot_commit_dcb_list;
376         struct list_head         ot_stop_dcb_list;
377         struct osp_update_request *ot_our;
378         atomic_t                 ot_refcount;
379 };
380
381 static inline struct osp_thandle *
382 thandle_to_osp_thandle(struct thandle *th)
383 {
384         return container_of(th, struct osp_thandle, ot_super);
385 }
386
387 static inline struct osp_update_request *
388 thandle_to_osp_update_request(struct thandle *th)
389 {
390         struct osp_thandle *oth;
391
392         oth = thandle_to_osp_thandle(th);
393         return oth->ot_our;
394 }
395
396 /* The transaction only include the updates on the remote node, and
397  * no local updates at all */
398 static inline bool is_only_remote_trans(struct thandle *th)
399 {
400         return th->th_top == NULL;
401 }
402
403 static inline void osp_objid_buf_prep(struct lu_buf *buf, loff_t *off,
404                                       __u64 *id, int index)
405 {
406         /* Note: through id is only 32 bits, it will also write 64 bits
407          * for oid to keep compatibility with the previous version. */
408         buf->lb_buf = (void *)id;
409         buf->lb_len = sizeof(u64);
410         *off = sizeof(u64) * index;
411 }
412
413 static inline void osp_objseq_buf_prep(struct lu_buf *buf, loff_t *off,
414                                        __u64 *seq, int index)
415 {
416         buf->lb_buf = (void *)seq;
417         buf->lb_len = sizeof(u64);
418         *off = sizeof(u64) * index;
419 }
420
421 static inline void osp_buf_prep(struct lu_buf *lb, void *buf, int buf_len)
422 {
423         lb->lb_buf = buf;
424         lb->lb_len = buf_len;
425 }
426
427 extern struct lu_context_key osp_thread_key;
428
429 static inline struct osp_thread_info *osp_env_info(const struct lu_env *env)
430 {
431         struct osp_thread_info *info;
432
433         info = lu_context_key_get(&env->le_ctx, &osp_thread_key);
434         if (info == NULL) {
435                 lu_env_refill((struct lu_env *)env);
436                 info = lu_context_key_get(&env->le_ctx, &osp_thread_key);
437         }
438         LASSERT(info);
439         return info;
440 }
441
442 struct osp_txn_info {
443         __u64   oti_current_id;
444 };
445
446 extern struct lu_context_key osp_txn_key;
447
448 static inline struct osp_txn_info *osp_txn_info(struct lu_context *ctx)
449 {
450         struct osp_txn_info *info;
451
452         info = lu_context_key_get(ctx, &osp_txn_key);
453         return info;
454 }
455
456 extern const struct lu_device_operations osp_lu_ops;
457
458 static inline int lu_device_is_osp(struct lu_device *d)
459 {
460         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osp_lu_ops);
461 }
462
463 static inline struct osp_device *lu2osp_dev(struct lu_device *d)
464 {
465         LASSERT(lu_device_is_osp(d));
466         return container_of0(d, struct osp_device, opd_dt_dev.dd_lu_dev);
467 }
468
469 static inline struct lu_device *osp2lu_dev(struct osp_device *d)
470 {
471         return &d->opd_dt_dev.dd_lu_dev;
472 }
473
474 static inline struct osp_device *dt2osp_dev(struct dt_device *d)
475 {
476         LASSERT(lu_device_is_osp(&d->dd_lu_dev));
477         return container_of0(d, struct osp_device, opd_dt_dev);
478 }
479
480 static inline struct osp_object *lu2osp_obj(struct lu_object *o)
481 {
482         LASSERT(ergo(o != NULL, lu_device_is_osp(o->lo_dev)));
483         return container_of0(o, struct osp_object, opo_obj.do_lu);
484 }
485
486 static inline struct lu_object *osp2lu_obj(struct osp_object *obj)
487 {
488         return &obj->opo_obj.do_lu;
489 }
490
491 static inline struct osp_object *osp_obj(const struct lu_object *o)
492 {
493         LASSERT(lu_device_is_osp(o->lo_dev));
494         return container_of0(o, struct osp_object, opo_obj.do_lu);
495 }
496
497 static inline struct osp_object *dt2osp_obj(const struct dt_object *d)
498 {
499         return osp_obj(&d->do_lu);
500 }
501
502 static inline struct dt_object *osp_object_child(struct osp_object *o)
503 {
504         return container_of0(lu_object_next(osp2lu_obj(o)),
505                              struct dt_object, do_lu);
506 }
507
508 static inline struct seq_server_site *osp_seq_site(struct osp_device *osp)
509 {
510         return osp->opd_dt_dev.dd_lu_dev.ld_site->ld_seq_site;
511 }
512
513 /**
514  * Serializes in-flight MDT-modifying RPC requests to preserve idempotency.
515  *
516  * This mutex is used to implement execute-once semantics on the MDT.
517  * The MDT stores the last transaction ID and result for every client in
518  * its last_rcvd file. If the client doesn't get a reply, it can safely
519  * resend the request and the MDT will reconstruct the reply being aware
520  * that the request has already been executed. Without this lock,
521  * execution status of concurrent in-flight requests would be
522  * overwritten.
523  *
524  * This imlpementation limits the extent to which we can keep a full pipeline
525  * of in-flight requests from a single client.  This limitation can be
526  * overcome by allowing multiple slots per client in the last_rcvd file,
527  * see LU-6864.
528  */
529 #define OSP_FAKE_RPCL_IT ((void *)0x2c0012bfUL)
530
531 static inline void osp_init_rpc_lock(struct osp_device *osp)
532 {
533         struct osp_rpc_lock *lck = &osp->opd_rpc_lock;
534
535         mutex_init(&lck->rpcl_mutex);
536         lck->rpcl_fakes = 0;
537 }
538
539 static inline void osp_get_rpc_lock(struct osp_device *osp)
540 {
541         struct osp_rpc_lock *lck = &osp->opd_rpc_lock;
542
543         /* This would normally block until the existing request finishes.
544          * If fail_loc is set it will block until the regular request is
545          * done, then increment rpcl_fakes.  Once that is non-zero it
546          * will only be cleared when all fake requests are finished.
547          * Only when all fake requests are finished can normal requests
548          * be sent, to ensure they are recoverable again.
549          */
550  again:
551         mutex_lock(&lck->rpcl_mutex);
552
553         if (CFS_FAIL_CHECK_QUIET(OBD_FAIL_MDC_RPCS_SEM) ||
554             CFS_FAIL_CHECK_QUIET(OBD_FAIL_OSP_RPCS_SEM)) {
555                 lck->rpcl_fakes++;
556                 mutex_unlock(&lck->rpcl_mutex);
557
558                 return;
559         }
560
561         /* This will only happen when the CFS_FAIL_CHECK() was just turned
562          * off but there are still requests in progress.  Wait until they
563          * finish.  It doesn't need to be efficient in this extremely rare
564          * case, just have low overhead in the common case when it isn't true.
565          */
566         if (unlikely(lck->rpcl_fakes)) {
567                 mutex_unlock(&lck->rpcl_mutex);
568                 schedule_timeout(cfs_time_seconds(1) / 4);
569
570                 goto again;
571         }
572 }
573
574 static inline void osp_put_rpc_lock(struct osp_device *osp)
575 {
576         struct osp_rpc_lock *lck = &osp->opd_rpc_lock;
577
578         if (lck->rpcl_fakes) { /* OBD_FAIL_OSP_RPCS_SEM */
579                 mutex_lock(&lck->rpcl_mutex);
580
581                 if (lck->rpcl_fakes) /* check again under lock */
582                         lck->rpcl_fakes--;
583         }
584
585         mutex_unlock(&lck->rpcl_mutex);
586 }
587
588 static inline int osp_fid_diff(const struct lu_fid *fid1,
589                                const struct lu_fid *fid2)
590 {
591         /* In 2.6+ ost_idx is packed into IDIF FID, while in 2.4 and 2.5 IDIF
592          * is always FID_SEQ_IDIF(0x100000000ULL), which does not include OST
593          * index in the seq. So we can not compare IDIF FID seq here */
594         if (fid_is_idif(fid1) && fid_is_idif(fid2)) {
595                 __u32 ost_idx1 = fid_idif_ost_idx(fid1);
596                 __u32 ost_idx2 = fid_idif_ost_idx(fid2);
597
598                 LASSERTF(ost_idx1 == 0 || ost_idx2 == 0 || ost_idx1 == ost_idx2,
599                          "fid1: "DFID", fid2: "DFID"\n", PFID(fid1),
600                          PFID(fid2));
601
602                 return fid_idif_id(fid1->f_seq, fid1->f_oid, 0) -
603                        fid_idif_id(fid2->f_seq, fid2->f_oid, 0);
604         }
605
606         LASSERTF(fid_seq(fid1) == fid_seq(fid2), "fid1:"DFID
607                  ", fid2:"DFID"\n", PFID(fid1), PFID(fid2));
608
609         return fid_oid(fid1) - fid_oid(fid2);
610 }
611
612
613 static inline void osp_update_last_fid(struct osp_device *d, struct lu_fid *fid)
614 {
615         int diff = osp_fid_diff(fid, &d->opd_last_used_fid);
616         struct lu_fid *gap_start = &d->opd_gap_start_fid;
617         /*
618          * we might have lost precreated objects due to VBR and precreate
619          * orphans, the gap in objid can be calculated properly only here
620          */
621         if (diff > 0) {
622                 if (diff > 1) {
623                         d->opd_gap_start_fid = d->opd_last_used_fid;
624                         if (fid_oid(gap_start) == LUSTRE_DATA_SEQ_MAX_WIDTH) {
625                                 gap_start->f_seq++;
626                                 gap_start->f_oid = fid_is_idif(gap_start) ?
627                                                                0 : 1;
628                         } else {
629                                 gap_start->f_oid++;
630                         }
631                         d->opd_gap_count = diff - 1;
632                         CDEBUG(D_HA, "Gap in objids: start="DFID", count =%d\n",
633                                PFID(&d->opd_gap_start_fid), d->opd_gap_count);
634                 }
635                 d->opd_last_used_fid = *fid;
636         }
637 }
638
639 static int osp_fid_end_seq(const struct lu_env *env, struct lu_fid *fid)
640 {
641         if (fid_is_idif(fid)) {
642                 struct osp_thread_info *info = osp_env_info(env);
643                 struct ost_id *oi = &info->osi_oi;
644
645                 fid_to_ostid(fid, oi);
646                 return ostid_id(oi) == IDIF_MAX_OID;
647         } else {
648                 return fid_oid(fid) == LUSTRE_DATA_SEQ_MAX_WIDTH;
649         }
650 }
651
652 static inline int osp_precreate_end_seq_nolock(const struct lu_env *env,
653                                                struct osp_device *osp)
654 {
655         struct lu_fid *fid = &osp->opd_pre_last_created_fid;
656
657         return osp_fid_end_seq(env, fid);
658 }
659
660 static inline int osp_precreate_end_seq(const struct lu_env *env,
661                                         struct osp_device *osp)
662 {
663         int rc;
664
665         spin_lock(&osp->opd_pre_lock);
666         rc = osp_precreate_end_seq_nolock(env, osp);
667         spin_unlock(&osp->opd_pre_lock);
668         return rc;
669 }
670
671 static inline int osp_is_fid_client(struct osp_device *osp)
672 {
673         struct obd_import *imp = osp->opd_obd->u.cli.cl_import;
674
675         return imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_FID;
676 }
677
678 struct object_update *
679 update_buffer_get_update(struct object_update_request *request,
680                          unsigned int index);
681
682 int osp_extend_update_buffer(const struct lu_env *env,
683                              struct osp_update_request *our);
684
685 struct osp_update_request_sub *
686 osp_current_object_update_request(struct osp_update_request *our);
687
688 int osp_object_update_request_create(struct osp_update_request *our,
689                                      size_t size);
690
691 #define OSP_UPDATE_RPC_PACK(env, out_something_pack, our, ...)          \
692 ({                                                                      \
693         struct object_update *object_update;                            \
694         size_t max_update_length;                                       \
695         struct osp_update_request_sub *ours;                            \
696         int ret;                                                        \
697                                                                         \
698         while (1) {                                                     \
699                 ours = osp_current_object_update_request(our);          \
700                 LASSERT(ours != NULL);                                  \
701                 max_update_length = ours->ours_req_size -               \
702                             object_update_request_size(ours->ours_req); \
703                                                                         \
704                 object_update = update_buffer_get_update(ours->ours_req,\
705                                          ours->ours_req->ourq_count);   \
706                 ret = out_something_pack(env, object_update,            \
707                                          &max_update_length,            \
708                                          __VA_ARGS__);                  \
709                 if (ret == -E2BIG) {                                    \
710                         int rc1;                                        \
711                         /* Create new object update request */          \
712                         rc1 = osp_object_update_request_create(our,     \
713                                 max_update_length  +                    \
714                                 offsetof(struct object_update_request,  \
715                                          ourq_updates[0]) + 1);         \
716                         if (rc1 != 0) {                                 \
717                                 ret = rc1;                              \
718                                 break;                                  \
719                         }                                               \
720                         continue;                                       \
721                 } else {                                                \
722                         if (ret == 0) {                                 \
723                                 ours->ours_req->ourq_count++;           \
724                                 (our)->our_update_nr++;                 \
725                                 object_update->ou_batchid =             \
726                                                      (our)->our_batchid;\
727                                 object_update->ou_flags |=              \
728                                                      (our)->our_flags;  \
729                         }                                               \
730                         break;                                          \
731                 }                                                       \
732         }                                                               \
733         ret;                                                            \
734 })
735
736 static inline bool osp_send_update_thread_running(struct osp_device *osp)
737 {
738         return osp->opd_update_thread.t_flags & SVC_RUNNING;
739 }
740
741 static inline bool osp_send_update_thread_stopped(struct osp_device *osp)
742 {
743         return osp->opd_update_thread.t_flags & SVC_STOPPED;
744 }
745
746 typedef int (*osp_update_interpreter_t)(const struct lu_env *env,
747                                         struct object_update_reply *rep,
748                                         struct ptlrpc_request *req,
749                                         struct osp_object *obj,
750                                         void *data, int index, int rc);
751
752 /* osp_dev.c */
753 void osp_update_last_id(struct osp_device *d, u64 objid);
754
755 /* osp_trans.c */
756 int osp_insert_async_request(const struct lu_env *env, enum update_type op,
757                              struct osp_object *obj, int count, __u16 *lens,
758                              const void **bufs, void *data, __u32 repsize,
759                              osp_update_interpreter_t interpreter);
760
761 int osp_unplug_async_request(const struct lu_env *env,
762                              struct osp_device *osp,
763                              struct osp_update_request *update);
764 int osp_trans_update_request_create(struct thandle *th);
765 struct thandle *osp_trans_create(const struct lu_env *env,
766                                  struct dt_device *d);
767 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
768                     struct thandle *th);
769 int osp_insert_update_callback(const struct lu_env *env,
770                                struct osp_update_request *update,
771                                struct osp_object *obj, void *data,
772                                osp_update_interpreter_t interpreter);
773
774 struct osp_update_request *osp_update_request_create(struct dt_device *dt);
775 void osp_update_request_destroy(const struct lu_env *env,
776                                 struct osp_update_request *update);
777
778 int osp_send_update_thread(void *arg);
779 int osp_check_and_set_rpc_version(struct osp_thandle *oth,
780                                   struct osp_object *obj);
781
782 void osp_thandle_destroy(const struct lu_env *env, struct osp_thandle *oth);
783 static inline void osp_thandle_get(struct osp_thandle *oth)
784 {
785         atomic_inc(&oth->ot_refcount);
786 }
787
788 static inline void osp_thandle_put(const struct lu_env *env,
789                                    struct osp_thandle *oth)
790 {
791         if (atomic_dec_and_test(&oth->ot_refcount))
792                 osp_thandle_destroy(env, oth);
793 }
794
795 int osp_prep_update_req(const struct lu_env *env, struct obd_import *imp,
796                         struct osp_update_request *our,
797                         struct ptlrpc_request **reqp);
798 int osp_remote_sync(const struct lu_env *env, struct osp_device *osp,
799                     struct osp_update_request *update,
800                     struct ptlrpc_request **reqp);
801
802 struct thandle *osp_get_storage_thandle(const struct lu_env *env,
803                                         struct thandle *th,
804                                         struct osp_device *osp);
805 void osp_trans_callback(const struct lu_env *env,
806                         struct osp_thandle *oth, int rc);
807 void osp_invalidate_request(struct osp_device *osp);
808 /* osp_object.c */
809 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
810                  struct lu_attr *attr);
811 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
812                   struct lu_buf *buf, const char *name);
813 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
814                           const struct lu_buf *buf, const char *name,
815                           int flag, struct thandle *th);
816 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
817                   const struct lu_buf *buf, const char *name, int fl,
818                   struct thandle *th);
819 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
820                           const char *name, struct thandle *th);
821 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
822                   const char *name, struct thandle *th);
823 int osp_invalidate(const struct lu_env *env, struct dt_object *dt);
824 void osp_obj_invalidate_cache(struct osp_object *obj);
825
826 int osp_trans_stop(const struct lu_env *env, struct dt_device *dt,
827                    struct thandle *th);
828 int osp_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb);
829
830 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
831                           __u32 attr);
832 void osp_it_fini(const struct lu_env *env, struct dt_it *di);
833 int osp_it_get(const struct lu_env *env, struct dt_it *di,
834                const struct dt_key *key);
835 void osp_it_put(const struct lu_env *env, struct dt_it *di);
836 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di);
837 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
838                    void *key_rec);
839 int osp_it_next_page(const struct lu_env *env, struct dt_it *di);
840 /* osp_md_object.c */
841 int osp_md_declare_create(const struct lu_env *env, struct dt_object *dt,
842                           struct lu_attr *attr, struct dt_allocation_hint *hint,
843                           struct dt_object_format *dof, struct thandle *th);
844 int osp_md_create(const struct lu_env *env, struct dt_object *dt,
845                   struct lu_attr *attr, struct dt_allocation_hint *hint,
846                   struct dt_object_format *dof, struct thandle *th);
847 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
848                             const struct lu_attr *attr, struct thandle *th);
849 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
850                     const struct lu_attr *attr, struct thandle *th);
851 extern const struct dt_index_operations osp_md_index_ops;
852
853 /* osp_precreate.c */
854 int osp_init_precreate(struct osp_device *d);
855 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d);
856 __u64 osp_precreate_get_id(struct osp_device *d);
857 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
858                           struct lu_fid *fid);
859 void osp_precreate_fini(struct osp_device *d);
860 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt, __u64);
861 void osp_pre_update_status(struct osp_device *d, int rc);
862 void osp_statfs_need_now(struct osp_device *d);
863 int osp_reset_last_used(const struct lu_env *env, struct osp_device *osp);
864 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
865                                  struct lu_fid *fid, int sync);
866 int osp_init_pre_fid(struct osp_device *osp);
867 int osp_init_statfs(struct osp_device *osp);
868 void osp_fini_statfs(struct osp_device *osp);
869 void osp_statfs_fini(struct osp_device *d);
870
871 /* lproc_osp.c */
872 void osp_tunables_init(struct osp_device *osp);
873 void osp_tunables_fini(struct osp_device *osp);
874
875 /* osp_sync.c */
876 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
877                          enum llog_op_type type, struct thandle *th);
878 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
879                  enum llog_op_type type, struct thandle *th,
880                  const struct lu_attr *attr);
881 int osp_sync_init(const struct lu_env *env, struct osp_device *d);
882 int osp_sync_fini(struct osp_device *d);
883 void osp_sync_check_for_work(struct osp_device *osp);
884 void osp_sync_force(const struct lu_env *env, struct osp_device *d);
885 int osp_sync_add_commit_cb_1s(const struct lu_env *env, struct osp_device *d,
886                               struct thandle *th);
887
888 /* lwp_dev.c */
889 extern struct obd_ops lwp_obd_device_ops;
890 extern struct lu_device_type lwp_device_type;
891
892 static inline struct lu_device *osp2top(const struct osp_device *osp)
893 {
894         return osp->opd_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
895 }
896
897 static inline void osp_set_req_replay(const struct osp_device *osp,
898                                       struct ptlrpc_request *req)
899 {
900         struct obd_device *obd = osp2top(osp)->ld_obd;
901
902         /* The RPC must be recovery related for the cases:
903          *
904          * 1. sent during recovery, or
905          * 2. sent before the recovery thread target_recovery_thread() start,
906          *    such as triggered by lod_sub_recovery_thread(). */
907         if (obd->obd_recovering || (obd->obd_replayable && obd->obd_no_conn))
908                 req->rq_allow_replay = 1;
909 }
910
911 #endif