Whamcloud - gitweb
7613f7050c93b9f2e90450ff1d382574d3837911
[fs/lustre-release.git] / lustre / mdt / mdt_internal.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mdt/mdt_internal.h
5  *  Lustre Metadata Target (mdt) request handler
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *
15  *   This file is part of the Lustre file system, http://www.lustre.org
16  *   Lustre is a trademark of Cluster File Systems, Inc.
17  *
18  *   You may have signed or agreed to another license before downloading
19  *   this software.  If so, you are bound by the terms and conditions
20  *   of that agreement, and the following does not apply to you.  See the
21  *   LICENSE file included with this distribution for more information.
22  *
23  *   If you did not agree to a different license, then this copy of Lustre
24  *   is open source software; you can redistribute it and/or modify it
25  *   under the terms of version 2 of the GNU General Public License as
26  *   published by the Free Software Foundation.
27  *
28  *   In either case, Lustre is distributed in the hope that it will be
29  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
30  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  *   license text for more details.
32  */
33
34 #ifndef _MDT_INTERNAL_H
35 #define _MDT_INTERNAL_H
36
37 #if defined(__KERNEL__)
38
39 /*
40  * struct ptlrpc_client
41  */
42 #include <lustre_net.h>
43 #include <obd.h>
44 /*
45  * struct obd_connect_data
46  * struct lustre_handle
47  */
48 #include <lustre/lustre_idl.h>
49 #include <md_object.h>
50 #include <dt_object.h>
51 #include <lustre_fid.h>
52 #include <lustre_fld.h>
53 #include <lustre_req_layout.h>
54 /* LR_CLIENT_SIZE, etc. */
55 #include <lustre_disk.h>
56
57
58 /* Data stored per client in the last_rcvd file.  In le32 order. */
59 struct mdt_client_data {
60         __u8  mcd_uuid[40];     /* client UUID */
61         __u64 mcd_last_transno; /* last completed transaction ID */
62         __u64 mcd_last_xid;     /* xid for the last transaction */
63         __u32 mcd_last_result;  /* result from last RPC */
64         __u32 mcd_last_data;    /* per-op data (disposition for open &c.) */
65         /* for MDS_CLOSE requests */
66         __u64 mcd_last_close_transno; /* last completed transaction ID */
67         __u64 mcd_last_close_xid;     /* xid for the last transaction */
68         __u32 mcd_last_close_result;  /* result from last RPC */
69         __u8 mcd_padding[LR_CLIENT_SIZE - 84];
70 };
71
72 static inline __u64 mcd_last_transno(struct mdt_client_data *mcd)
73 {
74         return (le64_to_cpu(mcd->mcd_last_transno) > 
75                 le64_to_cpu(mcd->mcd_last_close_transno) ?
76                 le64_to_cpu(mcd->mcd_last_transno) :
77                 le64_to_cpu(mcd->mcd_last_close_transno));
78 }
79
80 static inline __u64 mcd_last_xid(struct mdt_client_data *mcd)
81 {
82         return (le64_to_cpu(mcd->mcd_last_xid) > 
83                 le64_to_cpu(mcd->mcd_last_close_xid) ?
84                 le64_to_cpu(mcd->mcd_last_xid) :
85                 le64_to_cpu(mcd->mcd_last_close_xid));
86 }
87
88 /* copied from lr_server_data.
89  * mds data stored at the head of last_rcvd file. In le32 order. */
90 struct mdt_server_data {
91         __u8  msd_uuid[40];        /* server UUID */
92         __u64 msd_unused;          /* was fsd_last_objid - don't use for now */
93         __u64 msd_last_transno;    /* last completed transaction ID */
94         __u64 msd_mount_count;     /* incarnation number */
95         __u32 msd_feature_compat;  /* compatible feature flags */
96         __u32 msd_feature_rocompat;/* read-only compatible feature flags */
97         __u32 msd_feature_incompat;/* incompatible feature flags */
98         __u32 msd_server_size;     /* size of server data area */
99         __u32 msd_client_start;    /* start of per-client data area */
100         __u16 msd_client_size;     /* size of per-client data area */
101         __u16 msd_subdir_count;    /* number of subdirectories for objects */
102         __u64 msd_catalog_oid;     /* recovery catalog object id */
103         __u32 msd_catalog_ogen;    /* recovery catalog inode generation */
104         __u8  msd_peeruuid[40];    /* UUID of MDS associated with this OST */
105         __u32 msd_ost_index;       /* index number of OST in LOV */
106         __u32 msd_mdt_index;       /* index number of MDT in LMV */
107         __u8  msd_padding[LR_SERVER_SIZE - 148];
108 };
109
110 struct mdt_object;
111 /* file data for open files on MDS */
112 struct mdt_file_data {
113         struct portals_handle mfd_handle; /* must be first */
114         struct list_head      mfd_list;   /* protected by med_open_lock */
115         __u64                 mfd_xid;    /* xid of the open request */
116         int                   mfd_mode;   /* open mode provided by client */
117         struct mdt_object    *mfd_object; /* point to opened object */
118 };
119
120 struct mdt_device {
121         /* super-class */
122         struct md_device           mdt_md_dev;
123         struct ptlrpc_service     *mdt_service;
124         struct ptlrpc_service     *mdt_readpage_service;
125         struct ptlrpc_service     *mdt_setattr_service;
126         /* DLM name-space for meta-data locks maintained by this server */
127         struct ldlm_namespace     *mdt_namespace;
128         /* ptlrpc handle for MDS->client connections (for lock ASTs). */
129         struct ptlrpc_client      *mdt_ldlm_client;
130         /* underlying device */
131         struct md_device          *mdt_child;
132         struct dt_device          *mdt_bottom;
133         /*
134          * Options bit-fields.
135          */
136         struct {
137                 signed int         mo_user_xattr :1;
138                 signed int         mo_acl        :1;
139                 signed int         mo_compat_resname:1;
140         } mdt_opts;
141
142         /* lock to pretect epoch and write count
143          */
144         spinlock_t                 mdt_epoch_lock;
145         __u64                      mdt_io_epoch;
146
147         /* Transaction related stuff here */
148         spinlock_t                 mdt_transno_lock;
149         __u64                      mdt_last_transno;
150
151         /* transaction callbacks */
152         struct dt_txn_callback     mdt_txn_cb;
153         /* last_rcvd file */
154         struct dt_object          *mdt_last_rcvd;
155
156         /* these values should be updated from lov if necessary.
157          * or should be placed somewhere else. */
158         int                        mdt_max_mdsize;
159         int                        mdt_max_cookiesize;
160         __u64                      mdt_mount_count;
161
162         struct mdt_server_data     mdt_msd;
163         unsigned long              mdt_client_bitmap[(LR_MAX_CLIENTS >> 3) / sizeof(long)];
164 };
165
166 /*XXX copied from mds_internal.h */
167 #define MDT_SERVICE_WATCHDOG_TIMEOUT (obd_timeout * 1000)
168 #define MDT_ROCOMPAT_SUPP       (OBD_ROCOMPAT_LOVOBJID)
169 #define MDT_INCOMPAT_SUPP       (OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR)
170
171 struct mdt_object {
172         struct lu_object_header mot_header;
173         struct md_object        mot_obj;
174         __u64                   mot_io_epoch;
175         int                     mot_writecount;
176 };
177
178 struct mdt_lock_handle {
179         struct lustre_handle    mlh_lh;
180         ldlm_mode_t             mlh_mode;
181 };
182
183 enum {
184         MDT_REP_BUF_NR_MAX = 8
185 };
186
187 enum {
188         MDT_LH_PARENT,
189         MDT_LH_CHILD,
190         MDT_LH_OLD,
191         MDT_LH_NEW,
192         MDT_LH_NR
193 };
194
195 struct mdt_reint_record {
196         mdt_reint_t          rr_opcode;
197         const struct lu_fid *rr_fid1;
198         const struct lu_fid *rr_fid2;
199         const char          *rr_name;
200         const char          *rr_tgt;
201         int                  rr_eadatalen;
202         const void          *rr_eadata;
203         int                  rr_logcookielen;
204         const struct llog_cookie  *rr_logcookies;
205         __u32                rr_flags;
206 };
207
208 enum mdt_reint_flag {
209         MRF_SETATTR_LOCKED = 1 << 0,
210 };
211
212 enum {
213         MDT_NONEED_TRANSNO = (1 << 0) /*Do not need transno for this req*/
214 };
215 /*
216  * Common data shared by mdt-level handlers. This is allocated per-thread to
217  * reduce stack consumption.
218  */
219 struct mdt_thread_info {
220         const struct lu_context   *mti_ctxt;
221         struct mdt_device         *mti_mdt;
222         /*
223          * number of buffers in reply message.
224          */
225         int                        mti_rep_buf_nr;
226         /*
227          * sizes of reply buffers.
228          */
229         int                        mti_rep_buf_size[MDT_REP_BUF_NR_MAX];
230         /*
231          * Body for "habeo corpus" operations.
232          */
233         const struct mdt_body     *mti_body;
234         /*
235          * Lock request for "habeo clavis" operations.
236          */
237         const struct ldlm_request *mti_dlm_req;
238         /*
239          * Host object. This is released at the end of mdt_handler().
240          */
241         struct mdt_object         *mti_object;
242         /*
243          * Object attributes.
244          */
245         struct md_attr             mti_attr;
246         /*
247          * Create specification
248          */
249         struct md_create_spec mti_spec;
250         /*
251          * reint record. contains information for reint operations.
252          */
253         struct mdt_reint_record    mti_rr;
254         /*
255          * Additional fail id that can be set by handler. Passed to
256          * target_send_reply().
257          */
258         int                        mti_fail_id;
259         /*
260          * A couple of lock handles.
261          */
262         struct mdt_lock_handle     mti_lh[MDT_LH_NR];
263         /*
264          * for req-layout interface.
265          */
266         struct req_capsule         mti_pill;
267         /*
268          * buffer for mdt_statfs().
269          *
270          * XXX this is probably huge overkill, because statfs is not that
271          * frequent.
272          */
273
274         struct kstatfs             mti_sfs;
275
276         /* temporary stuff used by thread */
277         struct lu_fid              mti_tmp_fid1;
278         struct lu_fid              mti_tmp_fid2;
279         ldlm_policy_data_t         mti_policy;
280         struct ldlm_res_id         mti_res_id;
281         union {
282                 struct obd_uuid    uuid;
283                 char               ns_name[48];
284         } mti_u;
285         /* transaction number of current request */
286         __u64                      mti_transno;
287         __u32                      mti_trans_flags;
288
289         /* readdir hint structure */
290         struct lu_rdpg             mti_rdpg;
291 };
292 /*
293  * Info allocated per-transaction.
294  */
295 struct mdt_txn_info {
296         __u64  txi_transno;
297 };
298
299 static inline struct md_device_operations *mdt_child_ops(struct mdt_device * m)
300 {
301         LASSERT(m->mdt_child);
302         return m->mdt_child->md_ops;
303 }
304
305 static inline struct md_object *mdt_object_child(struct mdt_object *o)
306 {
307         return lu2md(lu_object_next(&o->mot_obj.mo_lu));
308 }
309
310 static inline struct ptlrpc_request *mdt_info_req(struct mdt_thread_info *info)
311 {
312          return info->mti_pill.rc_req;
313 }
314
315 static inline void mdt_object_get(const struct lu_context *ctxt,
316                                   struct mdt_object *o)
317 {
318         lu_object_get(&o->mot_obj.mo_lu);
319 }
320
321 static inline void mdt_object_put(const struct lu_context *ctxt,
322                                   struct mdt_object *o)
323 {
324         lu_object_put(ctxt, &o->mot_obj.mo_lu);
325 }
326
327 static inline const struct lu_fid *mdt_object_fid(struct mdt_object *o)
328 {
329         return lu_object_fid(&o->mot_obj.mo_lu);
330 }
331
332 int mdt_object_lock(struct mdt_thread_info *,
333                     struct mdt_object *,
334                     struct mdt_lock_handle *,
335                     __u64);
336
337 void mdt_object_unlock(struct mdt_thread_info *,
338                        struct mdt_object *,
339                        struct mdt_lock_handle *,
340                        int decref);
341
342 struct mdt_object *mdt_object_find(const struct lu_context *,
343                                    struct mdt_device *,
344                                    const struct lu_fid *);
345 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *,
346                                         const struct lu_fid *,
347                                         struct mdt_lock_handle *,
348                                         __u64);
349 void mdt_object_unlock_put(struct mdt_thread_info *,
350                            struct mdt_object *,
351                            struct mdt_lock_handle *,
352                            int decref);
353
354 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op);
355 int mdt_reint_rec(struct mdt_thread_info *);
356 void mdt_pack_attr2body(struct mdt_body *b, const struct lu_attr *attr,
357                         const struct lu_fid *fid);
358
359 int mdt_getxattr(struct mdt_thread_info *info);
360 int mdt_setxattr(struct mdt_thread_info *info);
361
362 void mdt_lock_handle_init(struct mdt_lock_handle *lh);
363 void mdt_lock_handle_fini(struct mdt_lock_handle *lh);
364
365 void mdt_reconstruct(struct mdt_thread_info *);
366
367 int mdt_fs_setup(const struct lu_context *, struct mdt_device *);
368 void mdt_fs_cleanup(const struct lu_context *, struct mdt_device *);
369
370 int mdt_client_free(const struct lu_context *ctxt,
371                     struct mdt_device *mdt,
372                     struct mdt_export_data *med);
373 int mdt_client_add(const struct lu_context *ctxt,
374                    struct mdt_device *mdt,
375                    struct mdt_export_data *med,
376                    int cl_idx);
377
378 int mdt_pin(struct mdt_thread_info* info);
379
380 int mdt_lock_new_child(struct mdt_thread_info *info,
381                        struct mdt_object *o,
382                        struct mdt_lock_handle *child_lockh);
383
384 int mdt_open(struct mdt_thread_info *info);
385
386 void mdt_mfd_close(const struct lu_context *ctxt, struct mdt_device *mdt,
387                    struct mdt_file_data *mfd, struct md_attr *ma);
388
389 int mdt_close(struct mdt_thread_info *info);
390
391 int mdt_done_writing(struct mdt_thread_info *info);
392 void mdt_shrink_reply(struct mdt_thread_info *info, int offset);
393 int mdt_handle_last_unlink(struct mdt_thread_info *, struct mdt_object *,
394                            const struct md_attr *);
395
396 void mdt_dump_lmm(int level, struct lov_mds_md *lmm);
397
398 extern struct lu_context_key       mdt_thread_key;
399 /* debug issues helper starts here*/
400 static inline void mdt_fail_write(const struct lu_context *ctx,
401                                   const struct dt_device *dd, int id)
402 {
403         if (OBD_FAIL_CHECK(id)) {
404                 CERROR(LUSTRE_MDT0_NAME": obd_fail_loc=%x, fail write ops\n",
405                        id);
406                 dd->dd_ops->dt_ro_set(ctx, dd);
407                 /* We set FAIL_ONCE because we never "un-fail" a device */
408                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
409         }
410 }
411
412 #define MDT_FAIL_CHECK(id)                                              \
413 ({                                                                      \
414         if (OBD_FAIL_CHECK(id))                                         \
415                 CERROR(LUSTRE_MDT0_NAME": " #id " test failed\n");      \
416         OBD_FAIL_CHECK(id);                                             \
417 })
418
419 #define MDT_FAIL_CHECK_ONCE(id)                                              \
420 ({      int _ret_ = 0;                                                       \
421         if (OBD_FAIL_CHECK(id)) {                                            \
422                 CERROR(LUSTRE_MDT0_NAME": *** obd_fail_loc=%x ***\n", id);   \
423                 obd_fail_loc |= OBD_FAILED;                                  \
424                 if ((id) & OBD_FAIL_ONCE)                                    \
425                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
426                 _ret_ = 1;                                                   \
427         }                                                                    \
428         _ret_;                                                               \
429 })
430
431 #define MDT_FAIL_RETURN(id, ret)                                             \
432 do {                                                                         \
433         if (MDT_FAIL_CHECK_ONCE(id)) {                                       \
434                 RETURN(ret);                                                 \
435         }                                                                    \
436 } while(0)
437
438 #endif /* __KERNEL__ */
439 #endif /* _MDT_H */