Whamcloud - gitweb
LU-10070 lod: SEL: Implement basic spillover space
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_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) 2011, 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/osd/osd_internal.h
33  *
34  * Shared definitions and declarations for osd module
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #ifndef _OSD_INTERNAL_H
40 #define _OSD_INTERNAL_H
41
42
43 /* struct mutex */
44 #include <linux/mutex.h>
45 /* struct rw_semaphore */
46 #include <linux/rwsem.h>
47 /* struct dentry */
48 #include <linux/dcache.h>
49 /* struct dirent64 */
50 #include <linux/dirent.h>
51 #include <linux/statfs.h>
52 #include <linux/bio.h>
53 #include <ldiskfs/ldiskfs.h>
54 #include <ldiskfs/ldiskfs_jbd2.h>
55
56 /* LUSTRE_OSD_NAME */
57 #include <obd.h>
58 /* class_register_type(), class_unregister_type(), class_get_type() */
59 #include <obd_class.h>
60 #include <lustre_disk.h>
61 #include <dt_object.h>
62 #include <lustre_quota.h>
63
64 #include "osd_oi.h"
65 #include "osd_iam.h"
66 #include "osd_scrub.h"
67 #include "osd_quota_fmt.h"
68
69 struct inode;
70 extern struct kmem_cache *dynlock_cachep;
71
72 #define OSD_COUNTERS (0)
73
74 /* ldiskfs special inode::i_state_flags need to be accessed with
75  * ldiskfs_{set,clear,test}_inode_state() only */
76
77 /* OI scrub should skip this inode. */
78 #define LDISKFS_STATE_LUSTRE_NOSCRUB    31
79 #define LDISKFS_STATE_LUSTRE_DESTROY    30
80
81 /** Enable thandle usage statistics */
82 #define OSD_THANDLE_STATS (0)
83
84 #define MAX_OBJID_GROUP (FID_SEQ_ECHO + 1)
85
86 #define OBJECTS         "OBJECTS"
87 #define ADMIN_USR       "admin_quotafile_v2.usr"
88 #define ADMIN_GRP       "admin_quotafile_v2.grp"
89
90 /* Statfs space reservation for fragmentation and local objects */
91 #define OSD_STATFS_RESERVED             (1ULL << 23) /* 8MB */
92 #define OSD_STATFS_RESERVED_SHIFT       (7) /* reserve 0.78% of all space */
93
94 /* check if ldiskfs support project quota */
95 #ifndef LDISKFS_IOC_FSSETXATTR
96 #undef HAVE_PROJECT_QUOTA
97 #endif
98
99 #define OBD_BRW_MAPPED  OBD_BRW_LOCAL1
100
101 struct osd_directory {
102         struct iam_container od_container;
103         struct iam_descr     od_descr;
104 };
105
106 /*
107  * Object Index (oi) instance.
108  */
109 struct osd_oi {
110         /*
111          * underlying index object, where fid->id mapping in stored.
112          */
113         struct inode         *oi_inode;
114         struct osd_directory  oi_dir;
115 };
116
117 extern const int osd_dto_credits_noquota[];
118
119 struct osd_object {
120         struct dt_object        oo_dt;
121         /**
122          * Inode for file system object represented by this osd_object. This
123          * inode is pinned for the whole duration of lu_object life.
124          *
125          * Not modified concurrently (either setup early during object
126          * creation, or assigned by osd_create() under write lock).
127          */
128         struct inode           *oo_inode;
129         /**
130          * to protect index ops.
131          */
132         struct htree_lock_head *oo_hl_head;
133         struct rw_semaphore     oo_ext_idx_sem;
134         struct rw_semaphore     oo_sem;
135         struct osd_directory    *oo_dir;
136         /** protects inode attributes. */
137         spinlock_t              oo_guard;
138
139         /**
140          * Following two members *compat_dot* are used to indicate
141          * the presence of dot and dotdot in the given directory.
142          * This is required for interop mode (b11826).
143          */
144         __u32                   oo_destroyed:1,
145                                 oo_pfid_in_lma:1,
146                                 oo_compat_dot_created:1,
147                                 oo_compat_dotdot_created:1;
148
149         /* the i_flags in LMA */
150         __u32                   oo_lma_flags;
151
152         const struct lu_env    *oo_owner;
153
154         struct list_head        oo_xattr_list;
155         struct lu_object_header *oo_header;
156 };
157
158 struct osd_obj_seq {
159         /* protects on-fly initialization */
160         int              oos_subdir_count; /* subdir count for each seq */
161         struct dentry    *oos_root;        /* O/<seq> */
162         struct dentry    **oos_dirs;       /* O/<seq>/d0-dXX */
163         u64              oos_seq;          /* seq number */
164         struct list_head oos_seq_list;     /* list to seq_list */
165 };
166
167 struct osd_obj_map {
168         struct dentry    *om_root;        /* dentry for /O */
169         rwlock_t         om_seq_list_lock; /* lock for seq_list */
170         struct list_head om_seq_list;      /* list head for seq */
171         int              om_subdir_count;
172         struct mutex     om_dir_init_mutex;
173 };
174
175 struct osd_mdobj {
176         struct dentry   *om_root;      /* AGENT/<index> */
177         u64              om_index;     /* mdt index */
178         struct list_head om_list;      /* list to omm_list */
179 };
180
181 struct osd_mdobj_map {
182         struct dentry   *omm_remote_parent;
183 };
184 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
185                           handle_t *handle, struct dentry *child,
186                           struct inode *inode, struct htree_lock *hlock);
187
188 #define OSD_OTABLE_IT_CACHE_SIZE        64
189 #define OSD_OTABLE_IT_CACHE_MASK        (~(OSD_OTABLE_IT_CACHE_SIZE - 1))
190
191 struct osd_inconsistent_item {
192         /* link into lustre_scrub::os_inconsistent_items,
193          * protected by lustre_scrub::os_lock. */
194         struct list_head       oii_list;
195
196         /* The right FID <=> ino#/gen mapping. */
197         struct osd_idmap_cache oii_cache;
198
199         unsigned int           oii_insert:1; /* insert or update mapping. */
200 };
201
202 struct osd_otable_cache {
203         struct osd_idmap_cache ooc_cache[OSD_OTABLE_IT_CACHE_SIZE];
204
205         /* Index for next cache slot to be filled. */
206         int                    ooc_producer_idx;
207
208         /* Index for next cache slot to be returned by it::next(). */
209         int                    ooc_consumer_idx;
210
211         /* How many items in ooc_cache. */
212         __u64                  ooc_cached_items;
213
214         /* Position for up layer LFSCK iteration pre-loading. */
215         __u64                  ooc_pos_preload;
216 };
217
218 struct osd_otable_it {
219         struct osd_device       *ooi_dev;
220         struct osd_otable_cache  ooi_cache;
221         struct osd_iit_param     ooi_iit_param;
222
223         /* The following bits can be updated/checked w/o lock protection.
224          * If more bits will be introduced in the future and need lock to
225          * protect, please add comment. */
226         unsigned long            ooi_used_outside:1, /* Some user out of OSD
227                                                       * uses the iteration. */
228                                  ooi_all_cached:1, /* No more entries can be
229                                                     * filled into cache. */
230                                  ooi_user_ready:1, /* The user out of OSD is
231                                                     * ready to iterate. */
232                                  ooi_waiting:1; /* it::next is waiting. */
233 };
234
235 struct osd_obj_orphan {
236         struct list_head oor_list;
237         struct lu_env   *oor_env; /* to identify "own" records */
238         __u32 oor_ino;
239 };
240
241 enum osd_t10_type {
242         OSD_T10_TYPE_UNKNOWN = 0,
243         OSD_T10_TYPE1_CRC,
244         OSD_T10_TYPE3_CRC,
245         OSD_T10_TYPE1_IP,
246         OSD_T10_TYPE3_IP
247 };
248
249 /*
250  * osd device.
251  */
252 struct osd_device {
253         /* super-class */
254         struct dt_device          od_dt_dev;
255         /* information about underlying file system */
256         struct vfsmount          *od_mnt;
257         /* object index */
258         struct osd_oi           **od_oi_table;
259         /* total number of OI containers */
260         int                       od_oi_count;
261         /*
262          * Fid Capability
263          */
264         unsigned int              od_fl_capa:1,
265                                   od_maybe_new:1,
266                                   od_igif_inoi:1,
267                                   od_check_ff:1,
268                                   od_is_ost:1,
269                                   od_in_init:1,
270                                   od_index_in_idif:1,
271         /* Other flags */
272                                   od_nonrotational:1;
273
274         __s64                     od_auto_scrub_interval;
275         __u32                     od_dirent_journal;
276         int                       od_index;
277         struct proc_dir_entry    *od_proc_entry;
278         struct lprocfs_stats     *od_stats;
279
280         spinlock_t                od_osfs_lock;
281
282         int                       od_connects;
283         struct lu_site            od_site;
284
285         struct osd_obj_map      *od_ost_map;
286         struct osd_mdobj_map    *od_mdt_map;
287
288         unsigned long long      od_readcache_max_filesize;
289         int                     od_read_cache;
290         int                     od_writethrough_cache;
291
292         struct brw_stats        od_brw_stats;
293         atomic_t                od_r_in_flight;
294         atomic_t                od_w_in_flight;
295
296         struct mutex              od_otable_mutex;
297         struct osd_otable_it     *od_otable_it;
298         struct osd_scrub          od_scrub;
299         struct list_head                  od_ios_list;
300
301         /* service name associated with the osd device */
302         char                      od_svname[MAX_OBD_NAME];
303         char                      od_mntdev[MAX_OBD_NAME];
304         uuid_t                  od_uuid;
305
306         /* quota slave instance for inode */
307         struct qsd_instance      *od_quota_slave_md;
308
309         /* quota slave instance for block */
310         struct qsd_instance      *od_quota_slave_dt;
311
312         /* osd seq instance */
313         struct lu_client_seq    *od_cl_seq;
314         /* If the ratio of "the total OI mappings count" vs
315          * "the bad OI mappings count" is lower than the
316          * osd_device::od_full_scrub_ratio, then trigger
317          * OI scrub to scan the whole the device. */
318         __u64                    od_full_scrub_ratio;
319         /* If the speed of found bad OI mappings (per minute)
320          * exceeds the osd_device::od_full_scrub_threshold_rate,
321          * then trigger OI scrub to scan the whole device. */
322         __u64                    od_full_scrub_threshold_rate;
323
324         /* a list of orphaned agent inodes, protected with od_osfs_lock */
325         struct list_head         od_orphan_list;
326         struct list_head         od_index_backup_list;
327         struct list_head         od_index_restore_list;
328         spinlock_t               od_lock;
329         struct inode            *od_index_backup_inode;
330         enum lustre_index_backup_policy od_index_backup_policy;
331         int                      od_index_backup_stop;
332         /* T10PI type, zero if not supported  */
333         enum osd_t10_type        od_t10_type;
334 };
335
336 static inline struct qsd_instance *osd_def_qsd(struct osd_device *osd)
337 {
338         if (osd->od_is_ost)
339                 return osd->od_quota_slave_dt;
340         else
341                 return osd->od_quota_slave_md;
342 }
343
344 enum osd_full_scrub_ratio {
345         /* Trigger OI scrub to scan the whole device directly. */
346         OFSR_DIRECTLY   = 0,
347
348         /* Because the bad OI mappings count cannot be larger than
349          * the total OI mappints count, then setting OFSR_NEVER means
350          * that the whole device scanning cannot be triggered by auto
351          * detected bad OI mappings during the RPC services. */
352         OFSR_NEVER      = 1,
353         OFSR_DEFAULT    = 10000,
354 };
355
356 #define FULL_SCRUB_THRESHOLD_RATE_DEFAULT       60
357
358 /* There are at most 15 uid/gid/projids are affected in a transaction, and
359  * that's rename case:
360  * - 3 for source parent uid & gid & projid;
361  * - 3 for source child uid & gid & projid ('..' entry update when
362  * child is directory);
363  * - 3 for target parent uid & gid & projid;
364  * - 3 for target child uid & gid & projid(if the target child exists);
365  * - 3 for root uid & gid(last_rcvd, llog, etc);
366  *
367  */
368 #define OSD_MAX_UGID_CNT        15
369
370 enum osd_op_type {
371         OSD_OT_ATTR_SET         = 0,
372         OSD_OT_PUNCH            = 1,
373         OSD_OT_XATTR_SET        = 2,
374         OSD_OT_CREATE           = 3,
375         OSD_OT_DESTROY          = 4,
376         OSD_OT_REF_ADD          = 5,
377         OSD_OT_REF_DEL          = 6,
378         OSD_OT_WRITE            = 7,
379         OSD_OT_INSERT           = 8,
380         OSD_OT_DELETE           = 9,
381         OSD_OT_QUOTA            = 10,
382         OSD_OT_MAX              = 11
383 };
384
385 struct osd_access_lock {
386         struct list_head         tl_list;
387         struct osd_object       *tl_obj;
388         bool                     tl_shared;
389         bool                     tl_truncate;
390 };
391
392 struct osd_thandle {
393         struct thandle          ot_super;
394         handle_t               *ot_handle;
395         struct ldiskfs_journal_cb_entry ot_jcb;
396         struct list_head       ot_commit_dcb_list;
397         struct list_head       ot_stop_dcb_list;
398         /* Link to the device, for debugging. */
399         struct lu_ref_link      ot_dev_link;
400         unsigned int            ot_credits;
401
402         /* quota IDs related to the transaction */
403         unsigned short          ot_id_cnt;
404         __u8                    ot_id_res[OSD_MAX_UGID_CNT];
405         __u8                    ot_id_types[OSD_MAX_UGID_CNT];
406         uid_t                   ot_id_array[OSD_MAX_UGID_CNT];
407         struct lquota_trans    *ot_quota_trans;
408
409         unsigned int            ot_remove_agents:1;
410 #if OSD_THANDLE_STATS
411         /** time when this handle was allocated */
412         ktime_t oth_alloced;
413
414         /** time when this thanle was started */
415         ktime_t oth_started;
416 #endif
417         struct list_head        ot_trunc_locks;
418 };
419
420 /**
421  * Basic transaction credit op
422  */
423 enum dt_txn_op {
424         DTO_INDEX_INSERT,
425         DTO_INDEX_DELETE,
426         DTO_INDEX_UPDATE,
427         DTO_OBJECT_CREATE,
428         DTO_OBJECT_DELETE,
429         DTO_ATTR_SET_BASE,
430         DTO_XATTR_SET,
431         DTO_WRITE_BASE,
432         DTO_WRITE_BLOCK,
433         DTO_ATTR_SET_CHOWN,
434
435         DTO_NR
436 };
437
438 /*
439  * osd dev stats
440  */
441
442 #ifdef CONFIG_PROC_FS
443 enum {
444         LPROC_OSD_READ_BYTES    = 0,
445         LPROC_OSD_WRITE_BYTES   = 1,
446         LPROC_OSD_GET_PAGE      = 2,
447         LPROC_OSD_NO_PAGE       = 3,
448         LPROC_OSD_CACHE_ACCESS  = 4,
449         LPROC_OSD_CACHE_HIT     = 5,
450         LPROC_OSD_CACHE_MISS    = 6,
451
452 #if OSD_THANDLE_STATS
453         LPROC_OSD_THANDLE_STARTING,
454         LPROC_OSD_THANDLE_OPEN,
455         LPROC_OSD_THANDLE_CLOSING,
456 #endif
457         LPROC_OSD_LAST,
458 };
459 #endif
460
461 /**
462  * Storage representation for fids.
463  *
464  * Variable size, first byte contains the length of the whole record.
465  */
466 struct osd_fid_pack {
467         unsigned char fp_len;
468         char fp_area[sizeof(struct lu_fid)];
469 };
470
471 struct osd_it_ea_dirent {
472         struct lu_fid   oied_fid;
473         __u64           oied_ino;
474         __u64           oied_off;
475         unsigned short  oied_namelen;
476         unsigned int    oied_type;
477         char            oied_name[0];
478 } __attribute__((packed));
479
480 /**
481  * as osd_it_ea_dirent (in memory dirent struct for osd) is greater
482  * than lu_dirent struct. osd readdir reads less number of dirent than
483  * required for mdd dir page. so buffer size need to be increased so that
484  * there  would be one ext3 readdir for every mdd readdir page.
485  */
486
487 #define OSD_IT_EA_BUFSIZE       (PAGE_SIZE + PAGE_SIZE/4)
488
489 /**
490  * This is iterator's in-memory data structure in interoperability
491  * mode (i.e. iterator over ldiskfs style directory)
492  */
493 struct osd_it_ea {
494         struct osd_object       *oie_obj;
495         /** used in ldiskfs iterator, to stored file pointer */
496         struct file             oie_file;
497         /** how many entries have been read-cached from storage */
498         int                     oie_rd_dirent;
499         /** current entry is being iterated by caller */
500         int                     oie_it_dirent;
501         /** current processing entry */
502         struct osd_it_ea_dirent *oie_dirent;
503         /** buffer to hold entries, size == OSD_IT_EA_BUFSIZE */
504         void                    *oie_buf;
505         struct dentry           oie_dentry;
506 };
507
508 /**
509  * Iterator's in-memory data structure for IAM mode.
510  */
511 struct osd_it_iam {
512         struct osd_object     *oi_obj;
513         struct iam_path_descr *oi_ipd;
514         struct iam_iterator    oi_it;
515 };
516
517 struct osd_quota_leaf {
518         struct list_head        oql_link;
519         uint            oql_blk;
520 };
521
522 /**
523  * Iterator's in-memory data structure for quota file.
524  */
525 struct osd_it_quota {
526         struct osd_object       *oiq_obj;
527         /** tree blocks path to where the entry is stored */
528         uint                     oiq_blk[LUSTRE_DQTREEDEPTH + 1];
529         /** on-disk offset for current key where quota record can be found */
530         loff_t                   oiq_offset;
531         /** identifier for current quota record */
532         __u64                    oiq_id;
533         /** the record index in the leaf/index block */
534         uint                     oiq_index[LUSTRE_DQTREEDEPTH + 1];
535         /** list of already processed leaf blocks */
536         struct list_head         oiq_list;
537 };
538
539 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
540
541 struct osd_iobuf {
542         wait_queue_head_t  dr_wait;
543         atomic_t       dr_numreqs;  /* number of reqs being processed */
544         int                dr_max_pages;
545         int                dr_npages;
546         int                dr_error;
547         int                dr_frags;
548         unsigned int       dr_elapsed_valid:1; /* we really did count time */
549         unsigned int       dr_rw:1;
550         struct lu_buf      dr_pg_buf;
551         struct page      **dr_pages;
552         struct niobuf_local     **dr_lnbs;
553         struct lu_buf      dr_bl_buf;
554         struct lu_buf      dr_lnb_buf;
555         sector_t          *dr_blocks;
556         ktime_t            dr_start_time;
557         ktime_t            dr_elapsed;  /* how long io took */
558         struct osd_device *dr_dev;
559         unsigned int       dr_init_at;  /* the line iobuf was initialized */
560 };
561
562 #ifdef HAVE_INODE_TIMESPEC64
563 # define osd_timespec                   timespec64
564 # define osd_timespec_trunc(ts, gran)   timespec64_trunc((ts), (gran))
565 #else
566 # define osd_timespec                   timespec
567 # define osd_timespec_trunc(ts, gran)   timespec_trunc((ts), (gran))
568 #endif
569
570 static inline struct osd_timespec osd_inode_time(struct inode *inode,
571                                                  s64 seconds)
572 {
573         struct osd_timespec ts = { .tv_sec = seconds };
574
575         return osd_timespec_trunc(ts, inode->i_sb->s_time_gran);
576 }
577
578 #define OSD_INS_CACHE_SIZE      8
579
580 struct osd_thread_info {
581         const struct lu_env   *oti_env;
582         /**
583          * used for index operations.
584          */
585         struct dentry          oti_obj_dentry;
586         struct dentry          oti_child_dentry;
587
588         /** dentry for Iterator context. */
589         struct dentry           oti_it_dentry;
590
591         union {
592                 /* fake struct file for osd_object_sync */
593                 struct file             oti_file;
594                 /* osd_statfs() */
595                 struct kstatfs          oti_ksfs;
596         };
597
598         struct htree_lock     *oti_hlock;
599
600         struct lu_fid          oti_fid;
601         struct lu_fid          oti_fid2;
602         struct lu_fid          oti_fid3;
603         struct osd_inode_id    oti_id;
604         struct osd_inode_id    oti_id2;
605         struct osd_inode_id    oti_id3;
606         struct ost_id          oti_ostid;
607
608         /**
609          * following ipd and it structures are used for osd_index_iam_lookup()
610          * these are defined separately as we might do index operation
611          * in open iterator session.
612          */
613
614         /** pre-allocated buffer used by oti_it_ea, size OSD_IT_EA_BUFSIZE */
615         void                    *oti_it_ea_buf;
616         unsigned int            oti_it_ea_buf_used:1;
617
618         /* IAM iterator for index operation. */
619         struct iam_iterator    oti_idx_it;
620
621         /** union to guarantee that ->oti_ipd[] has proper alignment. */
622         union {
623                 char           oti_name[48];
624                 char           oti_it_ipd[DX_IPD_MAX_SIZE];
625                 long long      oti_alignment_lieutenant;
626         };
627
628         union {
629                 char           oti_idx_ipd[DX_IPD_MAX_SIZE];
630                 long long      oti_alignment_lieutenant_colonel;
631         };
632
633         struct osd_idmap_cache oti_cache;
634
635         /* dedicated OI cache for insert (which needs inum) */
636         struct osd_idmap_cache          *oti_ins_cache;
637         int                             oti_ins_cache_size;
638         int                             oti_ins_cache_used;
639         /* inc by osd_trans_create and dec by osd_trans_stop */
640         int                             oti_ins_cache_depth;
641
642         int                             oti_r_locks;
643         int                             oti_w_locks;
644         int                             oti_txns;
645         /** used in osd_fid_set() to put xattr */
646         struct lu_buf                   oti_buf;
647         struct lu_buf                   oti_big_buf;
648         /** used in osd_ea_fid_set() to set fid into common ea */
649         union {
650                 struct lustre_ost_attrs oti_ost_attrs;
651                 struct filter_fid_18_23 oti_ff_old;
652                 struct filter_fid       oti_ff;
653         };
654         /** 0-copy IO */
655         struct osd_iobuf                oti_iobuf;
656         /* used to access objects in /O */
657         struct inode                    *oti_inode;
658 #define OSD_FID_REC_SZ 32
659         char                            oti_ldp[OSD_FID_REC_SZ];
660         char                            oti_ldp2[OSD_FID_REC_SZ];
661
662         /* used by quota code */
663         union {
664 #if defined(HAVE_DQUOT_QC_DQBLK)
665                 struct qc_dqblk         oti_qdq;
666 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
667                 struct fs_disk_quota    oti_fdq;
668 #else
669                 struct if_dqblk         oti_dqblk;
670 #endif
671                 struct if_dqinfo        oti_dqinfo;
672         };
673         struct lquota_id_info   oti_qi;
674         struct lquota_trans     oti_quota_trans;
675         union lquota_rec        oti_quota_rec;
676         __u64                   oti_quota_id;
677         struct lu_seq_range     oti_seq_range;
678
679         /* Tracking for transaction credits, to allow debugging and optimizing
680          * cases where a large number of credits are being allocated for
681          * single transaction. */
682         unsigned int            oti_credits_before;
683         unsigned int            oti_declare_ops[OSD_OT_MAX];
684         unsigned int            oti_declare_ops_cred[OSD_OT_MAX];
685         unsigned int            oti_declare_ops_used[OSD_OT_MAX];
686         struct osd_directory    oti_iam;
687
688         struct page             **oti_dio_pages;
689         int                     oti_dio_pages_used;
690 };
691
692 extern int ldiskfs_pdo;
693
694 #ifndef HAVE_VFS_SETXATTR
695 #define osd_setxattr(dentry, inode, name, buf, len, flag) \
696                 ((inode)->i_op->setxattr(dentry, name, buf, len, flag))
697 #define osd_getxattr(dentry, inode, name, buf, len) \
698                 ((inode)->i_op->getxattr(dentry, name, buf, len))
699 #define osd_removexattr(dentry, inode, name) \
700                 ((inode)->i_op->removexattr(dentry, name))
701 #else /* HAVE_VFS_SETXATTR */
702 #define osd_setxattr(dentry, inode, name, buf, len, flag) \
703                 __vfs_setxattr(dentry, inode, name, buf, len, flag)
704 #define osd_getxattr(dentry, inode, name, buf, len) \
705                 __vfs_getxattr(dentry, inode, name, buf, len)
706 #define osd_removexattr(dentry, inode, name) \
707                 __vfs_removexattr(dentry, name)
708 #endif /* !HAVE_VFS_SETXATTR */
709
710 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
711                                   const char *name, void *buf, int len)
712 {
713         if (inode == NULL)
714                 return -EINVAL;
715
716         dentry->d_inode = inode;
717         dentry->d_sb = inode->i_sb;
718         return osd_getxattr(dentry, inode, name, buf, len);
719 }
720
721 static inline int __osd_xattr_set(struct osd_thread_info *info,
722                                   struct inode *inode, const char *name,
723                                   const void *buf, int buflen, int fl)
724 {
725         struct dentry *dentry = &info->oti_child_dentry;
726
727         ll_vfs_dq_init(inode);
728         dentry->d_inode = inode;
729         dentry->d_sb = inode->i_sb;
730         return osd_setxattr(dentry, inode, name, buf, buflen, fl);
731 }
732
733 #ifdef CONFIG_PROC_FS
734 /* osd_lproc.c */
735 extern struct lprocfs_vars lprocfs_osd_obd_vars[];
736 int osd_procfs_init(struct osd_device *osd, const char *name);
737 int osd_procfs_fini(struct osd_device *osd);
738 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf);
739 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
740 int osd_register_proc_index_in_idif(struct osd_device *osd);
741 #endif
742
743 #endif
744 int osd_statfs(const struct lu_env *env, struct dt_device *dev,
745                struct obd_statfs *sfs, struct obd_statfs_info *info);
746 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
747                        struct osd_inode_id *id);
748 struct inode *
749 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
750              struct osd_inode_id *id, struct lu_fid *fid);
751 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
752                    const struct lu_fid *fid, __u32 compat, __u32 incompat);
753 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
754                 struct dentry *dentry, struct lustre_ost_attrs *loa);
755 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
756                       struct osd_inode_id *id, const struct lu_fid *fid);
757 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
758                  struct dentry *dentry, struct lu_fid *fid);
759
760 int osd_obj_map_init(const struct lu_env *env, struct osd_device *osd);
761 void osd_obj_map_fini(struct osd_device *dev);
762 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *osd,
763                         const struct lu_fid *fid, struct osd_inode_id *id);
764 int osd_obj_map_insert(struct osd_thread_info *info, struct osd_device *osd,
765                        const struct lu_fid *fid, const struct osd_inode_id *id,
766                        handle_t *th);
767 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
768                         const struct lu_fid *fid, handle_t *th);
769 int osd_obj_map_update(struct osd_thread_info *info, struct osd_device *osd,
770                        const struct lu_fid *fid, const struct osd_inode_id *id,
771                        handle_t *th);
772 int osd_obj_map_recover(struct osd_thread_info *info, struct osd_device *osd,
773                         struct inode *src_parent, struct dentry *src_child,
774                         const struct lu_fid *fid);
775 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
776                         const struct lu_fid *fid, struct osd_inode_id *id);
777 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
778                         const struct lu_fid *fid, const struct osd_inode_id *id,
779                         handle_t *th);
780 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
781                         const struct lu_fid *fid, const struct osd_inode_id *id,
782                         handle_t *th);
783
784 char *osd_lf_fid2name(const struct lu_fid *fid);
785 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
786                     __u32 flags);
787 void osd_scrub_stop(struct osd_device *dev);
788 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev);
789 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev);
790 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
791                    int insert);
792 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
793                    struct osd_inode_id *id);
794 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev);
795
796 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
797                    u64 seq, struct lu_seq_range *range);
798
799 int osd_delete_from_remote_parent(const struct lu_env *env,
800                                   struct osd_device *osd,
801                                   struct osd_object *obj,
802                                   struct osd_thandle *oh, bool destroy);
803 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
804                              struct osd_object *obj, struct osd_thandle *oh);
805 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
806                                 struct osd_device *osd,
807                                 const struct lu_fid *fid,
808                                 struct osd_inode_id *id);
809
810 int osd_ost_seq_exists(struct osd_thread_info *info, struct osd_device *osd,
811                        __u64 seq);
812 /* osd_quota_fmt.c */
813 int walk_tree_dqentry(const struct lu_env *env, struct osd_object *obj,
814                       int type, uint blk, int depth, uint index,
815                       struct osd_it_quota *it);
816 int walk_block_dqentry(const struct lu_env *env, struct osd_object *obj,
817                        int type, uint blk, uint index,
818                        struct osd_it_quota *it);
819 loff_t find_tree_dqentry(const struct lu_env *env,
820                          struct osd_object *obj, int type,
821                          qid_t dqid, uint blk, int depth,
822                          struct osd_it_quota *it);
823 /* osd_quota.c */
824 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
825                     struct lquota_id_info *qi, struct osd_object *obj,
826                     bool enforce, int *flags);
827 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
828                           __u32 projid, long long space, struct osd_thandle *oh,
829                           struct osd_object *obj, int *flags,
830                           enum osd_qid_declare_flags);
831 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
832                                     const struct dt_rec *rec,
833                                     union lquota_rec *quota_rec);
834 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec);
835
836 #ifndef HAVE_I_UID_READ
837 static inline uid_t i_uid_read(const struct inode *inode)
838 {
839         return inode->i_uid;
840 }
841
842 static inline gid_t i_gid_read(const struct inode *inode)
843 {
844         return inode->i_gid;
845 }
846
847 static inline void i_uid_write(struct inode *inode, uid_t uid)
848 {
849         inode->i_uid = uid;
850 }
851
852 static inline void i_gid_write(struct inode *inode, gid_t gid)
853 {
854         inode->i_gid = gid;
855 }
856 #endif
857
858 #ifdef HAVE_PROJECT_QUOTA
859 static inline __u32 i_projid_read(struct inode *inode)
860 {
861         return (__u32)from_kprojid(&init_user_ns, LDISKFS_I(inode)->i_projid);
862 }
863
864 static inline void i_projid_write(struct inode *inode, __u32 projid)
865 {
866         kprojid_t kprojid;
867         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
868         LDISKFS_I(inode)->i_projid = kprojid;
869 }
870 #else
871 static inline uid_t i_projid_read(struct inode *inode)
872 {
873         return 0;
874 }
875 static inline void i_projid_write(struct inode *inode, __u32 projid)
876 {
877         return;
878 }
879 #endif
880
881 #ifdef HAVE_LDISKFS_IGET_WITH_FLAGS
882 # define osd_ldiskfs_iget(sb, ino) \
883                 ldiskfs_iget((sb), (ino), LDISKFS_IGET_HANDLE)
884 #else
885 # define osd_ldiskfs_iget(sb, ino) ldiskfs_iget((sb), (ino))
886 #endif
887
888 #ifdef HAVE_LDISKFS_INFO_JINODE
889 # define osd_attach_jinode(inode) ldiskfs_inode_attach_jinode(inode)
890 #else  /* HAVE_LDISKFS_INFO_JINODE */
891 # define osd_attach_jinode(inode) 0
892 #endif /* HAVE_LDISKFS_INFO_JINODE */
893
894 #ifdef LDISKFS_HT_MISC
895 # define osd_journal_start_sb(sb, type, nblock) \
896                 ldiskfs_journal_start_sb(sb, type, nblock)
897 static inline struct buffer_head *osd_ldiskfs_append(handle_t *handle,
898                                                      struct inode *inode,
899                                                      ldiskfs_lblk_t *nblock)
900 {
901         int rc;
902
903         rc = osd_attach_jinode(inode);
904         if (rc)
905                 return ERR_PTR(rc);
906         return ldiskfs_append(handle, inode, nblock);
907 }
908 # define osd_ldiskfs_find_entry(dir, name, de, inlined, lock) \
909                 (__ldiskfs_find_entry(dir, name, de, inlined, lock) ?: \
910                  ERR_PTR(-ENOENT))
911 # define osd_journal_start(inode, type, nblocks) \
912                 ldiskfs_journal_start(inode, type, nblocks)
913 # define osd_transaction_size(dev) \
914                 (osd_journal(dev)->j_max_transaction_buffers / 2)
915 #else
916 # define LDISKFS_HT_MISC        0
917 # define osd_journal_start_sb(sb, type, nblock) \
918                 ldiskfs_journal_start_sb(sb, nblock)
919
920 static inline struct buffer_head *osd_ldiskfs_append(handle_t *handle,
921                                                      struct inode *inode,
922                                                      ldiskfs_lblk_t *nblock)
923 {
924         struct buffer_head *bh;
925         int err = 0;
926
927         bh = ldiskfs_append(handle, inode, nblock, &err);
928         if (bh == NULL)
929                 bh = ERR_PTR(err);
930
931         return bh;
932 }
933
934 # define osd_ldiskfs_find_entry(dir, name, de, inlined, lock) \
935                 (__ldiskfs_find_entry(dir, name, de, lock) ?: \
936                  ERR_PTR(-ENOENT))
937 # define osd_journal_start(inode, type, nblocks) \
938                 ldiskfs_journal_start(inode, nblocks)
939 # define osd_transaction_size(dev) \
940                 (osd_journal(dev)->j_max_transaction_buffers)
941 #endif
942
943 /*
944  * Invariants, assertions.
945  */
946
947 /*
948  * XXX: do not enable this, until invariant checking code is made thread safe
949  * in the face of pdirops locking.
950  */
951 #define OSD_INVARIANT_CHECKS (0)
952
953 #if OSD_INVARIANT_CHECKS
954 static inline int osd_invariant(const struct osd_object *obj)
955 {
956         return
957                 obj != NULL &&
958                 ergo(obj->oo_inode != NULL,
959                      obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
960                      atomic_read(&obj->oo_inode->i_count) > 0) &&
961                 ergo(obj->oo_dir != NULL &&
962                      obj->oo_dir->od_conationer.ic_object != NULL,
963                      obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
964 }
965 #else
966 #define osd_invariant(obj) (1)
967 #endif
968
969 #define OSD_MAX_CACHE_SIZE OBD_OBJECT_EOF
970
971 extern const struct dt_index_operations osd_otable_ops;
972
973 static inline int osd_oi_fid2idx(struct osd_device *dev,
974                                  const struct lu_fid *fid)
975 {
976         return fid->f_seq & (dev->od_oi_count - 1);
977 }
978
979 static inline struct osd_oi *osd_fid2oi(struct osd_device *osd,
980                                         const struct lu_fid *fid)
981 {
982         LASSERTF(!fid_is_idif(fid), DFID"\n", PFID(fid));
983         LASSERTF(!fid_is_last_id(fid), DFID"\n", PFID(fid));
984         LASSERTF(osd->od_oi_table != NULL && osd->od_oi_count >= 1,
985                  DFID"\n", PFID(fid));
986         /* It can work even od_oi_count equals to 1 although it's unexpected,
987          * the only reason we set it to 1 is for performance measurement */
988         return osd->od_oi_table[osd_oi_fid2idx(osd, fid)];
989 }
990
991 extern const struct lu_device_operations  osd_lu_ops;
992
993 static inline int lu_device_is_osd(const struct lu_device *d)
994 {
995         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
996 }
997
998 static inline struct osd_device *osd_dt_dev(const struct dt_device *d)
999 {
1000         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
1001         return container_of0(d, struct osd_device, od_dt_dev);
1002 }
1003
1004 static inline struct osd_device *osd_dev(const struct lu_device *d)
1005 {
1006         LASSERT(lu_device_is_osd(d));
1007         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
1008 }
1009
1010 static inline struct osd_device *osd_obj2dev(const struct osd_object *o)
1011 {
1012         return osd_dev(o->oo_dt.do_lu.lo_dev);
1013 }
1014
1015 static inline struct super_block *osd_sb(const struct osd_device *dev)
1016 {
1017         return dev->od_mnt->mnt_sb;
1018 }
1019
1020 static inline const char *osd_dev2name(const struct osd_device *dev)
1021 {
1022         return osd_sb(dev)->s_id;
1023 }
1024
1025 static inline const char *osd_ino2name(const struct inode *inode)
1026 {
1027         return inode->i_sb->s_id;
1028 }
1029
1030 /**
1031  * Put the osd object once done with it.
1032  *
1033  * \param obj osd object that needs to be put
1034  */
1035 static inline void osd_object_put(const struct lu_env *env,
1036                                   struct osd_object *obj)
1037 {
1038         dt_object_put(env, &obj->oo_dt);
1039 }
1040
1041 static inline int osd_object_is_root(const struct osd_object *obj)
1042 {
1043         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
1044 }
1045
1046 static inline struct osd_object *osd_obj(const struct lu_object *o)
1047 {
1048         LASSERT(lu_device_is_osd(o->lo_dev));
1049         return container_of0(o, struct osd_object, oo_dt.do_lu);
1050 }
1051
1052 static inline struct osd_object *osd_dt_obj(const struct dt_object *d)
1053 {
1054         return osd_obj(&d->do_lu);
1055 }
1056
1057 static inline struct lu_device *osd2lu_dev(struct osd_device *osd)
1058 {
1059         return &osd->od_dt_dev.dd_lu_dev;
1060 }
1061
1062 static inline journal_t *osd_journal(const struct osd_device *dev)
1063 {
1064         return LDISKFS_SB(osd_sb(dev))->s_journal;
1065 }
1066
1067 static inline struct seq_server_site *osd_seq_site(struct osd_device *osd)
1068 {
1069         return osd->od_dt_dev.dd_lu_dev.ld_site->ld_seq_site;
1070 }
1071
1072 static inline char *osd_name(struct osd_device *osd)
1073 {
1074         return osd->od_svname;
1075 }
1076
1077 static inline bool osd_is_ea_inode(struct inode *inode)
1078 {
1079         return !!(LDISKFS_I(inode)->i_flags & LDISKFS_EA_INODE_FL);
1080 }
1081
1082 extern const struct dt_body_operations osd_body_ops;
1083 extern struct lu_context_key osd_key;
1084
1085 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
1086 {
1087         return lu_context_key_get(&env->le_ctx, &osd_key);
1088 }
1089
1090 extern const struct dt_body_operations osd_body_ops_new;
1091
1092 /**
1093  * IAM Iterator
1094  */
1095 static inline
1096 struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
1097                                       const struct iam_container *bag)
1098 {
1099         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
1100                                            osd_oti_get(env)->oti_it_ipd);
1101 }
1102
1103 static inline
1104 struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
1105                                        const struct iam_container *bag)
1106 {
1107         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
1108                                            osd_oti_get(env)->oti_idx_ipd);
1109 }
1110
1111 static inline void osd_ipd_put(const struct lu_env *env,
1112                                const struct iam_container *bag,
1113                                struct iam_path_descr *ipd)
1114 {
1115         bag->ic_descr->id_ops->id_ipd_free(ipd);
1116 }
1117
1118 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1119                            const loff_t size, const loff_t pos,
1120                            const int blocks);
1121
1122 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
1123 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1124                              int write_NUL, loff_t *offs, handle_t *handle);
1125
1126 static inline
1127 struct dentry *osd_child_dentry_by_inode(const struct lu_env *env,
1128                                          struct inode *inode,
1129                                          const char *name, const int namelen)
1130 {
1131         struct osd_thread_info *info = osd_oti_get(env);
1132         struct dentry *child_dentry = &info->oti_child_dentry;
1133         struct dentry *obj_dentry = &info->oti_obj_dentry;
1134
1135         obj_dentry->d_inode = inode;
1136         obj_dentry->d_sb = inode->i_sb;
1137         obj_dentry->d_name.hash = 0;
1138
1139         child_dentry->d_name.hash = 0;
1140         child_dentry->d_parent = obj_dentry;
1141         child_dentry->d_name.name = name;
1142         child_dentry->d_name.len = namelen;
1143         return child_dentry;
1144 }
1145
1146 extern int osd_trans_declare_op2rb[];
1147 extern int ldiskfs_track_declares_assert;
1148 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th);
1149
1150 static inline void osd_trans_declare_op(const struct lu_env *env,
1151                                         struct osd_thandle *oh,
1152                                         enum osd_op_type op, int credits)
1153 {
1154         struct osd_thread_info *oti = osd_oti_get(env);
1155
1156         LASSERT(oh->ot_handle == NULL);
1157         if (unlikely(op >= OSD_OT_MAX)) {
1158                 if (unlikely(ldiskfs_track_declares_assert)) {
1159                         LASSERT(op < OSD_OT_MAX);
1160                 } else {
1161                         CWARN("%s: Invalid operation index %d\n",
1162                               osd_name(osd_dt_dev(oh->ot_super.th_dev)), op);
1163                         libcfs_debug_dumpstack(NULL);
1164                 }
1165         } else {
1166                 oti->oti_declare_ops[op]++;
1167                 oti->oti_declare_ops_cred[op] += credits;
1168         }
1169         oh->ot_credits += credits;
1170 }
1171
1172 static inline void osd_trans_exec_op(const struct lu_env *env,
1173                                      struct thandle *th,
1174                                      enum osd_op_type op)
1175 {
1176         struct osd_thread_info *oti = osd_oti_get(env);
1177         struct osd_thandle     *oh  = container_of(th, struct osd_thandle,
1178                                                    ot_super);
1179         unsigned int            rb, left;
1180
1181         LASSERT(oh->ot_handle != NULL);
1182         if (unlikely(op >= OSD_OT_MAX)) {
1183                 if (unlikely(ldiskfs_track_declares_assert))
1184                         LASSERT(op < OSD_OT_MAX);
1185                 else {
1186                         CWARN("%s: opcode %u: invalid value >= %u\n",
1187                               osd_name(osd_dt_dev(oh->ot_super.th_dev)),
1188                               op, OSD_OT_MAX);
1189                         libcfs_debug_dumpstack(NULL);
1190                         return;
1191                 }
1192         }
1193
1194         /* find rollback (or reverse) operation for the given one
1195          * such an operation doesn't require additional credits
1196          * as the same set of blocks are modified */
1197         rb = osd_trans_declare_op2rb[op];
1198
1199         /* check whether credits for this operation were reserved at all */
1200         if (unlikely(oti->oti_declare_ops_cred[op] == 0 &&
1201                      oti->oti_declare_ops_cred[rb] == 0)) {
1202                 /* the API is not perfect yet: CREATE does REF_ADD internally
1203                  * while DESTROY does not. To rollback CREATE the callers
1204                  * needs to call REF_DEL+DESTROY which is hard to detect using
1205                  * a simple table of rollback operations */
1206                 if (op == OSD_OT_REF_DEL &&
1207                     oti->oti_declare_ops_cred[OSD_OT_CREATE] > 0)
1208                         goto proceed;
1209                 if (op == OSD_OT_REF_ADD &&
1210                     oti->oti_declare_ops_cred[OSD_OT_DESTROY] > 0)
1211                         goto proceed;
1212                 CWARN("%s: opcode %u: credits = 0, rollback = %u\n",
1213                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op, rb);
1214                 osd_trans_dump_creds(env, th);
1215                 LASSERT(!ldiskfs_track_declares_assert);
1216         }
1217
1218 proceed:
1219         /* remember how many credits we have unused before the operation */
1220         oti->oti_credits_before = oh->ot_handle->h_buffer_credits;
1221         left = oti->oti_declare_ops_cred[op] - oti->oti_declare_ops_used[op];
1222         if (unlikely(oti->oti_credits_before < left)) {
1223                 CWARN("%s: opcode %u: before %u < left %u, rollback = %u\n",
1224                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op,
1225                       oti->oti_credits_before, left, rb);
1226                 osd_trans_dump_creds(env, th);
1227                 /* on a very small fs (testing?) it's possible that
1228                  * the transaction can't fit 1/4 of journal, so we
1229                  * just request less credits (see osd_trans_start()).
1230                  * ignore the same case here */
1231                 rb = osd_transaction_size(osd_dt_dev(th->th_dev));
1232                 if (unlikely(oh->ot_credits < rb))
1233                         LASSERT(!ldiskfs_track_declares_assert);
1234         }
1235 }
1236
1237 static inline void osd_trans_exec_check(const struct lu_env *env,
1238                                         struct thandle *th,
1239                                         enum osd_op_type op)
1240 {
1241         struct osd_thread_info *oti = osd_oti_get(env);
1242         struct osd_thandle     *oh  = container_of(th, struct osd_thandle,
1243                                                    ot_super);
1244         int                     used, over, quota;
1245
1246         /* how many credits have been used by the operation */
1247         used = oti->oti_credits_before - oh->ot_handle->h_buffer_credits;
1248
1249         if (unlikely(used < 0)) {
1250                 /* if some block was allocated and released in the same
1251                  * transaction, then it won't be a part of the transaction
1252                  * and delta can be negative */
1253                 return;
1254         }
1255
1256         if (used == 0) {
1257                 /* rollback operations (e.g. when we destroy just created
1258                  * object) should not consume any credits. there is no point
1259                  * to confuse the checks below */
1260                 return;
1261         }
1262
1263         oti->oti_declare_ops_used[op] += used;
1264         if (oti->oti_declare_ops_used[op] <= oti->oti_declare_ops_cred[op])
1265                 return;
1266
1267         /* we account quota for a whole transaction and any operation can
1268          * consume corresponding credits */
1269         over = oti->oti_declare_ops_used[op] -
1270                 oti->oti_declare_ops_cred[op];
1271         quota = oti->oti_declare_ops_cred[OSD_OT_QUOTA] -
1272                 oti->oti_declare_ops_used[OSD_OT_QUOTA];
1273         if (over <= quota) {
1274                 /* probably that credits were consumed by
1275                  * quota indirectly (in the depths of ldiskfs) */
1276                 oti->oti_declare_ops_used[OSD_OT_QUOTA] += over;
1277                 oti->oti_declare_ops_used[op] -= over;
1278         } else {
1279                 CWARN("%s: opcode %d: used %u, used now %u, reserved %u\n",
1280                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op,
1281                       oti->oti_declare_ops_used[op], used,
1282                       oti->oti_declare_ops_cred[op]);
1283                 osd_trans_dump_creds(env, th);
1284                 if (unlikely(ldiskfs_track_declares_assert))
1285                         LBUG();
1286         }
1287 }
1288
1289 /**
1290  * Helper function to pack the fid, ldiskfs stores fid in packed format.
1291  */
1292 static inline
1293 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1294                   struct lu_fid *befider)
1295 {
1296         fid_cpu_to_be(befider, (struct lu_fid *)fid);
1297         memcpy(pack->fp_area, befider, sizeof(*befider));
1298         pack->fp_len =  sizeof(*befider) + 1;
1299 }
1300
1301 static inline
1302 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1303 {
1304         int result;
1305
1306         result = 0;
1307         switch (pack->fp_len) {
1308         case sizeof *fid + 1:
1309                 memcpy(fid, pack->fp_area, sizeof *fid);
1310                 fid_be_to_cpu(fid, fid);
1311                 break;
1312         default:
1313                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1314                 result = -EIO;
1315         }
1316         return result;
1317 }
1318
1319 /**
1320  * Quota/Accounting handling
1321  */
1322 extern const struct dt_index_operations osd_acct_index_ops;
1323 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
1324                         const struct lu_fid *fid, struct osd_inode_id *id);
1325
1326 /* copy from fs/ext4/dir.c */
1327 static inline int is_32bit_api(void)
1328 {
1329 #ifdef CONFIG_COMPAT
1330         return in_compat_syscall();
1331 #else
1332         return (BITS_PER_LONG == 32);
1333 #endif
1334 }
1335
1336 static inline loff_t ldiskfs_get_htree_eof(struct file *filp)
1337 {
1338         if ((filp->f_mode & FMODE_32BITHASH) ||
1339             (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
1340                 return LDISKFS_HTREE_EOF_32BIT;
1341         else
1342                 return LDISKFS_HTREE_EOF_64BIT;
1343 }
1344
1345 static inline int fid_is_internal(const struct lu_fid *fid)
1346 {
1347         return (!fid_is_namespace_visible(fid) && !fid_is_idif(fid));
1348 }
1349
1350 static inline bool is_remote_parent_ino(struct osd_device *o, unsigned long ino)
1351 {
1352         if (o->od_is_ost)
1353                 return false;
1354
1355         LASSERT(o->od_mdt_map != NULL);
1356
1357         return ino == o->od_mdt_map->omm_remote_parent->d_inode->i_ino;
1358 }
1359
1360 /**
1361  * ext4_bread/ldiskfs_bread has either 5 or 4 parameters. The error
1362  * return code has been removed and integrated into the pointer in the
1363  * kernel 3.18.
1364  */
1365 static inline struct buffer_head *__ldiskfs_bread(handle_t *handle,
1366                                                   struct inode *inode,
1367                                                   ldiskfs_lblk_t block,
1368                                                   int create)
1369 {
1370         int rc = 0;
1371         struct buffer_head *bh;
1372
1373         if (create) {
1374                 rc = osd_attach_jinode(inode);
1375                 if (rc)
1376                         return ERR_PTR(rc);
1377         }
1378 #ifdef HAVE_EXT4_BREAD_4ARGS
1379         bh = ldiskfs_bread(handle, inode, block, create);
1380 #else
1381
1382         bh = ldiskfs_bread(handle, inode, block, create, &rc);
1383         if (bh == NULL && rc != 0)
1384                 bh = ERR_PTR(rc);
1385 #endif
1386         return bh;
1387 }
1388
1389 #ifndef HAVE_BIO_INTEGRITY_ENABLED
1390 bool bio_integrity_enabled(struct bio *bio);
1391 #endif
1392
1393 #ifdef HAVE_BI_BDEV
1394 # define bio_get_dev(bio)       ((bio)->bi_bdev)
1395 # define bio_get_disk(bio)      (bio_get_dev(bio)->bd_disk)
1396 # define bio_get_queue(bio)     bdev_get_queue(bio_get_dev(bio))
1397 # define bio_set_dev(bio, bdev) (bio_get_dev(bio) = (bdev))
1398 #else
1399 # define bio_get_disk(bio)      ((bio)->bi_disk)
1400 # define bio_get_queue(bio)     (bio_get_disk(bio)->queue)
1401 #endif
1402
1403 void ldiskfs_inc_count(handle_t *handle, struct inode *inode);
1404 void ldiskfs_dec_count(handle_t *handle, struct inode *inode);
1405
1406 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf);
1407
1408 static inline int
1409 osd_index_register(struct osd_device *osd, const struct lu_fid *fid,
1410                    __u32 keysize, __u32 recsize)
1411 {
1412         return lustre_index_register(&osd->od_dt_dev, osd_name(osd),
1413                                      &osd->od_index_backup_list, &osd->od_lock,
1414                                      &osd->od_index_backup_stop,
1415                                      fid, keysize, recsize);
1416 }
1417
1418 static inline void
1419 osd_index_backup(const struct lu_env *env, struct osd_device *osd, bool backup)
1420 {
1421         struct osd_thread_info *info = osd_oti_get(env);
1422         struct lu_fid *fid = &info->oti_fid3;
1423         struct osd_inode_id *id = &info->oti_id3;
1424
1425         lu_local_obj_fid(fid, INDEX_BACKUP_OID);
1426         osd_id_gen(id, osd->od_index_backup_inode->i_ino,
1427                    osd->od_index_backup_inode->i_generation);
1428         osd_add_oi_cache(info, osd, id, fid);
1429
1430         lustre_index_backup(env, &osd->od_dt_dev, osd_name(osd),
1431                             &osd->od_index_backup_list, &osd->od_lock,
1432                             &osd->od_index_backup_stop, backup);
1433 }
1434
1435 #ifdef LDISKFS_HAS_INCOMPAT_FEATURE
1436
1437 # ifdef LDISKFS_FEATURE_INCOMPAT_EXTENTS
1438 # define ldiskfs_has_feature_extents(sb) \
1439         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS)
1440 # endif
1441 # ifdef LDISKFS_FEATURE_INCOMPAT_EA_INODE
1442 # define ldiskfs_has_feature_ea_inode(sb) \
1443         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1444 # endif
1445 # ifdef LDISKFS_FEATURE_INCOMPAT_DIRDATA
1446 # define ldiskfs_has_feature_dirdata(sb) \
1447         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_DIRDATA)
1448 # endif
1449 # ifdef LDISKFS_FEATURE_COMPAT_HAS_JOURNAL
1450 # define ldiskfs_has_feature_journal(sb) \
1451         LDISKFS_HAS_COMPAT_FEATURE(sb, LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)
1452 # endif
1453 # ifdef LDISKFS_FEATURE_RO_COMPAT_QUOTA
1454 # define ldiskfs_has_feature_quota(sb) \
1455         LDISKFS_HAS_RO_COMPAT_FEATURE(sb, LDISKFS_FEATURE_RO_COMPAT_QUOTA)
1456 # endif
1457 # ifdef LDISKFS_FEATURE_RO_COMPAT_PROJECT
1458 # define ldiskfs_has_feature_project(sb) \
1459         LDISKFS_HAS_RO_COMPAT_FEATURE(sb, LDISKFS_FEATURE_RO_COMPAT_PROJECT)
1460 # endif
1461
1462 #endif
1463
1464 int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh,
1465                    bool shared);
1466 void osd_trunc_unlock_all(struct list_head *list);
1467 void osd_process_truncates(struct list_head *list);
1468 void osd_execute_truncate(struct osd_object *obj);
1469
1470 #ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
1471 #define osd_dio_complete_routine(bio, error) dio_complete_routine(bio)
1472 #else
1473 #define osd_dio_complete_routine(bio, error) dio_complete_routine(bio, error)
1474 #endif
1475
1476 #ifndef HAVE___BI_CNT
1477 #define __bi_cnt bi_cnt
1478 #endif
1479
1480 #ifndef HAVE_BI_OPF
1481 #define bi_opf bi_rw
1482 #endif
1483
1484 #ifndef HAVE_CLEAN_BDEV_ALIASES
1485 #define clean_bdev_aliases(bdev, block, len)    \
1486         unmap_underlying_metadata((bdev), (block))
1487 #endif
1488
1489 #ifndef HAVE_BI_STATUS
1490 #define bi_status bi_error
1491 #endif
1492
1493 /*
1494  * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
1495  * This limit is arbitrary, but is reasonable for the xattr API.
1496  */
1497 #define LDISKFS_XATTR_MAX_LARGE_EA_SIZE    (1024 * 1024)
1498
1499 struct osd_bio_private {
1500         struct osd_iobuf        *obp_iobuf;
1501         /* Start page index in the obp_iobuf for the bio */
1502         int                      obp_start_page_idx;
1503 };
1504
1505 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
1506 int osd_get_integrity_profile(struct osd_device *osd,
1507                               integrity_gen_fn **generate_fn,
1508                               integrity_vrfy_fn **verify_fn);
1509 #else
1510 #define integrity_gen_fn void
1511 #define integrity_vrfy_fn int
1512 static inline int osd_get_integrity_profile(struct osd_device *osd,
1513                                             integrity_gen_fn **generate_fn,
1514                                             integrity_vrfy_fn **verify_fn)
1515 {
1516         return 0;
1517 }
1518
1519 static inline bool bio_integrity_prep_fn(struct bio *bio,
1520                                          integrity_gen_fn *generate_fn,
1521                                          integrity_vrfy_fn *verify_fn)
1522 {
1523         return bio_integrity_prep(bio);
1524 }
1525 #endif
1526
1527 #endif /* _OSD_INTERNAL_H */