Whamcloud - gitweb
LU-10560 osd: bi_bdev is replaced by bi_disk
[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 /*
242  * osd device.
243  */
244 struct osd_device {
245         /* super-class */
246         struct dt_device          od_dt_dev;
247         /* information about underlying file system */
248         struct vfsmount          *od_mnt;
249         /* object index */
250         struct osd_oi           **od_oi_table;
251         /* total number of OI containers */
252         int                       od_oi_count;
253         /*
254          * Fid Capability
255          */
256         unsigned int              od_fl_capa:1,
257                                   od_maybe_new:1,
258                                   od_igif_inoi:1,
259                                   od_check_ff:1,
260                                   od_is_ost:1,
261                                   od_in_init:1,
262                                   od_index_in_idif:1;
263
264         __s64                     od_auto_scrub_interval;
265         __u32                     od_dirent_journal;
266         int                       od_index;
267         struct proc_dir_entry    *od_proc_entry;
268         struct lprocfs_stats     *od_stats;
269
270         spinlock_t                od_osfs_lock;
271
272         int                       od_connects;
273         struct lu_site            od_site;
274
275         struct osd_obj_map      *od_ost_map;
276         struct osd_mdobj_map    *od_mdt_map;
277
278         unsigned long long      od_readcache_max_filesize;
279         int                     od_read_cache;
280         int                     od_writethrough_cache;
281
282         struct brw_stats        od_brw_stats;
283         atomic_t                od_r_in_flight;
284         atomic_t                od_w_in_flight;
285
286         struct mutex              od_otable_mutex;
287         struct osd_otable_it     *od_otable_it;
288         struct osd_scrub          od_scrub;
289         struct list_head                  od_ios_list;
290
291         /* service name associated with the osd device */
292         char                      od_svname[MAX_OBD_NAME];
293         char                      od_mntdev[MAX_OBD_NAME];
294
295         /* quota slave instance */
296         struct qsd_instance      *od_quota_slave;
297
298         /* osd seq instance */
299         struct lu_client_seq    *od_cl_seq;
300         /* If the ratio of "the total OI mappings count" vs
301          * "the bad OI mappings count" is lower than the
302          * osd_device::od_full_scrub_ratio, then trigger
303          * OI scrub to scan the whole the device. */
304         __u64                    od_full_scrub_ratio;
305         /* If the speed of found bad OI mappings (per minute)
306          * exceeds the osd_device::od_full_scrub_threshold_rate,
307          * then trigger OI scrub to scan the whole device. */
308         __u64                    od_full_scrub_threshold_rate;
309
310         /* a list of orphaned agent inodes, protected with od_osfs_lock */
311         struct list_head         od_orphan_list;
312         struct list_head         od_index_backup_list;
313         struct list_head         od_index_restore_list;
314         spinlock_t               od_lock;
315         struct inode            *od_index_backup_inode;
316         enum lustre_index_backup_policy od_index_backup_policy;
317         int                      od_index_backup_stop;
318 };
319
320 enum osd_full_scrub_ratio {
321         /* Trigger OI scrub to scan the whole device directly. */
322         OFSR_DIRECTLY   = 0,
323
324         /* Because the bad OI mappings count cannot be larger than
325          * the total OI mappints count, then setting OFSR_NEVER means
326          * that the whole device scanning cannot be triggered by auto
327          * detected bad OI mappings during the RPC services. */
328         OFSR_NEVER      = 1,
329         OFSR_DEFAULT    = 10000,
330 };
331
332 #define FULL_SCRUB_THRESHOLD_RATE_DEFAULT       60
333
334 /* There are at most 15 uid/gid/projids are affected in a transaction, and
335  * that's rename case:
336  * - 3 for source parent uid & gid & projid;
337  * - 3 for source child uid & gid & projid ('..' entry update when
338  * child is directory);
339  * - 3 for target parent uid & gid & projid;
340  * - 3 for target child uid & gid & projid(if the target child exists);
341  * - 3 for root uid & gid(last_rcvd, llog, etc);
342  *
343  */
344 #define OSD_MAX_UGID_CNT        15
345
346 enum osd_op_type {
347         OSD_OT_ATTR_SET         = 0,
348         OSD_OT_PUNCH            = 1,
349         OSD_OT_XATTR_SET        = 2,
350         OSD_OT_CREATE           = 3,
351         OSD_OT_DESTROY          = 4,
352         OSD_OT_REF_ADD          = 5,
353         OSD_OT_REF_DEL          = 6,
354         OSD_OT_WRITE            = 7,
355         OSD_OT_INSERT           = 8,
356         OSD_OT_DELETE           = 9,
357         OSD_OT_QUOTA            = 10,
358         OSD_OT_MAX              = 11
359 };
360
361 struct osd_thandle {
362         struct thandle          ot_super;
363         handle_t               *ot_handle;
364         struct ldiskfs_journal_cb_entry ot_jcb;
365         struct list_head       ot_commit_dcb_list;
366         struct list_head       ot_stop_dcb_list;
367         /* Link to the device, for debugging. */
368         struct lu_ref_link      ot_dev_link;
369         unsigned int            ot_credits;
370         unsigned short          ot_id_cnt;
371         __u8                    ot_id_types[OSD_MAX_UGID_CNT];
372         unsigned int            ot_remove_agents:1;
373         uid_t                   ot_id_array[OSD_MAX_UGID_CNT];
374         struct lquota_trans    *ot_quota_trans;
375 #if OSD_THANDLE_STATS
376         /** time when this handle was allocated */
377         ktime_t oth_alloced;
378
379         /** time when this thanle was started */
380         ktime_t oth_started;
381 #endif
382 };
383
384 /**
385  * Basic transaction credit op
386  */
387 enum dt_txn_op {
388         DTO_INDEX_INSERT,
389         DTO_INDEX_DELETE,
390         DTO_INDEX_UPDATE,
391         DTO_OBJECT_CREATE,
392         DTO_OBJECT_DELETE,
393         DTO_ATTR_SET_BASE,
394         DTO_XATTR_SET,
395         DTO_WRITE_BASE,
396         DTO_WRITE_BLOCK,
397         DTO_ATTR_SET_CHOWN,
398
399         DTO_NR
400 };
401
402 /*
403  * osd dev stats
404  */
405
406 #ifdef CONFIG_PROC_FS
407 enum {
408         LPROC_OSD_READ_BYTES    = 0,
409         LPROC_OSD_WRITE_BYTES   = 1,
410         LPROC_OSD_GET_PAGE      = 2,
411         LPROC_OSD_NO_PAGE       = 3,
412         LPROC_OSD_CACHE_ACCESS  = 4,
413         LPROC_OSD_CACHE_HIT     = 5,
414         LPROC_OSD_CACHE_MISS    = 6,
415
416 #if OSD_THANDLE_STATS
417         LPROC_OSD_THANDLE_STARTING,
418         LPROC_OSD_THANDLE_OPEN,
419         LPROC_OSD_THANDLE_CLOSING,
420 #endif
421         LPROC_OSD_LAST,
422 };
423 #endif
424
425 /**
426  * Storage representation for fids.
427  *
428  * Variable size, first byte contains the length of the whole record.
429  */
430 struct osd_fid_pack {
431         unsigned char fp_len;
432         char fp_area[sizeof(struct lu_fid)];
433 };
434
435 struct osd_it_ea_dirent {
436         struct lu_fid   oied_fid;
437         __u64           oied_ino;
438         __u64           oied_off;
439         unsigned short  oied_namelen;
440         unsigned int    oied_type;
441         char            oied_name[0];
442 } __attribute__((packed));
443
444 /**
445  * as osd_it_ea_dirent (in memory dirent struct for osd) is greater
446  * than lu_dirent struct. osd readdir reads less number of dirent than
447  * required for mdd dir page. so buffer size need to be increased so that
448  * there  would be one ext3 readdir for every mdd readdir page.
449  */
450
451 #define OSD_IT_EA_BUFSIZE       (PAGE_SIZE + PAGE_SIZE/4)
452
453 /**
454  * This is iterator's in-memory data structure in interoperability
455  * mode (i.e. iterator over ldiskfs style directory)
456  */
457 struct osd_it_ea {
458         struct osd_object       *oie_obj;
459         /** used in ldiskfs iterator, to stored file pointer */
460         struct file             oie_file;
461         /** how many entries have been read-cached from storage */
462         int                     oie_rd_dirent;
463         /** current entry is being iterated by caller */
464         int                     oie_it_dirent;
465         /** current processing entry */
466         struct osd_it_ea_dirent *oie_dirent;
467         /** buffer to hold entries, size == OSD_IT_EA_BUFSIZE */
468         void                    *oie_buf;
469         struct dentry           oie_dentry;
470 };
471
472 /**
473  * Iterator's in-memory data structure for IAM mode.
474  */
475 struct osd_it_iam {
476         struct osd_object     *oi_obj;
477         struct iam_path_descr *oi_ipd;
478         struct iam_iterator    oi_it;
479 };
480
481 struct osd_quota_leaf {
482         struct list_head        oql_link;
483         uint            oql_blk;
484 };
485
486 /**
487  * Iterator's in-memory data structure for quota file.
488  */
489 struct osd_it_quota {
490         struct osd_object       *oiq_obj;
491         /** tree blocks path to where the entry is stored */
492         uint                     oiq_blk[LUSTRE_DQTREEDEPTH + 1];
493         /** on-disk offset for current key where quota record can be found */
494         loff_t                   oiq_offset;
495         /** identifier for current quota record */
496         __u64                    oiq_id;
497         /** the record index in the leaf/index block */
498         uint                     oiq_index[LUSTRE_DQTREEDEPTH + 1];
499         /** list of already processed leaf blocks */
500         struct list_head         oiq_list;
501 };
502
503 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
504
505 struct osd_iobuf {
506         wait_queue_head_t  dr_wait;
507         atomic_t       dr_numreqs;  /* number of reqs being processed */
508         int                dr_max_pages;
509         int                dr_npages;
510         int                dr_error;
511         int                dr_frags;
512         unsigned int       dr_ignore_quota:1;
513         unsigned int       dr_elapsed_valid:1; /* we really did count time */
514         unsigned int       dr_rw:1;
515         struct lu_buf      dr_pg_buf;
516         struct page      **dr_pages;
517         struct lu_buf      dr_bl_buf;
518         sector_t          *dr_blocks;
519         ktime_t            dr_start_time;
520         ktime_t            dr_elapsed;  /* how long io took */
521         struct osd_device *dr_dev;
522         unsigned int       dr_init_at;  /* the line iobuf was initialized */
523 };
524
525 #define OSD_INS_CACHE_SIZE      8
526
527 struct osd_thread_info {
528         const struct lu_env   *oti_env;
529         /**
530          * used for index operations.
531          */
532         struct dentry          oti_obj_dentry;
533         struct dentry          oti_child_dentry;
534
535         /** dentry for Iterator context. */
536         struct dentry           oti_it_dentry;
537
538         union {
539                 /* fake struct file for osd_object_sync */
540                 struct file             oti_file;
541                 /* osd_statfs() */
542                 struct kstatfs          oti_ksfs;
543         };
544
545         struct htree_lock     *oti_hlock;
546
547         struct lu_fid          oti_fid;
548         struct lu_fid          oti_fid2;
549         struct lu_fid          oti_fid3;
550         struct osd_inode_id    oti_id;
551         struct osd_inode_id    oti_id2;
552         struct osd_inode_id    oti_id3;
553         struct ost_id          oti_ostid;
554
555         /*
556          * XXX temporary: for ->i_op calls.
557          */
558         struct timespec        oti_time;
559
560         /**
561          * following ipd and it structures are used for osd_index_iam_lookup()
562          * these are defined separately as we might do index operation
563          * in open iterator session.
564          */
565
566         /** pre-allocated buffer used by oti_it_ea, size OSD_IT_EA_BUFSIZE */
567         void                    *oti_it_ea_buf;
568         unsigned int            oti_it_ea_buf_used:1;
569
570         /* IAM iterator for index operation. */
571         struct iam_iterator    oti_idx_it;
572
573         /** union to guarantee that ->oti_ipd[] has proper alignment. */
574         union {
575                 char           oti_name[48];
576                 char           oti_it_ipd[DX_IPD_MAX_SIZE];
577                 long long      oti_alignment_lieutenant;
578         };
579
580         union {
581                 char           oti_idx_ipd[DX_IPD_MAX_SIZE];
582                 long long      oti_alignment_lieutenant_colonel;
583         };
584
585         struct osd_idmap_cache oti_cache;
586
587         /* dedicated OI cache for insert (which needs inum) */
588         struct osd_idmap_cache *oti_ins_cache;
589         int                    oti_ins_cache_size;
590         int                    oti_ins_cache_used;
591         /* inc by osd_trans_create and dec by osd_trans_stop */
592         int                    oti_ins_cache_depth;
593
594         int                    oti_r_locks;
595         int                    oti_w_locks;
596         int                    oti_txns;
597         /** used in osd_fid_set() to put xattr */
598         struct lu_buf          oti_buf;
599         struct lu_buf          oti_big_buf;
600         /** used in osd_ea_fid_set() to set fid into common ea */
601         union {
602                 struct lustre_ost_attrs oti_ost_attrs;
603                 struct filter_fid_old   oti_ff;
604                 struct filter_fid       oti_ff_new;
605         };
606         /** 0-copy IO */
607         struct osd_iobuf       oti_iobuf;
608         /* used to access objects in /O */
609         struct inode          *oti_inode;
610 #define OSD_FID_REC_SZ 32
611         char                   oti_ldp[OSD_FID_REC_SZ];
612         char                   oti_ldp2[OSD_FID_REC_SZ];
613
614         /* used by quota code */
615         union {
616 #if defined(HAVE_DQUOT_QC_DQBLK)
617                 struct qc_dqblk         oti_qdq;
618 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
619                 struct fs_disk_quota    oti_fdq;
620 #else
621                 struct if_dqblk         oti_dqblk;
622 #endif
623                 struct if_dqinfo        oti_dqinfo;
624         };
625         struct lquota_id_info   oti_qi;
626         struct lquota_trans     oti_quota_trans;
627         union lquota_rec        oti_quota_rec;
628         __u64                   oti_quota_id;
629         struct lu_seq_range     oti_seq_range;
630
631         /* Tracking for transaction credits, to allow debugging and optimizing
632          * cases where a large number of credits are being allocated for
633          * single transaction. */
634         unsigned int            oti_credits_before;
635         unsigned int            oti_declare_ops[OSD_OT_MAX];
636         unsigned int            oti_declare_ops_cred[OSD_OT_MAX];
637         unsigned int            oti_declare_ops_used[OSD_OT_MAX];
638         struct osd_directory    oti_iam;
639 };
640
641 extern int ldiskfs_pdo;
642
643 #ifndef HAVE_VFS_SETXATTR
644 #define osd_setxattr(dentry, inode, name, buf, len, flag) \
645                 ((inode)->i_op->setxattr(dentry, name, buf, len, flag))
646 #define osd_getxattr(dentry, inode, name, buf, len) \
647                 ((inode)->i_op->getxattr(dentry, name, buf, len))
648 #define osd_removexattr(dentry, inode, name) \
649                 ((inode)->i_op->removexattr(dentry, name))
650 #else /* HAVE_VFS_SETXATTR */
651 #define osd_setxattr(dentry, inode, name, buf, len, flag) \
652                 __vfs_setxattr(dentry, inode, name, buf, len, flag)
653 #define osd_getxattr(dentry, inode, name, buf, len) \
654                 __vfs_getxattr(dentry, inode, name, buf, len)
655 #define osd_removexattr(dentry, inode, name) \
656                 __vfs_removexattr(dentry, name)
657 #endif /* !HAVE_VFS_SETXATTR */
658
659 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
660                                   const char *name, void *buf, int len)
661 {
662         if (inode == NULL)
663                 return -EINVAL;
664
665         dentry->d_inode = inode;
666         dentry->d_sb = inode->i_sb;
667         return osd_getxattr(dentry, inode, name, buf, len);
668 }
669
670 static inline int __osd_xattr_set(struct osd_thread_info *info,
671                                   struct inode *inode, const char *name,
672                                   const void *buf, int buflen, int fl)
673 {
674         struct dentry *dentry = &info->oti_child_dentry;
675
676         ll_vfs_dq_init(inode);
677         dentry->d_inode = inode;
678         dentry->d_sb = inode->i_sb;
679         return osd_setxattr(dentry, inode, name, buf, buflen, fl);
680 }
681
682 #ifdef CONFIG_PROC_FS
683 /* osd_lproc.c */
684 extern struct lprocfs_vars lprocfs_osd_obd_vars[];
685 extern struct lprocfs_vars lprocfs_osd_module_vars[];
686 int osd_procfs_init(struct osd_device *osd, const char *name);
687 int osd_procfs_fini(struct osd_device *osd);
688 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf);
689 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
690 int osd_register_proc_index_in_idif(struct osd_device *osd);
691 #endif
692
693 #endif
694 int osd_statfs(const struct lu_env *env, struct dt_device *dev,
695                struct obd_statfs *sfs);
696 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
697                        struct osd_inode_id *id);
698 struct inode *
699 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
700              struct osd_inode_id *id, struct lu_fid *fid);
701 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
702                    const struct lu_fid *fid, __u32 compat, __u32 incompat);
703 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
704                 struct dentry *dentry, struct lustre_ost_attrs *loa);
705 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
706                       struct osd_inode_id *id, const struct lu_fid *fid);
707 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
708                  struct dentry *dentry, struct lu_fid *fid);
709
710 int osd_obj_map_init(const struct lu_env *env, struct osd_device *osd);
711 void osd_obj_map_fini(struct osd_device *dev);
712 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *osd,
713                         const struct lu_fid *fid, struct osd_inode_id *id);
714 int osd_obj_map_insert(struct osd_thread_info *info, struct osd_device *osd,
715                        const struct lu_fid *fid, const struct osd_inode_id *id,
716                        handle_t *th);
717 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
718                         const struct lu_fid *fid, handle_t *th);
719 int osd_obj_map_update(struct osd_thread_info *info, struct osd_device *osd,
720                        const struct lu_fid *fid, const struct osd_inode_id *id,
721                        handle_t *th);
722 int osd_obj_map_recover(struct osd_thread_info *info, struct osd_device *osd,
723                         struct inode *src_parent, struct dentry *src_child,
724                         const struct lu_fid *fid);
725 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
726                         const struct lu_fid *fid, struct osd_inode_id *id);
727 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
728                         const struct lu_fid *fid, const struct osd_inode_id *id,
729                         handle_t *th);
730 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
731                         const struct lu_fid *fid, const struct osd_inode_id *id,
732                         handle_t *th);
733
734 char *osd_lf_fid2name(const struct lu_fid *fid);
735 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
736                     __u32 flags);
737 void osd_scrub_stop(struct osd_device *dev);
738 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev);
739 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev);
740 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
741                    int insert);
742 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
743                    struct osd_inode_id *id);
744 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev);
745
746 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
747                    u64 seq, struct lu_seq_range *range);
748
749 int osd_delete_from_remote_parent(const struct lu_env *env,
750                                   struct osd_device *osd,
751                                   struct osd_object *obj,
752                                   struct osd_thandle *oh, bool destroy);
753 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
754                              struct osd_object *obj, struct osd_thandle *oh);
755 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
756                                 struct osd_device *osd,
757                                 const struct lu_fid *fid,
758                                 struct osd_inode_id *id);
759
760 int osd_ost_seq_exists(struct osd_thread_info *info, struct osd_device *osd,
761                        __u64 seq);
762 /* osd_quota_fmt.c */
763 int walk_tree_dqentry(const struct lu_env *env, struct osd_object *obj,
764                       int type, uint blk, int depth, uint index,
765                       struct osd_it_quota *it);
766 int walk_block_dqentry(const struct lu_env *env, struct osd_object *obj,
767                        int type, uint blk, uint index,
768                        struct osd_it_quota *it);
769 loff_t find_tree_dqentry(const struct lu_env *env,
770                          struct osd_object *obj, int type,
771                          qid_t dqid, uint blk, int depth,
772                          struct osd_it_quota *it);
773 /* osd_quota.c */
774 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
775                     struct lquota_id_info *qi, struct osd_object *obj,
776                     bool enforce, int *flags);
777 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
778                           __u32 projid, long long space, struct osd_thandle *oh,
779                           struct osd_object *obj, int *flags,
780                           enum osd_qid_declare_flags);
781 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
782                                     const struct dt_rec *rec,
783                                     union lquota_rec *quota_rec);
784 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec);
785
786 #ifndef HAVE_I_UID_READ
787 static inline uid_t i_uid_read(const struct inode *inode)
788 {
789         return inode->i_uid;
790 }
791
792 static inline gid_t i_gid_read(const struct inode *inode)
793 {
794         return inode->i_gid;
795 }
796
797 static inline void i_uid_write(struct inode *inode, uid_t uid)
798 {
799         inode->i_uid = uid;
800 }
801
802 static inline void i_gid_write(struct inode *inode, gid_t gid)
803 {
804         inode->i_gid = gid;
805 }
806 #endif
807
808 #ifdef HAVE_PROJECT_QUOTA
809 static inline __u32 i_projid_read(struct inode *inode)
810 {
811         return (__u32)from_kprojid(&init_user_ns, LDISKFS_I(inode)->i_projid);
812 }
813
814 static inline void i_projid_write(struct inode *inode, __u32 projid)
815 {
816         kprojid_t kprojid;
817         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
818         LDISKFS_I(inode)->i_projid = kprojid;
819 }
820 #else
821 static inline uid_t i_projid_read(struct inode *inode)
822 {
823         return 0;
824 }
825 static inline void i_projid_write(struct inode *inode, __u32 projid)
826 {
827         return;
828 }
829 #endif
830
831 #ifdef HAVE_LDISKFS_INFO_JINODE
832 # define osd_attach_jinode(inode) ldiskfs_inode_attach_jinode(inode)
833 #else  /* HAVE_LDISKFS_INFO_JINODE */
834 # define osd_attach_jinode(inode) 0
835 #endif /* HAVE_LDISKFS_INFO_JINODE */
836
837 #ifdef LDISKFS_HT_MISC
838 # define osd_journal_start_sb(sb, type, nblock) \
839                 ldiskfs_journal_start_sb(sb, type, nblock)
840 static inline struct buffer_head *osd_ldiskfs_append(handle_t *handle,
841                                                      struct inode *inode,
842                                                      ldiskfs_lblk_t *nblock)
843 {
844         int rc;
845
846         rc = osd_attach_jinode(inode);
847         if (rc)
848                 return ERR_PTR(rc);
849         return ldiskfs_append(handle, inode, nblock);
850 }
851 # define osd_ldiskfs_find_entry(dir, name, de, inlined, lock) \
852                 (__ldiskfs_find_entry(dir, name, de, inlined, lock) ?: \
853                  ERR_PTR(-ENOENT))
854 # define osd_journal_start(inode, type, nblocks) \
855                 ldiskfs_journal_start(inode, type, nblocks)
856 # define osd_transaction_size(dev) \
857                 (osd_journal(dev)->j_max_transaction_buffers / 2)
858 #else
859 # define LDISKFS_HT_MISC        0
860 # define osd_journal_start_sb(sb, type, nblock) \
861                 ldiskfs_journal_start_sb(sb, nblock)
862
863 static inline struct buffer_head *osd_ldiskfs_append(handle_t *handle,
864                                                      struct inode *inode,
865                                                      ldiskfs_lblk_t *nblock)
866 {
867         struct buffer_head *bh;
868         int err = 0;
869
870         bh = ldiskfs_append(handle, inode, nblock, &err);
871         if (bh == NULL)
872                 bh = ERR_PTR(err);
873
874         return bh;
875 }
876
877 # define osd_ldiskfs_find_entry(dir, name, de, inlined, lock) \
878                 (__ldiskfs_find_entry(dir, name, de, lock) ?: \
879                  ERR_PTR(-ENOENT))
880 # define osd_journal_start(inode, type, nblocks) \
881                 ldiskfs_journal_start(inode, nblocks)
882 # define osd_transaction_size(dev) \
883                 (osd_journal(dev)->j_max_transaction_buffers)
884 #endif
885
886 /*
887  * Invariants, assertions.
888  */
889
890 /*
891  * XXX: do not enable this, until invariant checking code is made thread safe
892  * in the face of pdirops locking.
893  */
894 #define OSD_INVARIANT_CHECKS (0)
895
896 #if OSD_INVARIANT_CHECKS
897 static inline int osd_invariant(const struct osd_object *obj)
898 {
899         return
900                 obj != NULL &&
901                 ergo(obj->oo_inode != NULL,
902                      obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
903                      atomic_read(&obj->oo_inode->i_count) > 0) &&
904                 ergo(obj->oo_dir != NULL &&
905                      obj->oo_dir->od_conationer.ic_object != NULL,
906                      obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
907 }
908 #else
909 #define osd_invariant(obj) (1)
910 #endif
911
912 #define OSD_MAX_CACHE_SIZE OBD_OBJECT_EOF
913
914 extern const struct dt_index_operations osd_otable_ops;
915
916 static inline int osd_oi_fid2idx(struct osd_device *dev,
917                                  const struct lu_fid *fid)
918 {
919         return fid->f_seq & (dev->od_oi_count - 1);
920 }
921
922 static inline struct osd_oi *osd_fid2oi(struct osd_device *osd,
923                                         const struct lu_fid *fid)
924 {
925         LASSERTF(!fid_is_idif(fid), DFID"\n", PFID(fid));
926         LASSERTF(!fid_is_last_id(fid), DFID"\n", PFID(fid));
927         LASSERTF(osd->od_oi_table != NULL && osd->od_oi_count >= 1,
928                  DFID"\n", PFID(fid));
929         /* It can work even od_oi_count equals to 1 although it's unexpected,
930          * the only reason we set it to 1 is for performance measurement */
931         return osd->od_oi_table[osd_oi_fid2idx(osd, fid)];
932 }
933
934 extern const struct lu_device_operations  osd_lu_ops;
935
936 static inline int lu_device_is_osd(const struct lu_device *d)
937 {
938         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
939 }
940
941 static inline struct osd_device *osd_dt_dev(const struct dt_device *d)
942 {
943         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
944         return container_of0(d, struct osd_device, od_dt_dev);
945 }
946
947 static inline struct osd_device *osd_dev(const struct lu_device *d)
948 {
949         LASSERT(lu_device_is_osd(d));
950         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
951 }
952
953 static inline struct osd_device *osd_obj2dev(const struct osd_object *o)
954 {
955         return osd_dev(o->oo_dt.do_lu.lo_dev);
956 }
957
958 static inline struct super_block *osd_sb(const struct osd_device *dev)
959 {
960         return dev->od_mnt->mnt_sb;
961 }
962
963 static inline const char *osd_dev2name(const struct osd_device *dev)
964 {
965         return osd_sb(dev)->s_id;
966 }
967
968 static inline const char *osd_ino2name(const struct inode *inode)
969 {
970         return inode->i_sb->s_id;
971 }
972
973 /**
974  * Put the osd object once done with it.
975  *
976  * \param obj osd object that needs to be put
977  */
978 static inline void osd_object_put(const struct lu_env *env,
979                                   struct osd_object *obj)
980 {
981         dt_object_put(env, &obj->oo_dt);
982 }
983
984 static inline int osd_object_is_root(const struct osd_object *obj)
985 {
986         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
987 }
988
989 static inline struct osd_object *osd_obj(const struct lu_object *o)
990 {
991         LASSERT(lu_device_is_osd(o->lo_dev));
992         return container_of0(o, struct osd_object, oo_dt.do_lu);
993 }
994
995 static inline struct osd_object *osd_dt_obj(const struct dt_object *d)
996 {
997         return osd_obj(&d->do_lu);
998 }
999
1000 static inline struct lu_device *osd2lu_dev(struct osd_device *osd)
1001 {
1002         return &osd->od_dt_dev.dd_lu_dev;
1003 }
1004
1005 static inline journal_t *osd_journal(const struct osd_device *dev)
1006 {
1007         return LDISKFS_SB(osd_sb(dev))->s_journal;
1008 }
1009
1010 static inline struct seq_server_site *osd_seq_site(struct osd_device *osd)
1011 {
1012         return osd->od_dt_dev.dd_lu_dev.ld_site->ld_seq_site;
1013 }
1014
1015 static inline char *osd_name(struct osd_device *osd)
1016 {
1017         return osd->od_svname;
1018 }
1019
1020 static inline bool osd_is_ea_inode(struct inode *inode)
1021 {
1022         return !!(LDISKFS_I(inode)->i_flags & LDISKFS_EA_INODE_FL);
1023 }
1024
1025 extern const struct dt_body_operations osd_body_ops;
1026 extern struct lu_context_key osd_key;
1027
1028 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
1029 {
1030         return lu_context_key_get(&env->le_ctx, &osd_key);
1031 }
1032
1033 extern const struct dt_body_operations osd_body_ops_new;
1034
1035 /**
1036  * IAM Iterator
1037  */
1038 static inline
1039 struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
1040                                       const struct iam_container *bag)
1041 {
1042         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
1043                                            osd_oti_get(env)->oti_it_ipd);
1044 }
1045
1046 static inline
1047 struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
1048                                        const struct iam_container *bag)
1049 {
1050         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
1051                                            osd_oti_get(env)->oti_idx_ipd);
1052 }
1053
1054 static inline void osd_ipd_put(const struct lu_env *env,
1055                                const struct iam_container *bag,
1056                                struct iam_path_descr *ipd)
1057 {
1058         bag->ic_descr->id_ops->id_ipd_free(ipd);
1059 }
1060
1061 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1062                            const loff_t size, const loff_t pos,
1063                            const int blocks);
1064
1065 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
1066 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1067                              int write_NUL, loff_t *offs, handle_t *handle);
1068
1069 static inline
1070 struct dentry *osd_child_dentry_by_inode(const struct lu_env *env,
1071                                          struct inode *inode,
1072                                          const char *name, const int namelen)
1073 {
1074         struct osd_thread_info *info = osd_oti_get(env);
1075         struct dentry *child_dentry = &info->oti_child_dentry;
1076         struct dentry *obj_dentry = &info->oti_obj_dentry;
1077
1078         obj_dentry->d_inode = inode;
1079         obj_dentry->d_sb = inode->i_sb;
1080         obj_dentry->d_name.hash = 0;
1081
1082         child_dentry->d_name.hash = 0;
1083         child_dentry->d_parent = obj_dentry;
1084         child_dentry->d_name.name = name;
1085         child_dentry->d_name.len = namelen;
1086         return child_dentry;
1087 }
1088
1089 extern int osd_trans_declare_op2rb[];
1090 extern int ldiskfs_track_declares_assert;
1091 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th);
1092
1093 static inline void osd_trans_declare_op(const struct lu_env *env,
1094                                         struct osd_thandle *oh,
1095                                         enum osd_op_type op, int credits)
1096 {
1097         struct osd_thread_info *oti = osd_oti_get(env);
1098
1099         LASSERT(oh->ot_handle == NULL);
1100         if (unlikely(op >= OSD_OT_MAX)) {
1101                 if (unlikely(ldiskfs_track_declares_assert)) {
1102                         LASSERT(op < OSD_OT_MAX);
1103                 } else {
1104                         CWARN("%s: Invalid operation index %d\n",
1105                               osd_name(osd_dt_dev(oh->ot_super.th_dev)), op);
1106                         libcfs_debug_dumpstack(NULL);
1107                 }
1108         } else {
1109                 oti->oti_declare_ops[op]++;
1110                 oti->oti_declare_ops_cred[op] += credits;
1111         }
1112         oh->ot_credits += credits;
1113 }
1114
1115 static inline void osd_trans_exec_op(const struct lu_env *env,
1116                                      struct thandle *th,
1117                                      enum osd_op_type op)
1118 {
1119         struct osd_thread_info *oti = osd_oti_get(env);
1120         struct osd_thandle     *oh  = container_of(th, struct osd_thandle,
1121                                                    ot_super);
1122         unsigned int            rb, left;
1123
1124         LASSERT(oh->ot_handle != NULL);
1125         if (unlikely(op >= OSD_OT_MAX)) {
1126                 if (unlikely(ldiskfs_track_declares_assert))
1127                         LASSERT(op < OSD_OT_MAX);
1128                 else {
1129                         CWARN("%s: opcode %u: invalid value >= %u\n",
1130                               osd_name(osd_dt_dev(oh->ot_super.th_dev)),
1131                               op, OSD_OT_MAX);
1132                         libcfs_debug_dumpstack(NULL);
1133                         return;
1134                 }
1135         }
1136
1137         /* find rollback (or reverse) operation for the given one
1138          * such an operation doesn't require additional credits
1139          * as the same set of blocks are modified */
1140         rb = osd_trans_declare_op2rb[op];
1141
1142         /* check whether credits for this operation were reserved at all */
1143         if (unlikely(oti->oti_declare_ops_cred[op] == 0 &&
1144                      oti->oti_declare_ops_cred[rb] == 0)) {
1145                 /* the API is not perfect yet: CREATE does REF_ADD internally
1146                  * while DESTROY does not. To rollback CREATE the callers
1147                  * needs to call REF_DEL+DESTROY which is hard to detect using
1148                  * a simple table of rollback operations */
1149                 if (op == OSD_OT_REF_DEL &&
1150                     oti->oti_declare_ops_cred[OSD_OT_CREATE] > 0)
1151                         goto proceed;
1152                 if (op == OSD_OT_REF_ADD &&
1153                     oti->oti_declare_ops_cred[OSD_OT_DESTROY] > 0)
1154                         goto proceed;
1155                 CWARN("%s: opcode %u: credits = 0, rollback = %u\n",
1156                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op, rb);
1157                 osd_trans_dump_creds(env, th);
1158                 LASSERT(!ldiskfs_track_declares_assert);
1159         }
1160
1161 proceed:
1162         /* remember how many credits we have unused before the operation */
1163         oti->oti_credits_before = oh->ot_handle->h_buffer_credits;
1164         left = oti->oti_declare_ops_cred[op] - oti->oti_declare_ops_used[op];
1165         if (unlikely(oti->oti_credits_before < left)) {
1166                 CWARN("%s: opcode %u: before %u < left %u, rollback = %u\n",
1167                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op,
1168                       oti->oti_credits_before, left, rb);
1169                 osd_trans_dump_creds(env, th);
1170                 /* on a very small fs (testing?) it's possible that
1171                  * the transaction can't fit 1/4 of journal, so we
1172                  * just request less credits (see osd_trans_start()).
1173                  * ignore the same case here */
1174                 rb = osd_transaction_size(osd_dt_dev(th->th_dev));
1175                 if (unlikely(oh->ot_credits < rb))
1176                         LASSERT(!ldiskfs_track_declares_assert);
1177         }
1178 }
1179
1180 static inline void osd_trans_exec_check(const struct lu_env *env,
1181                                         struct thandle *th,
1182                                         enum osd_op_type op)
1183 {
1184         struct osd_thread_info *oti = osd_oti_get(env);
1185         struct osd_thandle     *oh  = container_of(th, struct osd_thandle,
1186                                                    ot_super);
1187         int                     used, over, quota;
1188
1189         /* how many credits have been used by the operation */
1190         used = oti->oti_credits_before - oh->ot_handle->h_buffer_credits;
1191
1192         if (unlikely(used < 0)) {
1193                 /* if some block was allocated and released in the same
1194                  * transaction, then it won't be a part of the transaction
1195                  * and delta can be negative */
1196                 return;
1197         }
1198
1199         if (used == 0) {
1200                 /* rollback operations (e.g. when we destroy just created
1201                  * object) should not consume any credits. there is no point
1202                  * to confuse the checks below */
1203                 return;
1204         }
1205
1206         oti->oti_declare_ops_used[op] += used;
1207         if (oti->oti_declare_ops_used[op] <= oti->oti_declare_ops_cred[op])
1208                 return;
1209
1210         /* we account quota for a whole transaction and any operation can
1211          * consume corresponding credits */
1212         over = oti->oti_declare_ops_used[op] -
1213                 oti->oti_declare_ops_cred[op];
1214         quota = oti->oti_declare_ops_cred[OSD_OT_QUOTA] -
1215                 oti->oti_declare_ops_used[OSD_OT_QUOTA];
1216         if (over <= quota) {
1217                 /* probably that credits were consumed by
1218                  * quota indirectly (in the depths of ldiskfs) */
1219                 oti->oti_declare_ops_used[OSD_OT_QUOTA] += over;
1220                 oti->oti_declare_ops_used[op] -= over;
1221         } else {
1222                 CWARN("%s: opcode %d: used %u, used now %u, reserved %u\n",
1223                       osd_name(osd_dt_dev(oh->ot_super.th_dev)), op,
1224                       oti->oti_declare_ops_used[op], used,
1225                       oti->oti_declare_ops_cred[op]);
1226                 osd_trans_dump_creds(env, th);
1227                 if (unlikely(ldiskfs_track_declares_assert))
1228                         LBUG();
1229         }
1230 }
1231
1232 /**
1233  * Helper function to pack the fid, ldiskfs stores fid in packed format.
1234  */
1235 static inline
1236 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1237                   struct lu_fid *befider)
1238 {
1239         fid_cpu_to_be(befider, (struct lu_fid *)fid);
1240         memcpy(pack->fp_area, befider, sizeof(*befider));
1241         pack->fp_len =  sizeof(*befider) + 1;
1242 }
1243
1244 static inline
1245 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1246 {
1247         int result;
1248
1249         result = 0;
1250         switch (pack->fp_len) {
1251         case sizeof *fid + 1:
1252                 memcpy(fid, pack->fp_area, sizeof *fid);
1253                 fid_be_to_cpu(fid, fid);
1254                 break;
1255         default:
1256                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1257                 result = -EIO;
1258         }
1259         return result;
1260 }
1261
1262 /**
1263  * Quota/Accounting handling
1264  */
1265 extern const struct dt_index_operations osd_acct_index_ops;
1266 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
1267                         const struct lu_fid *fid, struct osd_inode_id *id);
1268
1269 /* copy from fs/ext4/dir.c */
1270 static inline int is_32bit_api(void)
1271 {
1272 #ifdef CONFIG_COMPAT
1273         return in_compat_syscall();
1274 #else
1275         return (BITS_PER_LONG == 32);
1276 #endif
1277 }
1278
1279 static inline loff_t ldiskfs_get_htree_eof(struct file *filp)
1280 {
1281         if ((filp->f_mode & FMODE_32BITHASH) ||
1282             (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
1283                 return LDISKFS_HTREE_EOF_32BIT;
1284         else
1285                 return LDISKFS_HTREE_EOF_64BIT;
1286 }
1287
1288 static inline int fid_is_internal(const struct lu_fid *fid)
1289 {
1290         return (!fid_is_namespace_visible(fid) && !fid_is_idif(fid));
1291 }
1292
1293 static inline bool is_remote_parent_ino(struct osd_device *o, unsigned long ino)
1294 {
1295         if (o->od_is_ost)
1296                 return false;
1297
1298         LASSERT(o->od_mdt_map != NULL);
1299
1300         return ino == o->od_mdt_map->omm_remote_parent->d_inode->i_ino;
1301 }
1302
1303 /**
1304  * ext4_bread/ldiskfs_bread has either 5 or 4 parameters. The error
1305  * return code has been removed and integrated into the pointer in the
1306  * kernel 3.18.
1307  */
1308 static inline struct buffer_head *__ldiskfs_bread(handle_t *handle,
1309                                                   struct inode *inode,
1310                                                   ldiskfs_lblk_t block,
1311                                                   int create)
1312 {
1313         int rc = 0;
1314         struct buffer_head *bh;
1315
1316         if (create) {
1317                 rc = osd_attach_jinode(inode);
1318                 if (rc)
1319                         return ERR_PTR(rc);
1320         }
1321 #ifdef HAVE_EXT4_BREAD_4ARGS
1322         bh = ldiskfs_bread(handle, inode, block, create);
1323 #else
1324
1325         bh = ldiskfs_bread(handle, inode, block, create, &rc);
1326         if (bh == NULL && rc != 0)
1327                 bh = ERR_PTR(rc);
1328 #endif
1329         return bh;
1330 }
1331
1332 #ifndef bio_set_dev
1333 # define bio_set_dev(bio, bdev) ((bio)->bi_bdev = bdev)
1334 # define bio_get_queue(bio)     bdev_get_queue((bio)->bi_bdev)
1335 #else
1336 # define bio_get_queue(bio)     ((bio)->bi_disk->queue)
1337 #endif
1338
1339 void ldiskfs_inc_count(handle_t *handle, struct inode *inode);
1340 void ldiskfs_dec_count(handle_t *handle, struct inode *inode);
1341
1342 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf);
1343
1344 static inline int
1345 osd_index_register(struct osd_device *osd, const struct lu_fid *fid,
1346                    __u32 keysize, __u32 recsize)
1347 {
1348         return lustre_index_register(&osd->od_dt_dev, osd_name(osd),
1349                                      &osd->od_index_backup_list, &osd->od_lock,
1350                                      &osd->od_index_backup_stop,
1351                                      fid, keysize, recsize);
1352 }
1353
1354 static inline void
1355 osd_index_backup(const struct lu_env *env, struct osd_device *osd, bool backup)
1356 {
1357         struct osd_thread_info *info = osd_oti_get(env);
1358         struct lu_fid *fid = &info->oti_fid3;
1359         struct osd_inode_id *id = &info->oti_id3;
1360
1361         lu_local_obj_fid(fid, INDEX_BACKUP_OID);
1362         osd_id_gen(id, osd->od_index_backup_inode->i_ino,
1363                    osd->od_index_backup_inode->i_generation);
1364         osd_add_oi_cache(info, osd, id, fid);
1365
1366         lustre_index_backup(env, &osd->od_dt_dev, osd_name(osd),
1367                             &osd->od_index_backup_list, &osd->od_lock,
1368                             &osd->od_index_backup_stop, backup);
1369 }
1370
1371 #ifdef LDISKFS_HAS_INCOMPAT_FEATURE
1372
1373 # ifdef LDISKFS_FEATURE_INCOMPAT_EXTENTS
1374 # define ldiskfs_has_feature_extents(sb) \
1375         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS)
1376 # endif
1377 # ifdef LDISKFS_FEATURE_INCOMPAT_EA_INODE
1378 # define ldiskfs_has_feature_ea_inode(sb) \
1379         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1380 # endif
1381 # ifdef LDISKFS_FEATURE_INCOMPAT_DIRDATA
1382 # define ldiskfs_has_feature_dirdata(sb) \
1383         LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_DIRDATA)
1384 # endif
1385 # ifdef LDISKFS_FEATURE_COMPAT_HAS_JOURNAL
1386 # define ldiskfs_has_feature_journal(sb) \
1387         LDISKFS_HAS_COMPAT_FEATURE(sb, LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)
1388 # endif
1389 # ifdef LDISKFS_FEATURE_RO_COMPAT_QUOTA
1390 # define ldiskfs_has_feature_quota(sb) \
1391         LDISKFS_HAS_RO_COMPAT_FEATURE(sb, LDISKFS_FEATURE_RO_COMPAT_QUOTA)
1392 # endif
1393 # ifdef LDISKFS_FEATURE_RO_COMPAT_PROJECT
1394 # define ldiskfs_has_feature_project(sb) \
1395         LDISKFS_HAS_RO_COMPAT_FEATURE(sb, LDISKFS_FEATURE_RO_COMPAT_PROJECT)
1396 # endif
1397
1398 #endif
1399
1400 #endif /* _OSD_INTERNAL_H */