Whamcloud - gitweb
b=16098
[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 /* This should not be "optimized" use ~0ULL because page->index is a long and 
51  * 32-bit systems are therefore limited to 16TB in a mapping */
52 #define PAGE_CACHE_MAXBYTES ((__u64)(~0UL) << CFS_PAGE_SHIFT)
53
54 struct ll_file_data {
55         struct obd_client_handle fd_mds_och;
56         __u32 fd_flags;
57         struct lustre_handle fd_cwlockh;
58         unsigned long fd_gid;
59 };
60
61 struct llu_sb_info {
62         struct obd_uuid          ll_sb_uuid;
63         struct obd_export       *ll_md_exp;
64         struct obd_export       *ll_dt_exp;
65         struct lu_fid            ll_root_fid;
66         int                      ll_flags;
67         struct lustre_client_ocd ll_lco;
68         struct list_head         ll_conn_chain;
69
70         struct obd_uuid          ll_mds_uuid;
71         struct obd_uuid          ll_mds_peer_uuid;
72         char                    *ll_instance;
73 };
74
75 #define LL_SBI_NOLCK            0x1
76
77 enum lli_flags {
78         /* MDS has an authority for the Size-on-MDS attributes. */
79         LLIF_MDS_SIZE_LOCK      = (1 << 0),
80 };
81
82 struct llu_inode_info {
83         struct llu_sb_info     *lli_sbi;
84         struct lu_fid           lli_fid;
85
86         struct lov_stripe_md   *lli_smd;
87         char                   *lli_symlink_name;
88         struct semaphore        lli_open_sem;
89         __u64                   lli_maxbytes;
90         unsigned long           lli_flags;
91         __u64                   lli_ioepoch;
92
93         /* for libsysio */
94         struct file_identifier  lli_sysio_fid;
95
96         struct lookup_intent   *lli_it;
97
98         /* XXX workaround for libsysio readdir */
99         loff_t                  lli_dir_pos;
100
101         /* in libsysio we have no chance to store data in file,
102          * so place it here. since it's possible that an file
103          * was opened several times without close, we track an
104          * open_count here */
105         struct ll_file_data    *lli_file_data;
106         int                     lli_open_flags;
107         int                     lli_open_count;
108
109         /* not for stat, change it later */
110         int                     lli_st_flags;
111         unsigned long           lli_st_generation;
112 };
113
114 static inline struct llu_sb_info *llu_fs2sbi(struct filesys *fs)
115 {
116         return (struct llu_sb_info*)(fs->fs_private);
117 }
118
119 static inline struct llu_inode_info *llu_i2info(struct inode *inode)
120 {
121         return (struct llu_inode_info*)(inode->i_private);
122 }
123
124 static inline struct intnl_stat *llu_i2stat(struct inode *inode)
125 {
126         return &inode->i_stbuf;
127 }
128
129 static inline struct llu_sb_info *llu_i2sbi(struct inode *inode)
130 {
131         return llu_i2info(inode)->lli_sbi;
132 }
133
134 static inline struct obd_export *llu_i2obdexp(struct inode *inode)
135 {
136         return llu_i2info(inode)->lli_sbi->ll_dt_exp;
137 }
138
139 static inline struct obd_export *llu_i2mdexp(struct inode *inode)
140 {
141         return llu_i2info(inode)->lli_sbi->ll_md_exp;
142 }
143
144 static inline int llu_is_root_inode(struct inode *inode)
145 {
146         return (fid_seq(&llu_i2info(inode)->lli_fid) ==
147                 fid_seq(&llu_i2info(inode)->lli_sbi->ll_root_fid) &&
148                 fid_oid(&llu_i2info(inode)->lli_fid) ==
149                 fid_oid(&llu_i2info(inode)->lli_sbi->ll_root_fid));
150 }
151
152 #define LL_SAVE_INTENT(inode, it)                                              \
153 do {                                                                           \
154         struct lookup_intent *temp;                                            \
155         LASSERT(llu_i2info(inode)->lli_it == NULL);                            \
156         OBD_ALLOC(temp, sizeof(*temp));                                        \
157         memcpy(temp, it, sizeof(*temp));                                       \
158         llu_i2info(inode)->lli_it = temp;                                      \
159         CDEBUG(D_DENTRY, "alloc intent %p to inode %p(ino %llu)\n",            \
160                         temp, inode, (long long)llu_i2stat(inode)->st_ino);    \
161 } while(0)
162
163
164 #define LL_GET_INTENT(inode, it)                                               \
165 do {                                                                           \
166         it = llu_i2info(inode)->lli_it;                                        \
167                                                                                \
168         LASSERT(it);                                                           \
169         llu_i2info(inode)->lli_it = NULL;                                      \
170         CDEBUG(D_DENTRY, "dettach intent %p from inode %p(ino %llu)\n",        \
171                         it, inode, (long long)llu_i2stat(inode)->st_ino);      \
172 } while(0)
173
174 /* interpet return codes from intent lookup */
175 #define LL_LOOKUP_POSITIVE 1
176 #define LL_LOOKUP_NEGATIVE 2
177
178 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
179 {
180         LASSERT(inode != NULL);
181         return &llu_i2info(inode)->lli_fid;
182 }
183
184 struct it_cb_data {
185         struct inode *icbd_parent;
186         struct pnode *icbd_child;
187         obd_id hash;
188 };
189
190 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
191
192 typedef int (*intent_finish_cb)(struct ptlrpc_request *,
193                                 struct inode *parent, struct pnode *pnode,
194                                 struct lookup_intent *, int offset, obd_id ino);
195 int llu_intent_lock(struct inode *parent, struct pnode *pnode,
196                     struct lookup_intent *, int flags, intent_finish_cb);
197
198 static inline __u64 ll_file_maxbytes(struct inode *inode)
199 {
200         return llu_i2info(inode)->lli_maxbytes;
201 }
202
203 struct mount_option_s
204 {
205         char *md_uuid;
206         char *dt_uuid;
207 };
208
209 #define IS_BAD_PTR(ptr)         \
210         ((unsigned long)(ptr) == 0 || (unsigned long)(ptr) > -1000UL)
211
212 /* llite_lib.c */
213 int liblustre_process_log(struct config_llog_instance *cfg, char *mgsnid,
214                           char *profile, int allow_recov);
215 int ll_parse_mount_target(const char *target, char **mgsnid,
216                           char **fsname);
217
218 extern struct mount_option_s mount_option;
219
220 /* super.c */
221 void llu_update_inode(struct inode *inode, struct mdt_body *body,
222                       struct lov_stripe_md *lmm);
223 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid);
224 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid);
225 int ll_it_open_error(int phase, struct lookup_intent *it);
226 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md);
227 int llu_inode_getattr(struct inode *inode, struct obdo *obdo);
228 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
229                    struct md_open_data **mod);
230 int llu_setattr_raw(struct inode *inode, struct iattr *attr);
231
232 extern struct fssw_ops llu_fssw_ops;
233
234 /* file.c */
235 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
236                          struct inode *i2, const char *name, int namelen,
237                          int mode, __u32 opc);
238 void llu_finish_md_op_data(struct md_op_data *op_data);
239 int llu_create(struct inode *dir, struct pnode_base *pnode, int mode);
240 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it);
241 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode);
242 int llu_md_close(struct obd_export *md_exp, struct inode *inode);
243 int llu_file_release(struct inode *inode);
244 int llu_sizeonmds_update(struct inode *inode, struct md_open_data *mod,
245                          struct lustre_handle *fh, __u64 ioepoch);
246 int llu_iop_close(struct inode *inode);
247 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off);
248 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag obd_flags);
249 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid);
250 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir);
251
252 /* rw.c */
253 int llu_iop_read(struct inode *ino, struct ioctx *ioctxp);
254 int llu_iop_write(struct inode *ino, struct ioctx *ioctxp);
255 int llu_iop_iodone(struct ioctx *ioctxp);
256 int llu_local_size(struct inode *inode);
257 int llu_glimpse_size(struct inode *inode);
258 int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
259                               struct ldlm_lock_desc *new, void *data,
260                               int flag);
261 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
262                     struct lov_stripe_md *lsm, int mode,
263                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
264                     int ast_flags);
265 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
266                       struct lov_stripe_md *lsm, int mode,
267                       struct lustre_handle *lockh);
268
269 /* namei.c */
270 int llu_iop_lookup(struct pnode *pnode,
271                    struct inode **inop,
272                    struct intent *intnt,
273                    const char *path);
274 void unhook_stale_inode(struct pnode *pno);
275 struct inode *llu_inode_from_lock(struct ldlm_lock *lock);
276 int llu_md_blocking_ast(struct ldlm_lock *lock,
277                         struct ldlm_lock_desc *desc,
278                         void *data, int flag);
279
280 /* dir.c */
281 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep, 
282                                char *buf, size_t nbytes);
283
284 /* liblustre/llite_fid.c*/
285 unsigned long llu_fid_build_ino(struct llu_sb_info *sbi, 
286                                 struct lu_fid *fid);
287
288 /* ext2 related */
289 #define EXT2_NAME_LEN (255)
290
291 struct ext2_dirent {
292         __u32   inode;
293         __u16   rec_len;
294         __u8    name_len;
295         __u8    file_type;
296         char    name[EXT2_NAME_LEN];
297 };
298
299 #define EXT2_DIR_PAD                    4
300 #define EXT2_DIR_ROUND                  (EXT2_DIR_PAD - 1)
301 #define EXT2_DIR_REC_LEN(name_len)      (((name_len) + 8 + EXT2_DIR_ROUND) & \
302                                          ~EXT2_DIR_ROUND)
303
304 static inline struct ext2_dirent *ext2_next_entry(struct ext2_dirent *p)
305 {
306         return (struct ext2_dirent*)((char*) p + le16_to_cpu(p->rec_len));
307 }
308
309 static inline void inode_init_lvb(struct inode *inode, struct ost_lvb *lvb)
310 {
311         struct intnl_stat *st = llu_i2stat(inode);
312         lvb->lvb_size = st->st_size;
313         lvb->lvb_blocks = st->st_blocks;
314         lvb->lvb_mtime = st->st_mtime;
315         lvb->lvb_atime = st->st_atime;
316         lvb->lvb_ctime = st->st_ctime;
317 }
318
319 #endif