Whamcloud - gitweb
LU-5162 mdc: Add exception entry check for radix_tree
[fs/lustre-release.git] / lustre / liblustre / llite_lib.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
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 <fcntl.h>
41 #include <limits.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <sysio.h>
48 #ifdef HAVE_XTIO_H
49 # include <xtio.h>
50 #endif
51 #include <fs.h>
52 #include <mount.h>
53 #include <inode.h>
54 #ifdef HAVE_FILE_H
55 # include <file.h>
56 #endif
57 #include <libcfs/libcfs.h>
58 #include <lustre/lustre_idl.h>
59 #include <lclient.h>
60 #include <lustre_lib.h>
61 #include <lustre_lite.h>
62
63 enum cl_req_type;
64
65 /* This should not be "optimized" use ~0ULL because page->index is a long and
66  * 32-bit systems are therefore limited to 16TB in a mapping */
67 #define MAX_LFS_FILESIZE ((__u64)(~0UL) << PAGE_CACHE_SHIFT)
68 struct ll_file_data {
69         struct obd_client_handle fd_mds_och;
70         __u32 fd_flags;
71         struct ccc_grouplock fd_grouplock;
72 };
73
74 struct llu_sb_info {
75         struct obd_uuid          ll_sb_uuid;
76         struct obd_export       *ll_md_exp;
77         struct obd_export       *ll_dt_exp;
78         struct lu_fid            ll_root_fid;
79         struct lustre_client_ocd ll_lco;
80         cfs_list_t               ll_conn_chain;
81         struct lu_site          *ll_site;
82         struct cl_device        *ll_cl;
83 };
84
85 enum lli_flags {
86         /* MDS has an authority for the Size-on-MDS attributes. */
87         LLIF_MDS_SIZE_LOCK      = (1 << 0),
88 };
89
90 struct llu_inode_info {
91         struct llu_sb_info     *lli_sbi;
92         struct lu_fid           lli_fid;
93
94         char                   *lli_symlink_name;
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         /* checking lli_has_smd is reliable only inside an IO
113          * i.e, lov stripe has been held. */
114         bool                    lli_has_smd;
115         int                     lli_open_flags;
116         int                     lli_open_count;
117
118         /* not for stat, change it later */
119         int                     lli_st_flags;
120         unsigned long           lli_st_generation;
121         struct cl_object       *lli_clob;
122         /* the most recent timestamps obtained from mds */
123         struct ost_lvb          lli_lvb;
124 };
125
126 static inline struct llu_sb_info *llu_fs2sbi(struct filesys *fs)
127 {
128         return (struct llu_sb_info*)(fs->fs_private);
129 }
130
131 static inline struct llu_inode_info *llu_i2info(struct inode *inode)
132 {
133         return (struct llu_inode_info*)(inode->i_private);
134 }
135
136 static inline int ll_inode_flags(struct inode *inode)
137 {
138         return llu_i2info(inode)->lli_st_flags;
139 }
140
141 static inline struct intnl_stat *llu_i2stat(struct inode *inode)
142 {
143         return &inode->i_stbuf;
144 }
145
146 #define ll_inode_blksize(inode)     (llu_i2stat(inode)->st_blksize)
147
148 static inline struct llu_sb_info *llu_i2sbi(struct inode *inode)
149 {
150         return llu_i2info(inode)->lli_sbi;
151 }
152
153 static inline struct obd_export *llu_i2obdexp(struct inode *inode)
154 {
155         return llu_i2info(inode)->lli_sbi->ll_dt_exp;
156 }
157
158 static inline struct obd_export *llu_i2mdexp(struct inode *inode)
159 {
160         return llu_i2info(inode)->lli_sbi->ll_md_exp;
161 }
162
163 static inline int llu_is_root_inode(struct inode *inode)
164 {
165         return (fid_seq(&llu_i2info(inode)->lli_fid) ==
166                 fid_seq(&llu_i2info(inode)->lli_sbi->ll_root_fid) &&
167                 fid_oid(&llu_i2info(inode)->lli_fid) ==
168                 fid_oid(&llu_i2info(inode)->lli_sbi->ll_root_fid));
169 }
170
171 #define LL_SAVE_INTENT(inode, it)                                              \
172 do {                                                                           \
173         struct lookup_intent *temp;                                            \
174         LASSERT(llu_i2info(inode)->lli_it == NULL);                            \
175         OBD_ALLOC(temp, sizeof(*temp));                                        \
176         memcpy(temp, it, sizeof(*temp));                                       \
177         llu_i2info(inode)->lli_it = temp;                                      \
178         CDEBUG(D_DENTRY, "alloc intent %p to inode %p(ino %llu)\n",            \
179                         temp, inode, (long long)llu_i2stat(inode)->st_ino);    \
180 } while(0)
181
182
183 #define LL_GET_INTENT(inode, it)                                               \
184 do {                                                                           \
185         it = llu_i2info(inode)->lli_it;                                        \
186                                                                                \
187         LASSERT(it);                                                           \
188         llu_i2info(inode)->lli_it = NULL;                                      \
189         CDEBUG(D_DENTRY, "dettach intent %p from inode %p(ino %llu)\n",        \
190                         it, inode, (long long)llu_i2stat(inode)->st_ino);      \
191 } while(0)
192
193 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
194 {
195         LASSERT(inode != NULL);
196         return &llu_i2info(inode)->lli_fid;
197 }
198
199 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
200
201 static inline __u64 ll_file_maxbytes(struct inode *inode)
202 {
203         return llu_i2info(inode)->lli_maxbytes;
204 }
205
206 #define IS_BAD_PTR(ptr)         \
207         ((unsigned long)(ptr) == 0 || (unsigned long)(ptr) > -1000UL)
208
209 /* llite_lib.c */
210 int liblustre_process_log(struct config_llog_instance *cfg, char *mgsnid,
211                           char *profile, int allow_recov);
212 int ll_parse_mount_target(const char *target, char **mgsnid,
213                           char **fsname);
214 extern char *lustre_path;
215
216 /* super.c */
217 void llu_update_inode(struct inode *inode, struct lustre_md *md);
218 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md);
219 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
220                       __u64 ioepoch, int sync);
221 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
222                    struct md_open_data **mod);
223 int llu_setattr_raw(struct inode *inode, struct iattr *attr);
224 int llu_put_grouplock(struct inode *inode, unsigned long arg);
225
226 extern struct fssw_ops llu_fssw_ops;
227
228 /* file.c */
229 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
230                          struct inode *i2, const char *name, int namelen,
231                          int mode, __u32 opc);
232 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it);
233 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode);
234 void llu_done_writing_attr(struct inode *inode, struct md_op_data *op_data);
235 int llu_md_close(struct obd_export *md_exp, struct inode *inode);
236 void llu_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
237                            struct lustre_handle *fh);
238 int llu_file_release(struct inode *inode);
239 int llu_som_update(struct inode *inode, struct md_op_data *op_data);
240 int llu_iop_close(struct inode *inode);
241 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off);
242 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid);
243 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir);
244 void llu_ioepoch_open(struct llu_inode_info *lli, __u64 ioepoch);
245
246 /* rw.c */
247 int llu_iop_read(struct inode *ino, struct ioctx *ioctxp);
248 int llu_iop_write(struct inode *ino, struct ioctx *ioctxp);
249 int llu_iop_iodone(struct ioctx *ioctxp);
250
251 /* namei.c */
252 int llu_iop_lookup(struct pnode *pnode,
253                    struct inode **inop,
254                    struct intent *intnt,
255                    const char *path);
256 struct inode *llu_inode_from_resource_lock(struct ldlm_lock *lock);
257 int llu_md_blocking_ast(struct ldlm_lock *lock,
258                         struct ldlm_lock_desc *desc,
259                         void *data, int flag);
260
261 /* dir.c */
262 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep,
263                                char *buf, size_t nbytes);
264
265 /* ext2 related */
266 #define EXT2_NAME_LEN (255)
267
268 struct ext2_dirent {
269         __u32   inode;
270         __u16   rec_len;
271         __u8    name_len;
272         __u8    file_type;
273         char    name[EXT2_NAME_LEN];
274 };
275
276 static inline struct ext2_dirent *ext2_next_entry(struct ext2_dirent *p)
277 {
278         return (struct ext2_dirent*)((char*) p + le16_to_cpu(p->rec_len));
279 }
280
281 int llu_merge_lvb(const struct lu_env *env, struct inode *inode);
282
283 static inline void inode_init_lvb(struct inode *inode, struct ost_lvb *lvb)
284 {
285         struct intnl_stat *st = llu_i2stat(inode);
286         lvb->lvb_size = st->st_size;
287         lvb->lvb_blocks = st->st_blocks;
288         lvb->lvb_mtime = st->st_mtime;
289         lvb->lvb_atime = st->st_atime;
290         lvb->lvb_ctime = st->st_ctime;
291 }
292
293 struct llu_io_session {
294         struct inode           *lis_inode;
295         int                     lis_cmd;
296         int                     lis_rc;
297         __u64                   lis_rwcount;
298 };
299
300 struct llu_io_group
301 {
302         int                     lig_rc;
303         __u64                   lig_rwcount;
304 };
305
306 struct llu_io_session;
307 void put_io_group(struct llu_io_group *group);
308
309 int cl_sb_init(struct llu_sb_info *sbi);
310 int cl_sb_fini(struct llu_sb_info *sbi);
311
312 void llu_io_init(struct cl_io *io, struct inode *inode, int write);
313
314 struct slp_io {
315         struct llu_io_session *sio_session;
316 };
317
318 struct slp_session {
319         struct slp_io ss_ios;
320 };
321
322 int slp_global_init(void);
323 void slp_global_fini(void);
324
325 static inline struct slp_session *slp_env_session(const struct lu_env *env)
326 {
327         extern struct lu_context_key slp_session_key;
328         struct slp_session *ses;
329         ses = lu_context_key_get(env->le_ses, &slp_session_key);
330         LASSERT(ses != NULL);
331         return ses;
332 }
333 static inline struct slp_io *slp_env_io(const struct lu_env *env)
334 {
335         return &slp_env_session(env)->ss_ios;
336 }
337
338 /* lclient compat stuff */
339 #define cl_inode_info llu_inode_info
340 #define cl_i2info(info) llu_i2info(info)
341 #define cl_inode_mode(inode) (llu_i2stat(inode)->st_mode)
342 #define cl_i2sbi llu_i2sbi
343 #define cl_isize_read(inode)             (llu_i2stat(inode)->st_size)
344 #define cl_isize_write(inode,kms)        do{llu_i2stat(inode)->st_size = kms;}while(0)
345 #define cl_isize_write_nolock(inode,kms) cl_isize_write(inode,kms)
346
347 static inline struct ll_file_data *cl_iattr2fd(struct inode *inode,
348                                                const struct iattr *attr)
349 {
350         return llu_i2info(inode)->lli_file_data;
351 }
352
353 static inline void cl_isize_lock(struct inode *inode)
354 {
355 }
356
357 static inline void cl_isize_unlock(struct inode *inode)
358 {
359 }
360
361 static inline int cl_merge_lvb(const struct lu_env *env, struct inode *inode)
362 {
363         return llu_merge_lvb(env, inode);
364 }
365
366 #define cl_inode_atime(inode) (llu_i2stat(inode)->st_atime)
367 #define cl_inode_ctime(inode) (llu_i2stat(inode)->st_ctime)
368 #define cl_inode_mtime(inode) (llu_i2stat(inode)->st_mtime)
369
370 static inline struct obd_capa *cl_capa_lookup(struct inode *inode,
371                                               enum cl_req_type crt)
372 {
373         return NULL;
374 }
375
376 static inline void cl_stats_tally(struct cl_device *dev, enum cl_req_type crt,
377                                   int rc)
378 {
379 }
380
381 static inline loff_t i_size_read(struct inode *inode)
382 {
383         return inode->i_stbuf.st_size;
384 }
385
386 static inline __u64 hash_x_index(__u64 hash, int hash64)
387 {
388         if (BITS_PER_LONG == 32 && hash64)
389                 hash >>= 32;
390         /* save hash 0 with hash 1 */
391         return ~0ULL - (hash + !hash);
392 }
393 #endif