Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[fs/lustre-release.git] / lustre / mdt / mdt_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  *
31  * lustre/mdt/mdt_internal.h
32  *
33  * Lustre Metadata Target (mdt) request handler
34  *
35  * Author: Peter Braam <braam@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  * Author: Phil Schwan <phil@clusterfs.com>
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  * Author: Nikita Danilov <nikita@clusterfs.com>
40  * Author: Huang Hua <huanghua@clusterfs.com>
41  */
42
43 #ifndef _MDT_INTERNAL_H
44 #define _MDT_INTERNAL_H
45
46 #include <libcfs/libcfs.h>
47 #include <libcfs/libcfs_hash.h>
48 #include <upcall_cache.h>
49 #include <obd_class.h>
50 #include <lustre_disk.h>
51 #include <lu_target.h>
52 #include <md_object.h>
53 #include <lustre_fid.h>
54 #include <lustre_fld.h>
55 #include <lustre_req_layout.h>
56 #include <lustre_sec.h>
57 #include <lustre_idmap.h>
58 #include <lustre_quota.h>
59 #include <lustre_linkea.h>
60 #include <lustre_lmv.h>
61
62 struct mdt_object;
63
64 /* file data for open files on MDS */
65 struct mdt_file_data {
66         /**  portals handle must be first */
67         struct portals_handle   mfd_open_handle;
68         /* export data of portals_handle */
69         const struct mdt_export_data    *mfd_owner;
70         /** open mode provided by client */
71         u64                     mfd_open_flags;
72         /** protected by med_open_lock */
73         struct list_head        mfd_list;
74         /** xid of the open request */
75         __u64                   mfd_xid;
76         /** old handle in replay case */
77         struct lustre_handle    mfd_open_handle_old;
78         /** point to opened object */
79         struct mdt_object       *mfd_object;
80 };
81
82 #define CDT_NONBLOCKING_RESTORE         (1ULL << 0)
83 #define CDT_NORETRY_ACTION              (1ULL << 1)
84 #define CDT_POLICY_LAST                 CDT_NORETRY_ACTION
85 #define CDT_POLICY_SHIFT_COUNT          2
86 #define CDT_POLICY_ALL                  (CDT_NONBLOCKING_RESTORE | \
87                                         CDT_NORETRY_ACTION)
88
89 /* when adding a new policy, do not forget to update
90  * lustre/mdt/mdt_coordinator.c::hsm_policy_names[]
91  */
92 #define CDT_DEFAULT_POLICY              CDT_NORETRY_ACTION
93
94 /* Coordinator states. Keep the cdt_transition table in sync. */
95 enum cdt_states { CDT_STOPPED = 0,
96                   CDT_INIT,
97                   CDT_RUNNING,
98                   CDT_DISABLE,
99                   CDT_STOPPING,
100
101                   CDT_STATES_COUNT
102 };
103
104 static inline char *cdt_mdt_state2str(int state)
105 {
106         switch (state) {
107         case CDT_INIT:
108                 return "init";
109         case CDT_RUNNING:
110                 return "enabled";
111         case CDT_STOPPING:
112                 return "stopping";
113         case CDT_STOPPED:
114                 return "stopped";
115         case CDT_DISABLE:
116                 return "disabled";
117         default:
118                 return "unknown";
119         }
120 }
121
122 /* when multiple lock are needed, the lock order is
123  * cdt_llog_lock
124  * cdt_agent_lock
125  * cdt_counter_lock
126  * cdt_request_lock
127  */
128 struct coordinator {
129         refcount_t               cdt_ref;            /**< cdt refcount */
130         wait_queue_head_t        cdt_waitq;          /**< cdt wait queue */
131         bool                     cdt_event;          /**< coordinator event */
132         struct task_struct      *cdt_task;           /**< cdt thread handle */
133         struct lu_env            cdt_env;            /**< coordinator lustre
134                                                       * env */
135         struct lu_context        cdt_session;        /** session for lu_ucred */
136         struct dentry           *cdt_debugfs_dir;       /**< cdt debugfs directory */
137         struct completion        cdt_kobj_unregister;
138         struct kobject           cdt_hsm_kobj;          /* hsm sysfs object */
139         __u64                    cdt_policy;         /**< policy flags */
140         enum cdt_states          cdt_state;           /**< state */
141         struct mutex             cdt_state_lock;      /**< cdt_state lock */
142         __u64                    cdt_last_cookie;     /**< last cookie
143                                                        * allocated */
144         struct rw_semaphore      cdt_llog_lock;       /**< protect llog
145                                                        * access */
146         struct rw_semaphore      cdt_agent_lock;      /**< protect agent list */
147         struct rw_semaphore      cdt_request_lock;    /**< protect request
148                                                        * list */
149         timeout_t                cdt_loop_period;     /**< llog scan period */
150         timeout_t                cdt_grace_delay;     /**< request grace
151                                                        * delay */
152         timeout_t                cdt_active_req_timeout; /**< request timeout */
153         __u32                    cdt_default_archive_id; /**< archive id used
154                                                        * when none are
155                                                        * specified */
156         u64                      cdt_max_requests;    /**< max count of started
157                                                        * requests */
158         /** Current count of active requests */
159         atomic_t                 cdt_request_count;   /** total */
160         atomic_t                 cdt_archive_count;
161         atomic_t                 cdt_restore_count;
162         atomic_t                 cdt_remove_count;
163
164         /* started requests (struct cdt_agent_req:car_cookie_hash)
165          * indexed by cookie */
166         struct cfs_hash         *cdt_request_cookie_hash;
167         /* started requests (struct cdt_agent_req:car_request_list) */
168         struct list_head         cdt_request_list;
169         struct list_head         cdt_agents;          /**< list of register
170                                                        * agents */
171         struct rhashtable        cdt_restore_hash;    /* rhashtable for
172                                                        * restore requests
173                                                        */
174
175         /* Hash of cookies to locations of record locations in agent
176          * request log. */
177         struct cfs_hash         *cdt_agent_record_hash;
178
179         /* Bitmasks indexed by the HSMA_XXX constants. */
180         __u64                    cdt_user_request_mask;
181         __u64                    cdt_group_request_mask;
182         __u64                    cdt_other_request_mask;
183
184         /* Remove archive on last unlink policy */
185         bool                     cdt_remove_archive_on_last_unlink;
186
187         bool                     cdt_wakeup_coordinator;
188 };
189
190 /* mdt state flag bits */
191 #define MDT_FL_CFGLOG 0
192 #define MDT_FL_SYNCED 1
193
194 /* possible values for mo_dom_lock */
195 enum {
196         NO_DOM_LOCK_ON_OPEN = 0,
197         TRYLOCK_DOM_ON_OPEN = 1,
198         /* not used anymore, left here for compatibility */
199         ALWAYS_DOM_LOCK_ON_OPEN = 2,
200         NUM_DOM_LOCK_ON_OPEN_MODES
201 };
202
203 struct mdt_statfs_cache {
204         struct obd_statfs msf_osfs;
205         __u64 msf_age;
206 };
207
208 enum mdt_rename_type {
209         RENAME_SAMEDIR_SIZE = 0,
210         RENAME_CROSSDIR_SRC_SIZE,
211         RENAME_CROSSDIR_TGT_SIZE,
212         RENAME_LAST
213 };
214
215 struct rename_stats {
216         ktime_t                 rs_init;
217         struct obd_histogram    rs_hist[RENAME_LAST];
218 };
219
220 /* split directory automatically when sub file count exceeds 50k */
221 #define DIR_SPLIT_COUNT_DEFAULT 50000
222
223 /* directory auto-split allocate delta new stripes each time */
224 #define DIR_SPLIT_DELTA_DEFAULT 4
225
226 struct mdt_dir_restriper {
227         struct lu_env           mdr_env;
228         struct lu_context       mdr_session;
229         struct task_struct     *mdr_task;
230         /* lock for below fields */
231         spinlock_t              mdr_lock;
232         /* auto split when plain dir/shard sub files exceed threshold */
233         u64                     mdr_dir_split_count;
234         /* auto split growth delta */
235         u32                     mdr_dir_split_delta;
236         /* directories to split */
237         struct list_head        mdr_auto_splitting;
238         /* directories under which sub files are migrating */
239         struct list_head        mdr_migrating;
240         /* directories waiting to update layout after migration */
241         struct list_head        mdr_updating;
242         /* time to update directory layout after migration */
243         time64_t                mdr_update_time;
244         /* lum used in split/migrate/layout_change */
245         union lmv_mds_md        mdr_lmv;
246         /* page used in readdir */
247         struct page            *mdr_page;
248 };
249
250 struct mdt_device {
251         /* super-class */
252         struct lu_device           mdt_lu_dev;
253         struct seq_server_site     mdt_seq_site;
254         /* DLM name-space for meta-data locks maintained by this server */
255         struct ldlm_namespace     *mdt_namespace;
256         /* ptlrpc handle for MDS->client connections (for lock ASTs). */
257         struct ptlrpc_client      *mdt_ldlm_client;
258         /* underlying device */
259         struct obd_export         *mdt_child_exp;
260         struct md_device          *mdt_child;
261         struct dt_device          *mdt_bottom;
262         struct obd_export         *mdt_bottom_exp;
263         struct local_oid_storage  *mdt_los;
264         /** target device */
265         struct lu_target           mdt_lut;
266         /*
267          * Options bit-fields.
268          */
269         struct {
270                 unsigned int       mo_user_xattr:1,
271                                    mo_acl:1,
272                                    mo_cos:1;
273                 unsigned int       mo_dom_lock;
274         } mdt_opts;
275         /* mdt state flags */
276         unsigned long              mdt_state;
277
278         /* transaction callbacks */
279         struct dt_txn_callback     mdt_txn_cb;
280
281         /* these values should be updated from lov if necessary.
282          * or should be placed somewhere else. */
283         int                        mdt_max_mdsize;
284
285         int                        mdt_max_ea_size;
286
287         /* Max modify RPCs, replaces max_mod_rpcs_per_client module param */
288         unsigned int                       mdt_max_mod_rpcs_in_flight;
289
290         /* preferred BRW size, decided by storage type and capability */
291         __u32                      mdt_brw_size;
292
293         struct upcall_cache       *mdt_identity_cache;
294
295         unsigned int               mdt_evict_tgt_nids:1,
296                                    mdt_dom_read_open:1,
297                                    mdt_migrate_hsm_allowed:1,
298                                    mdt_enable_strict_som:1,
299                                    /* Enable remote dir on non-MDT0 */
300                                    mdt_enable_dir_migration:1,
301                                    mdt_enable_dir_restripe:1,
302                                    mdt_enable_dir_auto_split:1,
303                                    mdt_enable_parallel_rename_dir:1,
304                                    mdt_enable_parallel_rename_file:1,
305                                    mdt_enable_parallel_rename_crossdir:1,
306                                    mdt_enable_remote_dir:1,
307                                    mdt_enable_remote_rename:1,
308                                    mdt_enable_striped_dir:1,
309                                    mdt_readonly:1,
310                                    mdt_skip_lfsck:1,
311                                    /* dir restripe migrate dirent only */
312                                    mdt_dir_restripe_nsonly:1,
313                                    /* this is enabled by default, but once an
314                                     * old client joins, disable this to handle
315                                     * inherited default LMV on server.
316                                     */
317                                    mdt_enable_dmv_implicit_inherit:1,
318                                    /* set dmv by setxattr, off by default */
319                                    mdt_enable_dmv_xattr:1;
320
321                                    /* user with gid can create remote/striped
322                                     * dir, and set default dir stripe */
323         gid_t                      mdt_enable_remote_dir_gid;
324                                    /* user with this gid can change projid */
325         gid_t                      mdt_enable_chprojid_gid;
326         kernel_cap_t               mdt_enable_cap_mask;
327
328         /* lock for osfs and md_root */
329         spinlock_t                 mdt_lock;
330
331         /* statfs optimization: we cache a bit  */
332         struct mdt_statfs_cache    mdt_sum_osfs;
333         struct mdt_statfs_cache    mdt_osfs;
334
335         /* root squash */
336         struct root_squash_info    mdt_squash;
337
338         struct rename_stats        mdt_rename_stats;
339         struct lu_fid              mdt_md_root_fid;
340
341         /* connection to quota master */
342         struct obd_export         *mdt_qmt_exp;
343         /* quota master device associated with this MDT */
344         struct lu_device          *mdt_qmt_dev;
345
346         struct coordinator         mdt_coordinator;
347
348         /* inter-MDT connection count */
349         atomic_t                   mdt_mds_mds_conns;
350
351         /* MDT device async commit count, used for debug and sanity test */
352         atomic_t                   mdt_async_commit_count;
353
354         struct mdt_object         *mdt_md_root;
355
356         struct mdt_dir_restriper   mdt_restriper;
357
358         /* count of old clients that doesn't support DMV implicite inherit */
359         atomic_t                   mdt_dmv_old_client_count;
360
361         /* name of xattr used to store jobid in mdt inode */
362         char                       mdt_job_xattr[XATTR_JOB_MAX_LEN];
363 };
364
365 #define MDT_SERVICE_WATCHDOG_FACTOR     (2)
366 #define MDT_COS_DEFAULT         (0)
367
368 #define ENOENT_VERSION 1        /** 'virtual' version of non-existent object */
369
370 struct mdt_object {
371         struct lu_object_header mot_header;
372         struct lu_object        mot_obj;
373         unsigned int            mot_lov_created:1,  /* lov object created */
374                                 mot_cache_attr:1,   /* enable remote object
375                                                      * attribute cache */
376                                 mot_restriping:1,   /* dir restriping */
377                                 /* dir auto-split disabled */
378                                 mot_auto_split_disabled:1,
379                                 mot_lsom_inited:1; /* lsom was inited */
380         int                     mot_write_count;
381         spinlock_t              mot_write_lock;
382         /* Lock to protect create_data */
383         struct mutex            mot_lov_mutex;
384         /* Lock to protect object's SOM update. */
385         struct mutex            mot_som_mutex;
386         __u64                   mot_lsom_size;
387         __u64                   mot_lsom_blocks;
388         /* lock to protect read/write stages for Data-on-MDT files */
389         struct rw_semaphore     mot_dom_sem;
390         /* Lock to protect lease open.
391          * Lease open acquires write lock; normal open acquires read lock */
392         struct rw_semaphore     mot_open_sem;
393         atomic_t                mot_lease_count;
394         atomic_t                mot_open_count;
395         /* directory offset, used in sub file migration in dir restripe */
396         loff_t                  mot_restripe_offset;
397         /* link to mdt_restriper auto_splitting/migrating/updating */
398         struct list_head        mot_restripe_linkage;
399 };
400
401 struct mdt_lock_handle {
402         /* Lock type, reg for cross-ref use or pdo lock. */
403         mdl_type_t              mlh_type;
404
405         /* Regular lock */
406         struct lustre_handle    mlh_reg_lh;
407         enum ldlm_mode          mlh_reg_mode;
408         __u64                   mlh_gid;
409
410         /* Pdirops lock */
411         struct lustre_handle    mlh_pdo_lh;
412         enum ldlm_mode          mlh_pdo_mode;
413         unsigned int            mlh_pdo_hash;
414         unsigned int            mlh_pdo_remote:1;
415
416         /* Remote regular lock */
417         struct lustre_handle    mlh_rreg_lh;
418         enum ldlm_mode          mlh_rreg_mode;
419 };
420
421 enum {
422         MDT_LH_PARENT,  /* parent lockh */
423         MDT_LH_CHILD,   /* child lockh */
424         MDT_LH_OLD,     /* old lockh for rename */
425         MDT_LH_LAYOUT = MDT_LH_OLD, /* layout lock */
426         MDT_LH_NEW,     /* new lockh for rename */
427         MDT_LH_RMT,     /* used for return lh to caller */
428         MDT_LH_LOCAL,   /* local lock never return to client */
429         MDT_LH_LOOKUP,  /* lookup lock for source object in rename/migrate if
430                          * it's remote object */
431         MDT_LH_NR
432 };
433
434 enum {
435         MDT_LOCAL_LOCK,
436         MDT_CROSS_LOCK
437 };
438
439 /* Special magical errno for communicaiton between mdt_reint_open()
440  * and mdt_intent_reint() which means return the lock to the client
441  * for subsequent cross ref open. Previously we used plain -EREMOTE
442  * but other functions called in that path might return it too and
443  * confuse us. This is not returned to the client. See LU-5370. */
444 #define MDT_EREMOTE_OPEN (EREMOTE + 1024)
445
446 struct mdt_reint_record {
447         enum mds_reint_op                rr_opcode;
448         const struct lustre_handle      *rr_open_handle;
449         const struct lustre_handle      *rr_lease_handle;
450         const struct lu_fid             *rr_fid1;
451         const struct lu_fid             *rr_fid2;
452         struct lu_name                   rr_name;
453         struct lu_name                   rr_tgt_name;
454         void                            *rr_eadata;
455         int                              rr_eadatalen;
456         __u32                            rr_flags;
457         __u16                            rr_mirror_id;
458 };
459
460 enum mdt_reint_flag {
461         MRF_OPEN_TRUNC = BIT(0),
462         MRF_OPEN_RESEND = BIT(1),
463 };
464
465 /*
466  * Common data shared by mdt-level handlers. This is allocated per-thread to
467  * reduce stack consumption.
468  */
469 struct mdt_thread_info {
470         /*
471          * XXX: Part One:
472          * The following members will be filled explicitly
473          * with specific data in mdt_thread_info_init().
474          */
475         /* TODO: move this into mdt_session_key(with LCT_SESSION), because
476          * request handling may migrate from one server thread to another.
477          */
478         struct req_capsule        *mti_pill;
479
480         /*
481          * SUB request pill in a batch request.
482          */
483         struct req_capsule          mti_sub_pill;
484
485         /* although we have export in req, there are cases when it is not
486          * available, e.g. closing files upon export destroy */
487         struct obd_export          *mti_exp;
488         /*
489          * A couple of lock handles.
490          */
491         struct mdt_lock_handle     mti_lh[MDT_LH_NR];
492
493         struct mdt_device         *mti_mdt;
494         const struct lu_env       *mti_env;
495
496         /* transaction number of current request */
497         __u64                      mti_transno;
498
499         /*
500          * XXX: Part Two:
501          * The following members will be filled expilictly
502          * with zero in mdt_thread_info_init(). These members may be used
503          * by all requests.
504          */
505
506         /*
507          * Object attributes.
508          */
509         struct md_attr             mti_attr;
510         struct md_attr             mti_attr2; /* mdt_lvb.c */
511         /*
512          * Body for "habeo corpus" operations.
513          */
514         const struct mdt_body     *mti_body;
515         /*
516          * Host object. This is released at the end of mdt_handler().
517          */
518         struct mdt_object         *mti_object;
519         /*
520          * Lock request for "habeo clavis" operations.
521          */
522         const struct ldlm_request *mti_dlm_req;
523
524         __u32                      mti_cross_ref:1,
525         /* big_lmm buffer was used and must be used in reply */
526                                    mti_big_lmm_used:1,
527                                    mti_big_acl_used:1,
528                                    mti_som_strict:1,
529         /* Batch processing environment */
530                                    mti_batch_env:1,
531                                    mti_intent_lock:1;
532
533         /* opdata for mdt_reint_open(), has the same as
534          * ldlm_reply:lock_policy_res1.  mdt_update_last_rcvd() stores this
535          * value onto disk for recovery when mdt_trans_stop_cb() is called.
536          */
537         __u64                      mti_opdata;
538
539         /*
540          * XXX: Part Three:
541          * The following members will be filled explicitly
542          * with zero in mdt_reint_unpack(), because they are only used
543          * by reint requests (including mdt_reint_open()).
544          */
545
546         /*
547          * reint record. contains information for reint operations.
548          */
549         struct mdt_reint_record    mti_rr;
550
551         __u64                      mti_ver[PTLRPC_NUM_VERSIONS];
552         /*
553          * Operation specification (currently create and lookup)
554          */
555         struct md_op_spec          mti_spec;
556
557         /*
558          * XXX: Part Four:
559          * The following members will _NOT_ be initialized at all.
560          * DO NOT expect them to contain any valid value.
561          * They should be initialized explicitly by the user themselves.
562          */
563
564         /* XXX: If something is in a union, make sure they do not conflict */
565         struct lu_fid                   mti_tmp_fid1;
566         struct lu_fid                   mti_tmp_fid2;
567         union ldlm_policy_data          mti_policy; /* for mdt_object_lock() */
568         struct ldlm_res_id              mti_res_id; /* and mdt_rename_lock() */
569         union {
570                 struct obd_uuid         uuid[2];    /* for mdt_seq_init_cli() */
571                 char                    ns_name[48];/* for mdt_init0()        */
572                 struct lustre_cfg_bufs  bufs;       /* for mdt_stack_fini()   */
573                 struct obd_statfs       osfs;       /* for mdt_statfs()       */
574                 struct {
575                         /* for mdt_readpage()      */
576                         struct lu_rdpg     mti_rdpg;
577                 } rdpg;
578                 struct {
579                         struct md_attr attr;
580                 } hsm;
581                 struct {
582                         struct md_attr attr;
583                 } som;
584         } mti_u;
585
586         struct lustre_handle       mti_open_handle;
587         loff_t                     mti_off;
588         struct lu_buf              mti_buf;
589         struct lu_buf              mti_big_buf;
590
591         /* Ops object filename */
592         struct lu_name             mti_name;
593         char                       mti_filename[NAME_MAX + 1];
594         /* per-thread values, can be re-used, may be vmalloc'd */
595         void                      *mti_big_lmm;
596         void                      *mti_big_acl;
597         int                        mti_big_lmmsize;
598         int                        mti_big_aclsize;
599         /* should be enough to fit lustre_mdt_attrs */
600         char                       mti_xattr_buf[128];
601         struct ldlm_enqueue_info   mti_einfo;
602         /* einfo used by mdt_remote_object_lock_try() */
603         struct ldlm_enqueue_info   mti_remote_einfo;
604         struct tg_reply_data      *mti_reply_data;
605
606         /* FLR: layout change API */
607         struct md_layout_change    mti_mlc;
608
609         struct lu_seq_range        mti_range;
610 };
611
612 extern struct lu_context_key mdt_thread_key;
613
614 static inline struct mdt_thread_info *mdt_th_info(const struct lu_env *env)
615 {
616         return lu_env_info(env, &mdt_thread_key);
617 }
618
619 struct cdt_req_progress {
620         spinlock_t               crp_lock;      /**< protect tree */
621         struct interval_tree_root crp_root;     /**< tree to track extent
622                                                  *   moved */
623         __u64                    crp_total;
624 };
625
626 struct cdt_agent_req {
627         struct hlist_node        car_cookie_hash;  /**< find req by cookie */
628         struct list_head         car_request_list; /**< to chain all the req. */
629         struct kref              car_refcount;     /**< reference counter */
630         __u64                    car_flags;        /**< request original flags */
631         struct obd_uuid          car_uuid;         /**< agent doing the req. */
632         __u32                    car_archive_id;   /**< archive id */
633         int                      car_canceled;     /**< request was canceled */
634         time64_t                 car_req_start;    /**< start time */
635         time64_t                 car_req_update;   /**< last update time */
636         struct hsm_action_item  *car_hai;          /**< req. to the agent */
637         struct cdt_req_progress  car_progress;     /**< track data mvt
638                                                     *   progress */
639 };
640 extern struct kmem_cache *mdt_hsm_car_kmem;
641
642 struct hsm_agent {
643         struct list_head ha_list;               /**< to chain the agents */
644         struct obd_uuid  ha_uuid;               /**< agent uuid */
645         __u32           *ha_archive_id;         /**< archive id */
646         int              ha_archive_cnt;        /**< number of archive entries
647                                                  *   0 means any archive */
648         atomic_t         ha_requests;           /**< current request count */
649         atomic_t         ha_success;            /**< number of successful
650                                                  * actions */
651         atomic_t         ha_failure;            /**< number of failed actions */
652 };
653
654 struct cdt_restore_handle {
655         struct rhash_head       crh_hash;       /**< link to cdt_restore_hash */
656         struct lu_fid           crh_fid;        /**< fid of the object */
657         struct ldlm_extent      crh_extent;     /**< extent of the restore */
658         struct mdt_lock_handle  crh_lh;         /**< lock handle */
659         atomic_t                crh_refc;       /**< crh ref counter */
660         struct rcu_head         crh_rcu;        /**< crh rcu head */
661 };
662 extern struct kmem_cache *mdt_hsm_cdt_kmem;     /** restore handle slab cache */
663
664 struct hsm_record_update {
665         __u64 cookie;
666         enum agent_req_status status;
667 };
668
669 static inline const struct md_device_operations *
670 mdt_child_ops(struct mdt_device * m)
671 {
672         LASSERT(m->mdt_child);
673         return m->mdt_child->md_ops;
674 }
675
676 static inline struct md_object *mdt_object_child(struct mdt_object *o)
677 {
678         LASSERT(o);
679         return lu2md(lu_object_next(&o->mot_obj));
680 }
681
682 static inline struct ptlrpc_request *mdt_info_req(struct mdt_thread_info *info)
683 {
684          return info->mti_pill ? info->mti_pill->rc_req : NULL;
685 }
686
687 static inline __u64 mdt_conn_flags(struct mdt_thread_info *info)
688 {
689         LASSERT(info->mti_exp);
690         return exp_connect_flags(info->mti_exp);
691 }
692
693 static inline void mdt_object_get(const struct lu_env *env,
694                                   struct mdt_object *o)
695 {
696         ENTRY;
697         lu_object_get(&o->mot_obj);
698         EXIT;
699 }
700
701 static inline void mdt_object_put(const struct lu_env *env,
702                                   struct mdt_object *o)
703 {
704         ENTRY;
705         lu_object_put(env, &o->mot_obj);
706         EXIT;
707 }
708
709 static inline int mdt_object_exists(const struct mdt_object *o)
710 {
711         return lu_object_exists(&o->mot_obj);
712 }
713
714 static inline int mdt_object_remote(const struct mdt_object *o)
715 {
716         return lu_object_remote(&o->mot_obj);
717 }
718
719 static inline const struct lu_fid *mdt_object_fid(const struct mdt_object *o)
720 {
721         return lu_object_fid(&o->mot_obj);
722 }
723
724 static inline struct lu_site *mdt_lu_site(const struct mdt_device *mdt)
725 {
726         return mdt->mdt_lu_dev.ld_site;
727 }
728
729 static inline struct seq_server_site *mdt_seq_site(struct mdt_device *mdt)
730 {
731         return &mdt->mdt_seq_site;
732 }
733
734 static inline u32 mdt_node_id(const struct mdt_device *mdt)
735 {
736         return mdt->mdt_seq_site.ss_node_id;
737 }
738
739 static inline void mdt_export_evict(struct obd_export *exp)
740 {
741         class_fail_export(exp);
742 }
743
744 /* Here we use LVB_TYPE to check dne client, because it is
745  * also landed on 2.4. */
746 static inline bool mdt_is_dne_client(struct obd_export *exp)
747 {
748         return !!(exp_connect_flags(exp) & OBD_CONNECT_LVB_TYPE);
749 }
750
751 static inline bool mdt_is_striped_client(struct obd_export *exp)
752 {
753         return exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE;
754 }
755
756 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *dom_only);
757
758 static inline bool mdt_lmm_dom_only(struct lov_mds_md *lmm)
759 {
760         int dom_only = 0;
761
762         mdt_lmm_dom_entry_check(lmm, &dom_only);
763         return dom_only;
764 }
765
766 static inline __u32 mdt_lmm_dom_stripesize(struct lov_mds_md *lmm)
767 {
768         return mdt_lmm_dom_entry_check(lmm, NULL);
769 }
770
771 static inline bool mdt_lmm_is_flr(struct lov_mds_md *lmm)
772 {
773         struct lov_comp_md_v1 *lcm = (typeof(lcm))lmm;
774
775         return le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1 &&
776                le16_to_cpu(lcm->lcm_mirror_count) > 0;
777 }
778
779 static inline bool lmm_is_overstriping(struct lov_mds_md *lmm)
780 {
781         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
782             le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3)
783                 return le16_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_OVERSTRIPING;
784
785         return false;
786 }
787
788 static inline bool mdt_lmm_comp_overstriping(struct lov_mds_md *lmm)
789 {
790         struct lov_comp_md_v1 *comp_v1;
791         struct lov_mds_md *v1;
792         __u32 off;
793         int i;
794
795         comp_v1 = (struct lov_comp_md_v1 *)lmm;
796
797         for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
798                 off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
799                 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
800
801                 if (lmm_is_overstriping(v1))
802                         return true;
803         }
804
805         return false;
806 }
807
808 static inline bool mdt_lmm_is_overstriping(struct lov_mds_md *lmm)
809 {
810         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1)
811                 return mdt_lmm_comp_overstriping(lmm);
812
813         return lmm_is_overstriping(lmm);
814 }
815
816 static inline bool mdt_is_sum_statfs_client(struct obd_export *exp)
817 {
818         return exp_connect_flags(exp) & OBD_CONNECT_FLAGS2 &&
819                exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS;
820 }
821
822 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag);
823 void mdt_set_disposition(struct mdt_thread_info *info,
824                          struct ldlm_reply *rep, __u64 op_flag);
825 void mdt_clear_disposition(struct mdt_thread_info *info,
826                            struct ldlm_reply *rep, __u64 op_flag);
827
828 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
829                        const struct lu_name *lname);
830
831 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm);
832 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock);
833
834 int mdt_lock_setup(struct mdt_thread_info *info, struct mdt_object *mo,
835                    struct mdt_lock_handle *lh);
836
837 int mdt_check_resent_lock(struct mdt_thread_info *info, struct mdt_object *mo,
838                           struct mdt_lock_handle *lhc);
839
840 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *obj,
841                     struct mdt_lock_handle *lh, __u64 ibits,
842                     enum ldlm_mode mode);
843 int mdt_parent_lock(struct mdt_thread_info *info, struct mdt_object *o,
844                     struct mdt_lock_handle *lh, const struct lu_name *lname,
845                     enum ldlm_mode mode);
846 int mdt_object_stripes_lock(struct mdt_thread_info *info,
847                             struct mdt_object *pobj, struct mdt_object *o,
848                             struct mdt_lock_handle *lh,
849                             struct ldlm_enqueue_info *einfo, __u64 ibits,
850                             enum ldlm_mode mode);
851 int mdt_object_check_lock(struct mdt_thread_info *info,
852                           struct mdt_object *parent, struct mdt_object *child,
853                           struct mdt_lock_handle *lh, __u64 ibits,
854                           enum ldlm_mode mode);
855 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *mo,
856                         struct mdt_lock_handle *lh, __u64 *ibits,
857                         __u64 trybits, enum ldlm_mode mode);
858
859 /* below three lock functions are used internally */
860 int mdt_object_lock_internal(struct mdt_thread_info *info,
861                              struct mdt_object *obj, const struct lu_fid *fid,
862                              struct mdt_lock_handle *lh, __u64 *ibits,
863                              __u64 trybits, bool cache);
864 int mdt_object_pdo_lock(struct mdt_thread_info *info, struct mdt_object *obj,
865                         struct mdt_lock_handle *lh, const struct lu_name *name,
866                         enum ldlm_mode mode, bool pdo_lock);
867 int mdt_object_lookup_lock(struct mdt_thread_info *info,
868                            struct mdt_object *pobj, struct mdt_object *obj,
869                            struct mdt_lock_handle *lh, enum ldlm_mode mode);
870
871 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *mo,
872                        struct mdt_lock_handle *lh, int decref);
873 void mdt_object_stripes_unlock(struct mdt_thread_info *info,
874                                struct mdt_object *o, struct mdt_lock_handle *lh,
875                                struct ldlm_enqueue_info *einfo, int decref);
876
877 struct mdt_object *mdt_object_new(const struct lu_env *env,
878                                   struct mdt_device *,
879                                   const struct lu_fid *);
880 struct mdt_object *mdt_object_find(const struct lu_env *,
881                                    struct mdt_device *,
882                                    const struct lu_fid *);
883 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
884                                         const struct lu_fid *f,
885                                         struct mdt_lock_handle *lh,
886                                         __u64 ibits, enum ldlm_mode mode);
887 void mdt_object_unlock_put(struct mdt_thread_info *info,
888                            struct mdt_object *o,
889                            struct mdt_lock_handle *lh,
890                            int decref);
891
892 void mdt_client_compatibility(struct mdt_thread_info *info);
893
894 enum mdt_name_flags {
895         MNF_FIX_ANON = 1,
896 };
897
898 int mdt_name_unpack(struct req_capsule *pill,
899                     const struct req_msg_field *field,
900                     struct lu_name *ln,
901                     enum mdt_name_flags flags);
902 int mdt_close_unpack(struct mdt_thread_info *info);
903 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op);
904 void mdt_fix_lov_magic(struct mdt_thread_info *info, void *eadata);
905 int mdt_reint_rec(struct mdt_thread_info *, struct mdt_lock_handle *);
906 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
907 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
908                       struct mdt_object *o, struct lu_nodemap *nodemap);
909 #endif
910 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
911                         const struct lu_attr *attr, const struct lu_fid *fid);
912 int mdt_pack_size2body(struct mdt_thread_info *info,
913                         const struct lu_fid *fid,  struct lustre_handle *lh);
914 int mdt_getxattr(struct mdt_thread_info *info);
915 int mdt_reint_setxattr(struct mdt_thread_info *info,
916                        struct mdt_lock_handle *lh);
917
918 void mdt_reconstruct(struct mdt_thread_info *, struct mdt_lock_handle *);
919 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
920                              struct mdt_lock_handle *lhc);
921
922 int mdt_export_stats_init(struct obd_device *obd,
923                           struct obd_export *exp,
924                           void *client_nid);
925
926 int mdt_lock_new_child(struct mdt_thread_info *info,
927                        struct mdt_object *o,
928                        struct mdt_lock_handle *child_lockh);
929 void mdt_mfd_set_mode(struct mdt_file_data *mfd, u64 open_flags);
930 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc);
931 struct mdt_file_data *mdt_open_handle2mfd(struct mdt_export_data *med,
932                                         const struct lustre_handle *open_handle,
933                                         bool is_replay);
934 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
935                                   struct mdt_object *pobj,
936                                   struct mdt_object *obj);
937
938 int mdt_get_info(struct tgt_session_info *tsi);
939 int mdt_attr_get_complex(struct mdt_thread_info *info,
940                          struct mdt_object *o, struct md_attr *ma);
941 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
942                       const char *name);
943 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
944                      struct md_attr *ma, const char *name);
945 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
946                    struct md_attr *ma, const char *name);
947 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
948                       struct lu_fid *pfid);
949 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
950                            struct lu_fid *pfid, struct lu_name *lname);
951 int mdt_write_get(struct mdt_object *o);
952 void mdt_write_put(struct mdt_object *o);
953 int mdt_write_read(struct mdt_object *o);
954 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med);
955 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd);
956 void mdt_mfd_free(struct mdt_file_data *mfd);
957 int mdt_close(struct tgt_session_info *tsi);
958 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
959                         struct md_attr *ma);
960 int mdt_fix_reply(struct mdt_thread_info *info);
961 int mdt_handle_last_unlink(struct mdt_thread_info *, struct mdt_object *,
962                            struct md_attr *);
963 void mdt_reconstruct_open(struct mdt_thread_info *, struct mdt_lock_handle *);
964 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
965                       struct mdt_lock_handle *lhc,
966                       struct md_layout_change *spec);
967 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
968                            struct mdt_object *parent, struct mdt_object *child);
969 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt);
970
971 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len);
972 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
973                                    const void *area, ssize_t len);
974
975 void mdt_dump_lmm(int level, const struct lov_mds_md *lmm, __u64 valid);
976 void mdt_dump_lmv(unsigned int level, const union lmv_mds_md *lmv);
977
978 bool allow_client_chgrp(struct mdt_thread_info *info, struct lu_ucred *uc);
979 int mdt_check_ucred(struct mdt_thread_info *);
980 int mdt_init_ucred(struct mdt_thread_info *, struct mdt_body *);
981 int mdt_init_ucred_reint(struct mdt_thread_info *);
982 void mdt_exit_ucred(struct mdt_thread_info *);
983 int mdt_version_get_check(struct mdt_thread_info *, struct mdt_object *, int);
984 void mdt_version_get_save(struct mdt_thread_info *, struct mdt_object *, int);
985 int mdt_version_get_check_save(struct mdt_thread_info *, struct mdt_object *,
986                                int);
987 int mdt_lookup_version_check(struct mdt_thread_info *info,
988                              struct mdt_object *p,
989                              const struct lu_name *lname,
990                              struct lu_fid *fid, int idx);
991 void mdt_thread_info_reset(struct mdt_thread_info *info);
992 void mdt_thread_info_init(struct ptlrpc_request *req,
993                           struct mdt_thread_info *mti);
994 void mdt_thread_info_fini(struct mdt_thread_info *mti);
995 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi);
996 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
997                              struct ldlm_lock *new_lock,
998                              struct mdt_lock_handle *lh, __u64 flags);
999 int mdt_intent_lock_replace(struct mdt_thread_info *info,
1000                             struct ldlm_lock **lockp,
1001                             struct mdt_lock_handle *lh,
1002                             __u64 flags, int result);
1003
1004 int hsm_init_ucred(struct lu_ucred *uc);
1005 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
1006                      const struct md_hsm *mh);
1007
1008 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1009                             void *data, int flag);
1010 int mdt_links_read(struct mdt_thread_info *info,
1011                    struct mdt_object *mdt_obj,
1012                    struct linkea_data *ldata);
1013 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
1014                        struct mdt_body *repbody);
1015 int mdt_pack_secctx_in_reply(struct mdt_thread_info *info,
1016                              struct mdt_object *child);
1017 int mdt_pack_encctx_in_reply(struct mdt_thread_info *info,
1018                              struct mdt_object *child);
1019 void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
1020                               struct mdt_object *obj, struct md_attr *ma,
1021                               __u64 open_flags);
1022
1023 static inline struct mdt_device *mdt_dev(struct lu_device *d)
1024 {
1025         return container_of_safe(d, struct mdt_device, mdt_lu_dev);
1026 }
1027
1028 static inline struct mdt_object *mdt_obj(struct lu_object *o)
1029 {
1030         return container_of_safe(o, struct mdt_object, mot_obj);
1031 }
1032
1033 static inline struct dt_object *mdt_obj2dt(struct mdt_object *mo)
1034 {
1035         struct lu_object        *lo;
1036         struct mdt_device       *mdt = mdt_dev(mo->mot_obj.lo_dev);
1037
1038         lo = lu_object_locate(mo->mot_obj.lo_header,
1039                               mdt->mdt_bottom->dd_lu_dev.ld_type);
1040
1041         return lu2dt(lo);
1042 }
1043
1044 static inline bool agent_req_in_final_state(enum agent_req_status ars)
1045 {
1046         return ((ars == ARS_SUCCEED) || (ars == ARS_FAILED) ||
1047                 (ars == ARS_CANCELED));
1048 }
1049
1050 /* mdt/mdt_identity.c */
1051 #define MDT_IDENTITY_UPCALL_PATH        "/usr/sbin/l_getidentity"
1052 #define UC_IDCACHE_HASH_SIZE 128
1053 extern struct upcall_cache_ops mdt_identity_upcall_cache_ops;
1054
1055 struct md_identity *mdt_identity_get(struct upcall_cache *, __u32);
1056
1057 void mdt_identity_put(struct upcall_cache *, struct md_identity *);
1058
1059 void mdt_flush_identity(struct upcall_cache *, int);
1060
1061 __u32 mdt_identity_get_perm(struct md_identity *identity, struct lnet_nid *nid);
1062
1063 /* mdt/mdt_recovery.c */
1064 __u64 mdt_req_from_lrd(struct ptlrpc_request *req, struct tg_reply_data *trd);
1065
1066 /* mdt/mdt_batch.c */
1067 int mdt_batch(struct tgt_session_info *tsi);
1068
1069 /* mdt/mdt_hsm.c */
1070 int mdt_hsm_state_get(struct tgt_session_info *tsi);
1071 int mdt_hsm_state_set(struct tgt_session_info *tsi);
1072 int mdt_hsm_action(struct tgt_session_info *tsi);
1073 int mdt_hsm_progress(struct tgt_session_info *tsi);
1074 int mdt_hsm_ct_register(struct tgt_session_info *tsi);
1075 int mdt_hsm_ct_unregister(struct tgt_session_info *tsi);
1076 int mdt_hsm_request(struct tgt_session_info *tsi);
1077
1078 /* mdt/mdt_hsm_cdt_actions.c */
1079 extern const struct file_operations mdt_hsm_actions_fops;
1080 void dump_llog_agent_req_rec(const char *prefix,
1081                              const struct llog_agent_req_rec *larr);
1082 int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
1083                      llog_cb_t cb, void *data, u32 start_cat_idx,
1084                      u32 start_rec_idx, int rw);
1085 int mdt_agent_record_add(const struct lu_env *env, struct mdt_device *mdt,
1086                          __u32 archive_id, __u64 flags,
1087                          struct hsm_action_item *hai);
1088 int mdt_agent_record_update(struct mdt_thread_info *mti,
1089                             struct hsm_record_update *updates,
1090                             unsigned int updates_count);
1091 void cdt_agent_record_hash_add(struct coordinator *cdt, u64 cookie, u32 cat_idt,
1092                                u32 rec_idx);
1093 void cdt_agent_record_hash_lookup(struct coordinator *cdt, u64 cookie,
1094                                   u32 *cat_idt, u32 *rec_idx);
1095 void cdt_agent_record_hash_del(struct coordinator *cdt, u64 cookie);
1096
1097 /* mdt/mdt_hsm_cdt_agent.c */
1098 extern const struct file_operations mdt_hsm_agent_fops;
1099 int mdt_hsm_agent_register(struct mdt_thread_info *info,
1100                            const struct obd_uuid *uuid,
1101                            int nr_archives, __u32 *archive_num);
1102 int mdt_hsm_agent_register_mask(struct mdt_thread_info *info,
1103                                 const struct obd_uuid *uuid,
1104                                 __u32 archive_mask);
1105 int mdt_hsm_agent_unregister(struct mdt_thread_info *info,
1106                              const struct obd_uuid *uuid);
1107 int mdt_hsm_agent_update_statistics(struct coordinator *cdt,
1108                                     int succ_rq, int fail_rq, int new_rq,
1109                                     const struct obd_uuid *uuid);
1110 int mdt_hsm_find_best_agent(struct coordinator *cdt, __u32 archive,
1111                             struct obd_uuid *uuid);
1112 int mdt_hsm_agent_send(struct mdt_thread_info *mti, struct hsm_action_list *hal,
1113                        bool purge);
1114 /* mdt/mdt_hsm_cdt_client.c */
1115 int mdt_hsm_add_actions(struct mdt_thread_info *info,
1116                         struct hsm_action_list *hal);
1117 int mdt_hsm_get_action(struct mdt_thread_info *mti,
1118                        const struct lu_fid *fid,
1119                        enum hsm_copytool_action *action,
1120                        enum agent_req_status *status,
1121                        struct hsm_extent *extent);
1122 bool mdt_hsm_restore_is_running(struct mdt_thread_info *mti,
1123                                 const struct lu_fid *fid);
1124 /* mdt/mdt_hsm_cdt_requests.c */
1125 extern struct cfs_hash_ops cdt_request_cookie_hash_ops;
1126 extern struct cfs_hash_ops cdt_agent_record_hash_ops;
1127 extern const struct file_operations mdt_hsm_active_requests_fops;
1128 void dump_requests(char *prefix, struct coordinator *cdt);
1129 struct cdt_agent_req *mdt_cdt_alloc_request(__u32 archive_id, __u64 flags,
1130                                             struct obd_uuid *uuid,
1131                                             struct hsm_action_item *hai);
1132 void mdt_cdt_free_request(struct cdt_agent_req *car);
1133 int mdt_cdt_add_request(struct coordinator *cdt, struct cdt_agent_req *new_car);
1134 struct cdt_agent_req *mdt_cdt_find_request(struct coordinator *cdt, u64 cookie);
1135 void mdt_cdt_get_request(struct cdt_agent_req *car);
1136 void mdt_cdt_put_request(struct cdt_agent_req *car);
1137 struct cdt_agent_req *mdt_cdt_update_request(struct coordinator *cdt,
1138                                          const struct hsm_progress_kernel *pgs);
1139 int mdt_cdt_remove_request(struct coordinator *cdt, __u64 cookie);
1140 /* mdt/mdt_coordinator.c */
1141 void mdt_hsm_dump_hal(int level, const char *prefix,
1142                       struct hsm_action_list *hal);
1143 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
1144                            const struct lu_fid *fid,
1145                            const struct hsm_extent *he);
1146 bool cdt_restore_handle_exists(struct coordinator *cdt,
1147                                                    const struct lu_fid *fid);
1148 void cdt_restore_handle_del(struct mdt_thread_info *mti,
1149                             struct coordinator *cdt, const struct lu_fid *fid);
1150 int cdt_getref_try(struct coordinator *cdt);
1151 void cdt_putref(struct coordinator *cdt);
1152 /* coordinator management */
1153 int mdt_hsm_cdt_init(struct mdt_device *mdt);
1154 int mdt_hsm_cdt_stop(struct mdt_device *mdt);
1155 int mdt_hsm_cdt_fini(struct mdt_device *mdt);
1156
1157 /*
1158  * Signal the coordinator has work to do
1159  * \param cdt [IN] coordinator
1160  */
1161 static inline void mdt_hsm_cdt_event(struct coordinator *cdt)
1162 {
1163         cdt->cdt_event = true;
1164 }
1165
1166 /* coordinator control sysfs interface */
1167 ssize_t hsm_control_show(struct kobject *kobj, struct attribute *attr,
1168                          char *buf);
1169 ssize_t hsm_control_store(struct kobject *kobj, struct attribute *attr,
1170                           const char *buffer, size_t count);
1171 int hsm_cdt_tunables_init(struct mdt_device *mdt);
1172 void hsm_cdt_tunables_fini(struct mdt_device *mdt);
1173 /* md_hsm helpers */
1174 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
1175                                       const struct lu_fid *fid,
1176                                       struct md_hsm *hsm);
1177 /* actions/request helpers */
1178 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1179                     struct hsm_action_list *hal, struct obd_uuid *uuid);
1180 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1181                               u32 archive_id, u64 rq_flags,
1182                               const struct md_hsm *hsm);
1183 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1184                                  struct hsm_progress_kernel *pgs);
1185
1186 extern struct lu_context_key       mdt_thread_key;
1187
1188 /* debug issues helper starts here*/
1189 static inline int mdt_fail_write(const struct lu_env *env,
1190                                  struct dt_device *dd, int id)
1191 {
1192         if (CFS_FAIL_CHECK_ORSET(id, CFS_FAIL_ONCE)) {
1193                 CERROR(LUSTRE_MDT_NAME": cfs_fail_loc=%x, fail write ops\n",
1194                        id);
1195                 return dt_ro(env, dd);
1196                 /* We set FAIL_ONCE because we never "un-fail" a device */
1197         }
1198
1199         return 0;
1200 }
1201
1202 static inline struct mdt_export_data *mdt_req2med(struct ptlrpc_request *req)
1203 {
1204         return &req->rq_export->exp_mdt_data;
1205 }
1206
1207 static inline struct mdt_device *mdt_exp2dev(struct obd_export *exp)
1208 {
1209         return mdt_dev(exp->exp_obd->obd_lu_dev);
1210 }
1211
1212 static inline bool mdt_rdonly(struct obd_export *exp)
1213 {
1214         return (exp_connect_flags(exp) & OBD_CONNECT_RDONLY ||
1215                 mdt_exp2dev(exp)->mdt_bottom->dd_rdonly ||
1216                 mdt_exp2dev(exp)->mdt_readonly);
1217 }
1218
1219 typedef void (*mdt_reconstruct_t)(struct mdt_thread_info *mti,
1220                                   struct mdt_lock_handle *lhc);
1221 static inline int mdt_check_resent(struct mdt_thread_info *info,
1222                                    mdt_reconstruct_t reconstruct,
1223                                    struct mdt_lock_handle *lhc)
1224 {
1225         struct ptlrpc_request *req = mdt_info_req(info);
1226         int rc = 0;
1227         ENTRY;
1228
1229         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1230                 OBD_ALLOC_PTR(info->mti_reply_data);
1231                 if (info->mti_reply_data == NULL)
1232                         RETURN(-ENOMEM);
1233
1234                 rc = req_can_reconstruct(req, info->mti_reply_data);
1235                 if (rc == 1) {
1236                         reconstruct(info, lhc);
1237                 } else {
1238                         DEBUG_REQ(D_HA, req,
1239                                   "no reply data found for RESENT req, rc = %d",
1240                                   rc);
1241                 }
1242                 OBD_FREE_PTR(info->mti_reply_data);
1243                 info->mti_reply_data = NULL;
1244         }
1245         RETURN(rc);
1246 }
1247
1248 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info);
1249 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info);
1250
1251 static inline int is_identity_get_disabled(struct upcall_cache *cache)
1252 {
1253         return cache ? (strcmp(cache->uc_upcall, "NONE") == 0) : 1;
1254 }
1255
1256 int mdt_blocking_ast(struct ldlm_lock*, struct ldlm_lock_desc*, void*, int);
1257
1258 static int mdt_dom_glimpse_ast(struct ldlm_lock *lock, void *reqp)
1259 {
1260         return -ELDLM_NO_LOCK_DATA;
1261 }
1262
1263 /* Issues dlm lock on passed @ns, @f stores it lock handle into @lh. */
1264 static inline int mdt_fid_lock(const struct lu_env *env,
1265                                struct ldlm_namespace *ns,
1266                                struct lustre_handle *lh, enum ldlm_mode mode,
1267                                union ldlm_policy_data *policy,
1268                                const struct ldlm_res_id *res_id,
1269                                __u64 flags, const __u64 *client_cookie)
1270 {
1271         int rc;
1272         bool glimpse = policy->l_inodebits.bits & MDS_INODELOCK_DOM;
1273
1274         LASSERT(ns != NULL);
1275         LASSERT(lh != NULL);
1276
1277         rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS, policy,
1278                                     mode, &flags, mdt_blocking_ast,
1279                                     ldlm_completion_ast,
1280                                     glimpse ? mdt_dom_glimpse_ast : NULL,
1281                                     NULL, 0, LVB_T_NONE, client_cookie, lh);
1282         return rc == ELDLM_OK ? 0 : -EIO;
1283 }
1284
1285 static inline void mdt_fid_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
1286 {
1287         ldlm_lock_decref(lh, mode);
1288 }
1289
1290 /* this is identical to say this is a DNE system */
1291 static inline bool mdt_slc_is_enabled(struct mdt_device *mdt)
1292 {
1293         return mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING;
1294 }
1295
1296 extern mdl_mode_t mdt_mdl_lock_modes[];
1297 extern enum ldlm_mode mdt_dlm_lock_modes[];
1298
1299 /* LCK_MINMODE which is zero returns false for is_power_of_2 */
1300
1301 static inline mdl_mode_t mdt_dlm_mode2mdl_mode(enum ldlm_mode mode)
1302 {
1303         LASSERT(mode == LCK_MINMODE || is_power_of_2(mode));
1304         return mdt_mdl_lock_modes[mode];
1305 }
1306
1307 static inline enum ldlm_mode mdt_mdl_mode2dlm_mode(mdl_mode_t mode)
1308 {
1309         LASSERT(mode == MDL_MINMODE || is_power_of_2(mode));
1310         return mdt_dlm_lock_modes[mode];
1311 }
1312
1313 /* mdt_som.c */
1314 int mdt_set_som(struct mdt_thread_info *info, struct mdt_object *obj,
1315                 enum lustre_som_flags flag, __u64 size, __u64 blocks);
1316 int mdt_get_som(struct mdt_thread_info *info, struct mdt_object *obj,
1317                 struct md_attr *ma);
1318 int mdt_lsom_downgrade(struct mdt_thread_info *info, struct mdt_object *obj);
1319 int mdt_lsom_update(struct mdt_thread_info *info, struct mdt_object *obj,
1320                     bool truncate);
1321
1322 /* mdt_lvb.c */
1323 extern struct ldlm_valblock_ops mdt_lvbo;
1324 int mdt_dom_lvb_is_valid(struct ldlm_resource *res);
1325 int mdt_dom_lvbo_update(struct ldlm_resource *res, struct ldlm_lock *lock,
1326                         struct ptlrpc_request *req, bool increase_only);
1327
1328 void mdt_enable_cos(struct mdt_device *dev, bool enable);
1329 int mdt_cos_is_enabled(struct mdt_device *);
1330
1331 /* lprocfs stuff */
1332 enum mdt_stat_idx {
1333         LPROC_MDT_OPEN,
1334         LPROC_MDT_CLOSE,
1335         LPROC_MDT_MKNOD,
1336         LPROC_MDT_LINK,
1337         LPROC_MDT_UNLINK,
1338         LPROC_MDT_MKDIR,
1339         LPROC_MDT_RMDIR,
1340         LPROC_MDT_RENAME,
1341         LPROC_MDT_GETATTR,
1342         LPROC_MDT_SETATTR,
1343         LPROC_MDT_GETXATTR,
1344         LPROC_MDT_SETXATTR,
1345         LPROC_MDT_STATFS,
1346         LPROC_MDT_SYNC,
1347         LPROC_MDT_RENAME_SAMEDIR,
1348         LPROC_MDT_RENAME_PAR_FILE,
1349         LPROC_MDT_RENAME_PAR_DIR,
1350         LPROC_MDT_RENAME_CROSSDIR,
1351         LPROC_MDT_IO_READ,
1352         LPROC_MDT_IO_WRITE,
1353         LPROC_MDT_IO_READ_BYTES,
1354         LPROC_MDT_IO_WRITE_BYTES,
1355         LPROC_MDT_IO_PUNCH,
1356         LPROC_MDT_MIGRATE,
1357         LPROC_MDT_FALLOCATE,
1358         LPROC_MDT_LAST,
1359 };
1360
1361 void mdt_counter_incr(struct ptlrpc_request *req, int opcode, long amount);
1362 void mdt_stats_counter_init(struct lprocfs_stats *stats, unsigned int offset,
1363                             enum lprocfs_counter_config cntr_umask);
1364 int mdt_tunables_init(struct mdt_device *mdt, const char *name);
1365 void mdt_tunables_fini(struct mdt_device *mdt);
1366
1367 /* lustre/mdt_mdt_lproc.c */
1368 int lprocfs_mdt_open_files_seq_open(struct inode *inode,
1369                                     struct file *file);
1370 void mdt_rename_counter_tally(struct mdt_thread_info *info,
1371                               struct mdt_device *mdt,
1372                               struct ptlrpc_request *req,
1373                               struct mdt_object *src, struct mdt_object *tgt,
1374                               enum mdt_stat_idx msi, s64 count);
1375
1376 static inline struct obd_device *mdt2obd_dev(const struct mdt_device *mdt)
1377 {
1378         return mdt->mdt_lu_dev.ld_obd;
1379 }
1380
1381 extern const struct lu_device_operations mdt_lu_ops;
1382
1383 static inline char *mdt_obd_name(struct mdt_device *mdt)
1384 {
1385         return mdt->mdt_lu_dev.ld_obd->obd_name;
1386 }
1387
1388 int mds_mod_init(void);
1389 void mds_mod_exit(void);
1390
1391 static inline char *mdt_req_get_jobid(struct ptlrpc_request *req)
1392 {
1393         struct obd_export       *exp = req->rq_export;
1394         char                    *jobid = NULL;
1395
1396         if (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS)
1397                 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1398
1399         return jobid;
1400 }
1401
1402 /* MDT IO */
1403
1404 #define VALID_FLAGS (LA_TYPE | LA_MODE | LA_SIZE | LA_BLOCKS | \
1405                      LA_BLKSIZE | LA_ATIME | LA_MTIME | LA_CTIME)
1406
1407 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
1408                    struct obdo *oa, int objcount, struct obd_ioobj *obj,
1409                    struct niobuf_remote *rnb, int *nr_local,
1410                    struct niobuf_local *lnb);
1411
1412 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
1413                      struct obdo *oa, int objcount, struct obd_ioobj *obj,
1414                      struct niobuf_remote *rnb, int npages,
1415                      struct niobuf_local *lnb, int old_rc, int nob,
1416                      ktime_t kstart);
1417 int mdt_punch_hdl(struct tgt_session_info *tsi);
1418 int mdt_fallocate_hdl(struct tgt_session_info *tsi);
1419 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1420                         struct ldlm_lock **lockp, __u64 flags);
1421 int mdt_brw_enqueue(struct mdt_thread_info *info, struct ldlm_namespace *ns,
1422                     struct ldlm_lock **lockp, __u64 flags);
1423 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1424                          struct lustre_handle *lh);
1425 void mdt_dom_discard_data(struct mdt_thread_info *info, struct mdt_object *mo);
1426 int mdt_dom_disk_lvbo_update(const struct lu_env *env, struct mdt_object *mo,
1427                              struct ldlm_resource *res, bool increase_only);
1428 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
1429                             struct obdo *oa, bool increase_only);
1430 int mdt_dom_lvb_alloc(struct ldlm_resource *res);
1431
1432 static inline bool mdt_dom_check_for_discard(struct mdt_thread_info *mti,
1433                                              struct mdt_object *mo)
1434 {
1435         return lu_object_is_dying(&mo->mot_header) &&
1436                S_ISREG(lu_object_attr(&mo->mot_obj));
1437 }
1438
1439 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1440                         const struct lu_fid *fid, struct mdt_body *mb,
1441                         bool dom_lock);
1442 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1443                              const struct lu_fid *fid);
1444 void mdt_hp_brw(struct tgt_session_info *tsi);
1445 void mdt_hp_punch(struct tgt_session_info *tsi);
1446 int mdt_data_version_get(struct tgt_session_info *tsi);
1447 int mdt_io_set_info(struct tgt_session_info *tsi);
1448
1449 /* grants */
1450 long mdt_grant_connect(const struct lu_env *env, struct obd_export *exp,
1451                        u64 want, bool conservative);
1452 extern struct kmem_cache *ldlm_glimpse_work_kmem;
1453
1454 static inline bool mdt_changelog_allow(struct mdt_thread_info *info)
1455 {
1456         struct lu_ucred *uc = NULL;
1457         bool is_admin;
1458         int rc;
1459
1460         if (info == NULL || info->mti_body == NULL)
1461 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 3, 0)
1462                 /* return true in case old client did not send mdt body */
1463                 return true;
1464 #else
1465                 return false;
1466 #endif
1467
1468         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
1469         if (rc < 0)
1470                 return false;
1471
1472         uc = mdt_ucred(info);
1473         is_admin = (uc->uc_uid == 0 && uc->uc_gid == 0 &&
1474                     cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
1475                     uc->uc_rbac_chlg_ops);
1476
1477         mdt_exit_ucred(info);
1478
1479         return is_admin;
1480 }
1481
1482 /* convert a capability into an integer to print or manage more easily */
1483 static inline u64 mdt_cap2num(kernel_cap_t cap)
1484 {
1485 #ifdef CAP_FOR_EACH_U32
1486         /* kernels before v6.2-13111-gf122a08b197d had a more complex
1487          * kernel_cap_t structure with an array of __u32 values, but this
1488          * was then fixed to have a single __u64 value.  There are accessor
1489          * functions for the old kernel_cap_t but since that is now dead code
1490          * it isn't worthwhile to jump through hoops for compatibility for it.
1491          */
1492         return ((u64)cap.cap[1] << 32) | cap.cap[0];
1493 #else
1494         return cap.val;
1495 #endif
1496 }
1497
1498 /* convert an integer into a capabilityt */
1499 static inline kernel_cap_t mdt_num2cap(u64 num)
1500 {
1501         kernel_cap_t cap;
1502
1503 #ifdef CAP_FOR_EACH_U32
1504         cap.cap[0] = num;
1505         cap.cap[1] = (num >> 32);
1506 #else
1507         cap.val = num;
1508 #endif
1509
1510         return cap;
1511 }
1512
1513 /* We forbid operations from encryption-unaware clients if they try to
1514  * manipulate encrypted files/directories.
1515  */
1516 static inline int mdt_check_enc(struct mdt_thread_info *info,
1517                                 struct mdt_object *obj)
1518 {
1519         struct obd_export *exp;
1520         struct md_attr ma = { 0 };
1521         int rc = 0;
1522
1523         if (!mdt_info_req(info))
1524                 /* no req, so cannot tell about client connection flags:
1525                  * allow access
1526                  */
1527                 return 0;
1528         exp = mdt_info_req(info)->rq_export;
1529         if (exp_connect_encrypt(exp))
1530                 /* client is encryption aware: fine */
1531                 return 0;
1532
1533         ma.ma_need = MA_INODE;
1534         mdt_attr_get_complex(info, obj, &ma);
1535         if (ma.ma_attr.la_valid & LA_FLAGS &&
1536             ma.ma_attr.la_flags & LUSTRE_ENCRYPT_FL)
1537                 rc = -ENOKEY;
1538
1539         if (rc)
1540                 CDEBUG(D_SEC, "%s: operation on encrypted "DFID
1541                        " from encryption-unaware client %s: %d\n",
1542                        mdt_obd_name(info->mti_mdt),
1543                        PFID(lu_object_fid(&obj->mot_obj)),
1544                        libcfs_nidstr(&exp->exp_connection->c_peer.nid), rc);
1545
1546         return rc;
1547 }
1548
1549 int mdt_fids_different_target(struct mdt_thread_info *info,
1550                               const struct lu_fid *fid1,
1551                               const struct lu_fid *fid2);
1552
1553 int mdt_reint_migrate(struct mdt_thread_info *info,
1554                       struct mdt_lock_handle *unused);
1555 int mdt_dir_layout_update(struct mdt_thread_info *info);
1556
1557 /* directory restripe */
1558 int mdt_restripe_internal(struct mdt_thread_info *info,
1559                           struct mdt_object *parent,
1560                           struct mdt_object *child,
1561                           const struct lu_name *lname,
1562                           struct lu_fid *tfid,
1563                           struct md_op_spec *spec,
1564                           struct md_attr *ma);
1565 int mdt_restriper_start(struct mdt_device *mdt);
1566 void mdt_restriper_stop(struct mdt_device *mdt);
1567 void mdt_auto_split_add(struct mdt_thread_info *info, struct mdt_object *o);
1568 void mdt_restripe_migrate_add(struct mdt_thread_info *info,
1569                               struct mdt_object *o);
1570 void mdt_restripe_update_add(struct mdt_thread_info *info,
1571                              struct mdt_object *o);
1572 int mdt_is_remote_object(struct mdt_thread_info *info,
1573                          struct mdt_object *parent,
1574                          struct mdt_object *child);
1575
1576 static unsigned int max_mod_rpcs_per_client = OBD_MAX_RIF_DEFAULT;
1577 #if OBD_OCD_VERSION(3, 0, 53, 0) > LUSTRE_VERSION_CODE
1578 static inline bool mdt_max_mod_rpcs_changed(struct mdt_device *mdt)
1579 {
1580         return max_mod_rpcs_per_client != OBD_MAX_RIF_DEFAULT &&
1581                 mdt->mdt_max_mod_rpcs_in_flight == OBD_MAX_RIF_DEFAULT;
1582 }
1583 #else
1584 #define mdt_max_mod_rpcs_changed(mdt) false
1585 #endif
1586
1587 #endif /* _MDT_INTERNAL_H */