Whamcloud - gitweb
9062bf93e3c5409e3b2cabc5ff38a20dba30434a
[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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #ifndef __LLU_H_
40 #define __LLU_H_
41 #include <fcntl.h>
42 #include <sys/queue.h>
43 #include <sysio.h>
44 #ifdef HAVE_XTIO_H
45 #include <xtio.h>
46 #endif
47 #include <fs.h>
48 #include <mount.h>
49 #include <inode.h>
50 #ifdef HAVE_FILE_H
51 #include <file.h>
52 #endif
53
54 #include <liblustre.h>
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <lustre_mdc.h>
58 #include <lustre_lite.h>
59 #include <lustre_ver.h>
60
61 #include <sys/types.h>
62 #include <sys/stat.h>
63
64 /* for struct cl_lock_descr and struct cl_io */
65 #include <cl_object.h>
66 #include <lclient.h>
67
68 /* This should not be "optimized" use ~0ULL because page->index is a long and
69  * 32-bit systems are therefore limited to 16TB in a mapping */
70 #define PAGE_CACHE_MAXBYTES ((__u64)(~0UL) << CFS_PAGE_SHIFT)
71 struct ll_file_data {
72         struct obd_client_handle fd_mds_och;
73         __u32 fd_flags;
74         struct ccc_grouplock fd_grouplock;
75 };
76
77 struct llu_sb_info {
78         struct obd_uuid          ll_sb_uuid;
79         struct obd_export       *ll_md_exp;
80         struct obd_export       *ll_dt_exp;
81         struct lu_fid            ll_root_fid;
82         int                      ll_flags;
83         struct lustre_client_ocd ll_lco;
84         cfs_list_t               ll_conn_chain;
85
86         struct obd_uuid          ll_mds_uuid;
87         struct obd_uuid          ll_mds_peer_uuid;
88         char                    *ll_instance;
89         struct lu_site           *ll_site;
90         struct cl_device         *ll_cl;
91 };
92
93 #define LL_SBI_NOLCK            0x1
94
95 enum lli_flags {
96         /* MDS has an authority for the Size-on-MDS attributes. */
97         LLIF_MDS_SIZE_LOCK      = (1 << 0),
98 };
99
100 struct llu_inode_info {
101         struct llu_sb_info     *lli_sbi;
102         struct lu_fid           lli_fid;
103
104         struct lov_stripe_md   *lli_smd;
105         char                   *lli_symlink_name;
106         cfs_semaphore_t         lli_open_sem;
107         __u64                   lli_maxbytes;
108         unsigned long           lli_flags;
109         __u64                   lli_ioepoch;
110
111         /* for libsysio */
112         struct file_identifier  lli_sysio_fid;
113
114         struct lookup_intent   *lli_it;
115
116         /* XXX workaround for libsysio readdir */
117         loff_t                  lli_dir_pos;
118
119         /* in libsysio we have no chance to store data in file,
120          * so place it here. since it's possible that an file
121          * was opened several times without close, we track an
122          * open_count here */
123         struct ll_file_data    *lli_file_data;
124         int                     lli_open_flags;
125         int                     lli_open_count;
126
127         /* not for stat, change it later */
128         int                     lli_st_flags;
129         unsigned long           lli_st_generation;
130         struct cl_object       *lli_clob;
131         /* the most recent timestamps obtained from mds */
132         struct ost_lvb          lli_lvb;
133 };
134
135 static inline struct llu_sb_info *llu_fs2sbi(struct filesys *fs)
136 {
137         return (struct llu_sb_info*)(fs->fs_private);
138 }
139
140 static inline struct llu_inode_info *llu_i2info(struct inode *inode)
141 {
142         return (struct llu_inode_info*)(inode->i_private);
143 }
144
145 static inline int ll_inode_flags(struct inode *inode)
146 {
147         return llu_i2info(inode)->lli_st_flags;
148 }
149
150 static inline struct intnl_stat *llu_i2stat(struct inode *inode)
151 {
152         return &inode->i_stbuf;
153 }
154
155 #define ll_inode_blksize(inode)     (llu_i2stat(inode)->st_blksize)
156
157 static inline struct llu_sb_info *llu_i2sbi(struct inode *inode)
158 {
159         return llu_i2info(inode)->lli_sbi;
160 }
161
162 static inline struct obd_export *llu_i2obdexp(struct inode *inode)
163 {
164         return llu_i2info(inode)->lli_sbi->ll_dt_exp;
165 }
166
167 static inline struct obd_export *llu_i2mdexp(struct inode *inode)
168 {
169         return llu_i2info(inode)->lli_sbi->ll_md_exp;
170 }
171
172 static inline int llu_is_root_inode(struct inode *inode)
173 {
174         return (fid_seq(&llu_i2info(inode)->lli_fid) ==
175                 fid_seq(&llu_i2info(inode)->lli_sbi->ll_root_fid) &&
176                 fid_oid(&llu_i2info(inode)->lli_fid) ==
177                 fid_oid(&llu_i2info(inode)->lli_sbi->ll_root_fid));
178 }
179
180 #define LL_SAVE_INTENT(inode, it)                                              \
181 do {                                                                           \
182         struct lookup_intent *temp;                                            \
183         LASSERT(llu_i2info(inode)->lli_it == NULL);                            \
184         OBD_ALLOC(temp, sizeof(*temp));                                        \
185         memcpy(temp, it, sizeof(*temp));                                       \
186         llu_i2info(inode)->lli_it = temp;                                      \
187         CDEBUG(D_DENTRY, "alloc intent %p to inode %p(ino %llu)\n",            \
188                         temp, inode, (long long)llu_i2stat(inode)->st_ino);    \
189 } while(0)
190
191
192 #define LL_GET_INTENT(inode, it)                                               \
193 do {                                                                           \
194         it = llu_i2info(inode)->lli_it;                                        \
195                                                                                \
196         LASSERT(it);                                                           \
197         llu_i2info(inode)->lli_it = NULL;                                      \
198         CDEBUG(D_DENTRY, "dettach intent %p from inode %p(ino %llu)\n",        \
199                         it, inode, (long long)llu_i2stat(inode)->st_ino);      \
200 } while(0)
201
202 /* interpet return codes from intent lookup */
203 #define LL_LOOKUP_POSITIVE 1
204 #define LL_LOOKUP_NEGATIVE 2
205
206 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
207 {
208         LASSERT(inode != NULL);
209         return &llu_i2info(inode)->lli_fid;
210 }
211
212 struct it_cb_data {
213         struct inode *icbd_parent;
214         struct pnode *icbd_child;
215         obd_id hash;
216 };
217
218 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
219
220 typedef int (*intent_finish_cb)(struct ptlrpc_request *,
221                                 struct inode *parent, struct pnode *pnode,
222                                 struct lookup_intent *, int offset, obd_id ino);
223 int llu_intent_lock(struct inode *parent, struct pnode *pnode,
224                     struct lookup_intent *, int flags, intent_finish_cb);
225
226 static inline __u64 ll_file_maxbytes(struct inode *inode)
227 {
228         return llu_i2info(inode)->lli_maxbytes;
229 }
230
231 struct mount_option_s
232 {
233         char *md_uuid;
234         char *dt_uuid;
235 };
236
237 #define IS_BAD_PTR(ptr)         \
238         ((unsigned long)(ptr) == 0 || (unsigned long)(ptr) > -1000UL)
239
240 /* llite_lib.c */
241 int liblustre_process_log(struct config_llog_instance *cfg, char *mgsnid,
242                           char *profile, int allow_recov);
243 int ll_parse_mount_target(const char *target, char **mgsnid,
244                           char **fsname);
245
246 extern struct mount_option_s mount_option;
247
248 /* super.c */
249 void llu_update_inode(struct inode *inode, struct lustre_md *md);
250 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid);
251 int ll_it_open_error(int phase, struct lookup_intent *it);
252 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md);
253 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
254                       __u64 ioepoch, int sync);
255 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
256                    struct md_open_data **mod);
257 int llu_setattr_raw(struct inode *inode, struct iattr *attr);
258 int llu_put_grouplock(struct inode *inode, unsigned long arg);
259
260 extern struct fssw_ops llu_fssw_ops;
261
262 /* file.c */
263 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
264                          struct inode *i2, const char *name, int namelen,
265                          int mode, __u32 opc);
266 int llu_create(struct inode *dir, struct pnode_base *pnode, int mode);
267 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it);
268 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode);
269 void llu_done_writing_attr(struct inode *inode, struct md_op_data *op_data);
270 int llu_md_close(struct obd_export *md_exp, struct inode *inode);
271 void llu_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
272                            struct lustre_handle *fh);
273 int llu_file_release(struct inode *inode);
274 int llu_som_update(struct inode *inode, struct md_op_data *op_data);
275 int llu_iop_close(struct inode *inode);
276 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off);
277 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag obd_flags);
278 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid);
279 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir);
280 void llu_ioepoch_open(struct llu_inode_info *lli, __u64 ioepoch);
281
282 /* rw.c */
283 int llu_iop_read(struct inode *ino, struct ioctx *ioctxp);
284 int llu_iop_write(struct inode *ino, struct ioctx *ioctxp);
285 int llu_iop_iodone(struct ioctx *ioctxp);
286 int llu_glimpse_size(struct inode *inode);
287 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
288                     struct lov_stripe_md *lsm, int mode,
289                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
290                     int ast_flags);
291 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
292                       struct lov_stripe_md *lsm, int mode,
293                       struct lustre_handle *lockh);
294
295 /* namei.c */
296 int llu_iop_lookup(struct pnode *pnode,
297                    struct inode **inop,
298                    struct intent *intnt,
299                    const char *path);
300 void unhook_stale_inode(struct pnode *pno);
301 struct inode *llu_inode_from_lock(struct ldlm_lock *lock);
302 int llu_md_blocking_ast(struct ldlm_lock *lock,
303                         struct ldlm_lock_desc *desc,
304                         void *data, int flag);
305
306 /* dir.c */
307 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep,
308                                char *buf, size_t nbytes);
309
310 /* liblustre/llite_fid.c*/
311 unsigned long llu_fid_build_ino(struct llu_sb_info *sbi,
312                                 struct lu_fid *fid);
313
314 /* ext2 related */
315 #define EXT2_NAME_LEN (255)
316
317 struct ext2_dirent {
318         __u32   inode;
319         __u16   rec_len;
320         __u8    name_len;
321         __u8    file_type;
322         char    name[EXT2_NAME_LEN];
323 };
324
325 #define EXT2_DIR_PAD                    4
326 #define EXT2_DIR_ROUND                  (EXT2_DIR_PAD - 1)
327 #define EXT2_DIR_REC_LEN(name_len)      (((name_len) + 8 + EXT2_DIR_ROUND) & \
328                                          ~EXT2_DIR_ROUND)
329
330 static inline struct ext2_dirent *ext2_next_entry(struct ext2_dirent *p)
331 {
332         return (struct ext2_dirent*)((char*) p + le16_to_cpu(p->rec_len));
333 }
334
335 int llu_merge_lvb(struct inode *inode);
336
337 static inline void inode_init_lvb(struct inode *inode, struct ost_lvb *lvb)
338 {
339         struct intnl_stat *st = llu_i2stat(inode);
340         lvb->lvb_size = st->st_size;
341         lvb->lvb_blocks = st->st_blocks;
342         lvb->lvb_mtime = st->st_mtime;
343         lvb->lvb_atime = st->st_atime;
344         lvb->lvb_ctime = st->st_ctime;
345 }
346
347 #define LLU_IO_GROUP_SIZE(x) \
348         (sizeof(struct llu_io_group) + \
349          (sizeof(struct ll_async_page) + \
350           sizeof(cfs_page_t) + \
351           llap_cookie_size) * (x))
352
353 struct llu_io_session {
354         struct inode           *lis_inode;
355         int                     lis_cmd;
356         int                     lis_max_groups;
357         int                     lis_ngroups;
358         int                     lis_rc;
359         __u64                   lis_rwcount;
360 };
361
362 struct llu_io_group
363 {
364         struct lustre_rw_params *lig_params;
365         int                     lig_rc;
366         __u64                   lig_rwcount;
367 };
368
369 struct llu_io_session;
370 void put_io_group(struct llu_io_group *group);
371
372 int cl_sb_init(struct llu_sb_info *sbi);
373 int cl_sb_fini(struct llu_sb_info *sbi);
374
375 void llu_io_init(struct cl_io *io, struct inode *inode, int write);
376
377 struct slp_io {
378         struct llu_io_session *sio_session;
379 };
380
381 struct slp_session {
382         struct slp_io ss_ios;
383 };
384
385 static inline struct slp_session *slp_env_session(const struct lu_env *env)
386 {
387         extern struct lu_context_key slp_session_key;
388         struct slp_session *ses;
389         ses = lu_context_key_get(env->le_ses, &slp_session_key);
390         LASSERT(ses != NULL);
391         return ses;
392 }
393 static inline struct slp_io *slp_env_io(const struct lu_env *env)
394 {
395         return &slp_env_session(env)->ss_ios;
396 }
397
398 /* lclient compat stuff */
399 #define cl_inode_info llu_inode_info
400 #define cl_i2info(info) llu_i2info(info)
401 #define cl_inode_mode(inode) (llu_i2stat(inode)->st_mode)
402 #define cl_i2sbi llu_i2sbi
403 #define cl_isize_read(inode)             (llu_i2stat(inode)->st_size)
404 #define cl_isize_write(inode,kms)        do{llu_i2stat(inode)->st_size = kms;}while(0)
405 #define cl_isize_write_nolock(inode,kms) cl_isize_write(inode,kms)
406
407 static inline struct ll_file_data *cl_iattr2fd(struct inode *inode,
408                                                const struct iattr *attr)
409 {
410         return llu_i2info(inode)->lli_file_data;
411 }
412
413 static inline void cl_isize_lock(struct inode *inode, int lsmlock)
414 {
415 }
416
417 static inline void cl_isize_unlock(struct inode *inode, int lsmlock)
418 {
419 }
420
421 static inline int cl_merge_lvb(struct inode *inode)
422 {
423         return llu_merge_lvb(inode);
424 }
425
426 #define cl_inode_atime(inode) (llu_i2stat(inode)->st_atime)
427 #define cl_inode_ctime(inode) (llu_i2stat(inode)->st_ctime)
428 #define cl_inode_mtime(inode) (llu_i2stat(inode)->st_mtime)
429
430 static inline struct obd_capa *cl_capa_lookup(struct inode *inode,
431                                               enum cl_req_type crt)
432 {
433         return NULL;
434 }
435
436 static inline void cl_stats_tally(struct cl_device *dev, enum cl_req_type crt,
437                                   int rc)
438 {
439 }
440
441 static inline loff_t i_size_read(struct inode *inode)
442 {
443         return inode->i_stbuf.st_size;
444 }
445
446 static inline void i_size_write(struct inode *inode, loff_t i_sz)
447 {
448         inode->i_stbuf.st_size = i_sz;
449 }
450 #endif