Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / liblustre / llite_lib.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __LLU_H_
38 #define __LLU_H_
39
40 #include <liblustre.h>
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <lustre_mdc.h>
44 #include <lustre_lite.h>
45 #include <lustre_ver.h>
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49
50 /* for struct cl_lock_descr and struct cl_io */
51 #include <cl_object.h>
52 #include <lclient.h>
53
54 /* This should not be "optimized" use ~0ULL because page->index is a long and
55  * 32-bit systems are therefore limited to 16TB in a mapping */
56 #define PAGE_CACHE_MAXBYTES ((__u64)(~0UL) << CFS_PAGE_SHIFT)
57
58 struct ll_file_data {
59         struct obd_client_handle fd_mds_och;
60         __u32 fd_flags;
61         struct lustre_handle fd_cwlockh;
62         unsigned long fd_gid;
63 };
64
65 struct llu_sb_info {
66         struct obd_uuid          ll_sb_uuid;
67         struct obd_export       *ll_md_exp;
68         struct obd_export       *ll_dt_exp;
69         struct lu_fid            ll_root_fid;
70         int                      ll_flags;
71         struct lustre_client_ocd ll_lco;
72         struct list_head         ll_conn_chain;
73
74         struct obd_uuid          ll_mds_uuid;
75         struct obd_uuid          ll_mds_peer_uuid;
76         char                    *ll_instance;
77         struct lu_site           *ll_site;
78         struct cl_device         *ll_cl;
79 };
80
81 #define LL_SBI_NOLCK            0x1
82
83 enum lli_flags {
84         /* MDS has an authority for the Size-on-MDS attributes. */
85         LLIF_MDS_SIZE_LOCK      = (1 << 0),
86 };
87
88 struct llu_inode_info {
89         struct llu_sb_info     *lli_sbi;
90         struct lu_fid           lli_fid;
91
92         struct lov_stripe_md   *lli_smd;
93         char                   *lli_symlink_name;
94         struct semaphore        lli_open_sem;
95         __u64                   lli_maxbytes;
96         unsigned long           lli_flags;
97         __u64                   lli_ioepoch;
98
99         /* for libsysio */
100         struct file_identifier  lli_sysio_fid;
101
102         struct lookup_intent   *lli_it;
103
104         /* XXX workaround for libsysio readdir */
105         loff_t                  lli_dir_pos;
106
107         /* in libsysio we have no chance to store data in file,
108          * so place it here. since it's possible that an file
109          * was opened several times without close, we track an
110          * open_count here */
111         struct ll_file_data    *lli_file_data;
112         int                     lli_open_flags;
113         int                     lli_open_count;
114
115         /* not for stat, change it later */
116         int                     lli_st_flags;
117         unsigned long           lli_st_generation;
118         struct cl_object       *lli_clob;
119 };
120
121
122 static inline struct llu_sb_info *llu_fs2sbi(struct filesys *fs)
123 {
124         return (struct llu_sb_info*)(fs->fs_private);
125 }
126
127 static inline struct llu_inode_info *llu_i2info(struct inode *inode)
128 {
129         return (struct llu_inode_info*)(inode->i_private);
130 }
131
132 static inline struct intnl_stat *llu_i2stat(struct inode *inode)
133 {
134         return &inode->i_stbuf;
135 }
136
137 static inline struct llu_sb_info *llu_i2sbi(struct inode *inode)
138 {
139         return llu_i2info(inode)->lli_sbi;
140 }
141
142 static inline struct obd_export *llu_i2obdexp(struct inode *inode)
143 {
144         return llu_i2info(inode)->lli_sbi->ll_dt_exp;
145 }
146
147 static inline struct obd_export *llu_i2mdexp(struct inode *inode)
148 {
149         return llu_i2info(inode)->lli_sbi->ll_md_exp;
150 }
151
152 static inline int llu_is_root_inode(struct inode *inode)
153 {
154         return (fid_seq(&llu_i2info(inode)->lli_fid) ==
155                 fid_seq(&llu_i2info(inode)->lli_sbi->ll_root_fid) &&
156                 fid_oid(&llu_i2info(inode)->lli_fid) ==
157                 fid_oid(&llu_i2info(inode)->lli_sbi->ll_root_fid));
158 }
159
160 #define LL_SAVE_INTENT(inode, it)                                              \
161 do {                                                                           \
162         struct lookup_intent *temp;                                            \
163         LASSERT(llu_i2info(inode)->lli_it == NULL);                            \
164         OBD_ALLOC(temp, sizeof(*temp));                                        \
165         memcpy(temp, it, sizeof(*temp));                                       \
166         llu_i2info(inode)->lli_it = temp;                                      \
167         CDEBUG(D_DENTRY, "alloc intent %p to inode %p(ino %llu)\n",            \
168                         temp, inode, (long long)llu_i2stat(inode)->st_ino);    \
169 } while(0)
170
171
172 #define LL_GET_INTENT(inode, it)                                               \
173 do {                                                                           \
174         it = llu_i2info(inode)->lli_it;                                        \
175                                                                                \
176         LASSERT(it);                                                           \
177         llu_i2info(inode)->lli_it = NULL;                                      \
178         CDEBUG(D_DENTRY, "dettach intent %p from inode %p(ino %llu)\n",        \
179                         it, inode, (long long)llu_i2stat(inode)->st_ino);      \
180 } while(0)
181
182 /* interpet return codes from intent lookup */
183 #define LL_LOOKUP_POSITIVE 1
184 #define LL_LOOKUP_NEGATIVE 2
185
186 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
187 {
188         LASSERT(inode != NULL);
189         return &llu_i2info(inode)->lli_fid;
190 }
191
192 struct it_cb_data {
193         struct inode *icbd_parent;
194         struct pnode *icbd_child;
195         obd_id hash;
196 };
197
198 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
199
200 typedef int (*intent_finish_cb)(struct ptlrpc_request *,
201                                 struct inode *parent, struct pnode *pnode,
202                                 struct lookup_intent *, int offset, obd_id ino);
203 int llu_intent_lock(struct inode *parent, struct pnode *pnode,
204                     struct lookup_intent *, int flags, intent_finish_cb);
205
206 static inline __u64 ll_file_maxbytes(struct inode *inode)
207 {
208         return llu_i2info(inode)->lli_maxbytes;
209 }
210
211 struct mount_option_s
212 {
213         char *md_uuid;
214         char *dt_uuid;
215 };
216
217 #define IS_BAD_PTR(ptr)         \
218         ((unsigned long)(ptr) == 0 || (unsigned long)(ptr) > -1000UL)
219
220 /* llite_lib.c */
221 int liblustre_process_log(struct config_llog_instance *cfg, char *mgsnid,
222                           char *profile, int allow_recov);
223 int ll_parse_mount_target(const char *target, char **mgsnid,
224                           char **fsname);
225
226 extern struct mount_option_s mount_option;
227
228 /* super.c */
229 void llu_update_inode(struct inode *inode, struct lustre_md *md);
230 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid);
231 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid);
232 int ll_it_open_error(int phase, struct lookup_intent *it);
233 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md);
234 int llu_inode_getattr(struct inode *inode, struct obdo *obdo);
235 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
236                    struct md_open_data **mod);
237 int llu_setattr_raw(struct inode *inode, struct iattr *attr);
238
239 extern struct fssw_ops llu_fssw_ops;
240
241 /* file.c */
242 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
243                          struct inode *i2, const char *name, int namelen,
244                          int mode, __u32 opc);
245 void llu_finish_md_op_data(struct md_op_data *op_data);
246 int llu_create(struct inode *dir, struct pnode_base *pnode, int mode);
247 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it);
248 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode);
249 int llu_md_close(struct obd_export *md_exp, struct inode *inode);
250 int llu_file_release(struct inode *inode);
251 int llu_sizeonmds_update(struct inode *inode, struct lustre_handle *fh,
252                          __u64 ioepoch);
253 int llu_iop_close(struct inode *inode);
254 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off);
255 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag obd_flags);
256 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid);
257 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir);
258
259 /* rw.c */
260 int llu_iop_read(struct inode *ino, struct ioctx *ioctxp);
261 int llu_iop_write(struct inode *ino, struct ioctx *ioctxp);
262 int llu_iop_iodone(struct ioctx *ioctxp);
263 int llu_glimpse_size(struct inode *inode);
264 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
265                     struct lov_stripe_md *lsm, int mode,
266                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
267                     int ast_flags);
268 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
269                       struct lov_stripe_md *lsm, int mode,
270                       struct lustre_handle *lockh);
271
272 /* namei.c */
273 int llu_iop_lookup(struct pnode *pnode,
274                    struct inode **inop,
275                    struct intent *intnt,
276                    const char *path);
277 void unhook_stale_inode(struct pnode *pno);
278 struct inode *llu_inode_from_lock(struct ldlm_lock *lock);
279 int llu_md_blocking_ast(struct ldlm_lock *lock,
280                         struct ldlm_lock_desc *desc,
281                         void *data, int flag);
282
283 /* dir.c */
284 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep,
285                                char *buf, size_t nbytes);
286
287 /* liblustre/llite_fid.c*/
288 unsigned long llu_fid_build_ino(struct llu_sb_info *sbi,
289                                 struct lu_fid *fid);
290
291 /* ext2 related */
292 #define EXT2_NAME_LEN (255)
293
294 struct ext2_dirent {
295         __u32   inode;
296         __u16   rec_len;
297         __u8    name_len;
298         __u8    file_type;
299         char    name[EXT2_NAME_LEN];
300 };
301
302 #define EXT2_DIR_PAD                    4
303 #define EXT2_DIR_ROUND                  (EXT2_DIR_PAD - 1)
304 #define EXT2_DIR_REC_LEN(name_len)      (((name_len) + 8 + EXT2_DIR_ROUND) & \
305                                          ~EXT2_DIR_ROUND)
306
307 static inline struct ext2_dirent *ext2_next_entry(struct ext2_dirent *p)
308 {
309         return (struct ext2_dirent*)((char*) p + le16_to_cpu(p->rec_len));
310 }
311
312 int llu_merge_lvb(struct inode *inode);
313
314 static inline void inode_init_lvb(struct inode *inode, struct ost_lvb *lvb)
315 {
316         struct intnl_stat *st = llu_i2stat(inode);
317         lvb->lvb_size = st->st_size;
318         lvb->lvb_blocks = st->st_blocks;
319         lvb->lvb_mtime = st->st_mtime;
320         lvb->lvb_atime = st->st_atime;
321         lvb->lvb_ctime = st->st_ctime;
322 }
323
324 #define LLU_IO_GROUP_SIZE(x) \
325         (sizeof(struct llu_io_group) + \
326          (sizeof(struct ll_async_page) + \
327           sizeof(cfs_page_t) + \
328           llap_cookie_size) * (x))
329
330 #define LLU_IO_SESSION_SIZE(x)  \
331         (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
332
333 struct llu_io_session {
334         struct inode           *lis_inode;
335         int                     lis_cmd;
336         int                     lis_max_groups;
337         int                     lis_ngroups;
338         struct llu_io_group    *lis_groups[0];
339 };
340
341 struct llu_io_group
342 {
343         struct lustre_rw_params *lig_params;
344         int                     lig_rc;
345         __u64                   lig_rwcount;
346 };
347
348 struct llu_io_session;
349 void put_io_group(struct llu_io_group *group);
350
351 int cl_sb_init(struct llu_sb_info *sbi);
352 int cl_sb_fini(struct llu_sb_info *sbi);
353 int cl_inode_init(struct inode *inode, struct lustre_md *md);
354 void cl_inode_fini(struct inode *inode);
355
356 void llu_io_init(struct cl_io *io, struct inode *inode, int write);
357
358 struct slp_io {
359         struct llu_io_session *sio_session;
360 };
361
362 struct slp_session {
363         struct slp_io ss_ios;
364 };
365
366 static inline struct slp_session *slp_env_session(const struct lu_env *env)
367 {
368         extern struct lu_context_key slp_session_key;
369         struct slp_session *ses;
370         ses = lu_context_key_get(env->le_ses, &slp_session_key);
371         LASSERT(ses != NULL);
372         return ses;
373 }
374 static inline struct slp_io *slp_env_io(const struct lu_env *env)
375 {
376         return &slp_env_session(env)->ss_ios;
377 }
378
379 /* lclient compat stuff */
380 #define cl_inode_info llu_inode_info
381 #define cl_i2info(info) llu_i2info(info)
382 #define cl_inode_mode(inode) (llu_i2stat(inode)->st_mode)
383 #define cl_i2sbi llu_i2sbi
384 #define cl_isize_read(inode) (llu_i2stat(inode)->st_size)
385 #define cl_isize_write(inode,kms) do{llu_i2stat(inode)->st_size = kms;}while(0)
386 #define cl_isize_write_nolock(inode,kms) do{llu_i2stat(inode)->st_size = kms;}while(0)
387
388 static inline void cl_isize_lock(struct inode *inode, int lsmlock)
389 {
390 }
391
392 static inline void cl_isize_unlock(struct inode *inode, int lsmlock)
393 {
394 }
395
396 static inline int cl_merge_lvb(struct inode *inode)
397 {
398         return llu_merge_lvb(inode);
399 }
400
401 #define cl_inode_atime(inode) (llu_i2stat(inode)->st_atime)
402 #define cl_inode_ctime(inode) (llu_i2stat(inode)->st_ctime)
403 #define cl_inode_mtime(inode) (llu_i2stat(inode)->st_mtime)
404
405 static inline struct obd_capa *cl_capa_lookup(struct inode *inode,
406                                               enum cl_req_type crt)
407 {
408         return NULL;
409 }
410
411 #endif