Whamcloud - gitweb
LU-9679 llite: use lli_flags instead of lli_update_atime
[fs/lustre-release.git] / lustre / llite / llite_internal.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef LLITE_INTERNAL_H
34 #define LLITE_INTERNAL_H
35 #include <lustre_debug.h>
36 #include <lustre_disk.h>  /* for s2sbi */
37 #include <lustre_eacl.h>
38 #include <lustre_linkea.h>
39
40 /* for struct cl_lock_descr and struct cl_io */
41 #include <cl_object.h>
42 #include <lustre_lmv.h>
43 #include <lustre_mdc.h>
44 #include <lustre_intent.h>
45 #include <linux/compat.h>
46 #include <linux/aio.h>
47 #include <lustre_compat.h>
48
49 #include "vvp_internal.h"
50 #include "range_lock.h"
51 #include "pcc.h"
52
53 #ifndef FMODE_EXEC
54 #define FMODE_EXEC 0
55 #endif
56
57 #ifndef VM_FAULT_RETRY
58 #define VM_FAULT_RETRY 0
59 #endif
60
61 /* Kernel 3.1 kills LOOKUP_CONTINUE, LOOKUP_PARENT is equivalent to it.
62  * seem kernel commit 49084c3bb2055c401f3493c13edae14d49128ca0 */
63 #ifndef LOOKUP_CONTINUE
64 #define LOOKUP_CONTINUE LOOKUP_PARENT
65 #endif
66
67 /** Only used on client-side for indicating the tail of dir hash/offset. */
68 #define LL_DIR_END_OFF          0x7fffffffffffffffULL
69 #define LL_DIR_END_OFF_32BIT    0x7fffffffUL
70
71 /* 4UL * 1024 * 1024 */
72 #define LL_MAX_BLKSIZE_BITS 22
73
74 #define LL_IT2STR(it) ((it) ? ldlm_it2str((it)->it_op) : "0")
75 #define LUSTRE_FPRIVATE(file) ((file)->private_data)
76
77 struct ll_dentry_data {
78         struct lookup_intent            *lld_it;
79         unsigned int                    lld_sa_generation;
80         unsigned int                    lld_invalid:1;
81         unsigned int                    lld_nfs_dentry:1;
82         struct rcu_head                 lld_rcu_head;
83 };
84
85 #define ll_d2d(de) ((struct ll_dentry_data*)((de)->d_fsdata))
86
87 #define LLI_INODE_MAGIC                 0x111d0de5
88 #define LLI_INODE_DEAD                  0xdeadd00d
89
90 struct ll_getname_data {
91 #ifdef HAVE_DIR_CONTEXT
92         struct dir_context      ctx;
93 #endif
94         char            *lgd_name;      /* points to a buffer with NAME_MAX+1 size */
95         struct lu_fid   lgd_fid;        /* target fid we are looking for */
96         int             lgd_found;      /* inode matched? */
97 };
98
99 struct ll_grouplock {
100         struct lu_env   *lg_env;
101         struct cl_io    *lg_io;
102         struct cl_lock  *lg_lock;
103         unsigned long    lg_gid;
104 };
105
106 struct ll_inode_info {
107         __u32                           lli_inode_magic;
108         spinlock_t                      lli_lock;
109
110         volatile unsigned long          lli_flags;
111         struct posix_acl                *lli_posix_acl;
112
113         /* identifying fields for both metadata and data stacks. */
114         struct lu_fid                   lli_fid;
115         /* master inode fid for stripe directory */
116         struct lu_fid                   lli_pfid;
117
118         /* We need all three because every inode may be opened in different
119          * modes */
120         struct obd_client_handle       *lli_mds_read_och;
121         struct obd_client_handle       *lli_mds_write_och;
122         struct obd_client_handle       *lli_mds_exec_och;
123         __u64                           lli_open_fd_read_count;
124         __u64                           lli_open_fd_write_count;
125         __u64                           lli_open_fd_exec_count;
126         /* Protects access to och pointers and their usage counters */
127         struct mutex                    lli_och_mutex;
128
129         struct inode                    lli_vfs_inode;
130
131         /* the most recent timestamps obtained from mds */
132         s64                             lli_atime;
133         s64                             lli_mtime;
134         s64                             lli_ctime;
135         spinlock_t                      lli_agl_lock;
136
137         /* Try to make the d::member and f::member are aligned. Before using
138          * these members, make clear whether it is directory or not. */
139         union {
140                 /* for directory */
141                 struct {
142                         /* metadata statahead */
143                         /* since parent-child threads can share the same @file
144                          * struct, "opendir_key" is the token when dir close for
145                          * case of parent exit before child -- it is me should
146                          * cleanup the dir readahead. */
147                         void                           *lli_opendir_key;
148                         struct ll_statahead_info       *lli_sai;
149                         /* protect statahead stuff. */
150                         spinlock_t                      lli_sa_lock;
151                         /* "opendir_pid" is the token when lookup/revalid
152                          * -- I am the owner of dir statahead. */
153                         pid_t                           lli_opendir_pid;
154                         /* stat will try to access statahead entries or start
155                          * statahead if this flag is set, and this flag will be
156                          * set upon dir open, and cleared when dir is closed,
157                          * statahead hit ratio is too low, or start statahead
158                          * thread failed. */
159                         unsigned int                    lli_sa_enabled:1;
160                         /* generation for statahead */
161                         unsigned int                    lli_sa_generation;
162                         /* rw lock protects lli_lsm_md */
163                         struct rw_semaphore             lli_lsm_sem;
164                         /* directory stripe information */
165                         struct lmv_stripe_md            *lli_lsm_md;
166                         /* directory default LMV */
167                         struct lmv_stripe_md            *lli_default_lsm_md;
168                 };
169
170                 /* for non-directory */
171                 struct {
172                         struct mutex            lli_size_mutex;
173                         char                   *lli_symlink_name;
174                         /*
175                          * struct rw_semaphore {
176                          *    signed long       count;     // align d.d_def_acl
177                          *    spinlock_t        wait_lock; // align d.d_sa_lock
178                          *    struct list_head wait_list;
179                          * }
180                          */
181                         struct rw_semaphore     lli_trunc_sem;
182                         struct range_lock_tree  lli_write_tree;
183
184                         struct rw_semaphore     lli_glimpse_sem;
185                         ktime_t                 lli_glimpse_time;
186                         struct list_head        lli_agl_list;
187                         __u64                   lli_agl_index;
188
189                         /* for writepage() only to communicate to fsync */
190                         int                     lli_async_rc;
191
192                         /* protect the file heat fields */
193                         spinlock_t                      lli_heat_lock;
194                         __u32                           lli_heat_flags;
195                         struct obd_heat_instance        lli_heat_instances[OBD_HEAT_COUNT];
196
197                         /*
198                          * Whenever a process try to read/write the file, the
199                          * jobid of the process will be saved here, and it'll
200                          * be packed into the write PRC when flush later.
201                          *
202                          * So the read/write statistics for jobid will not be
203                          * accurate if the file is shared by different jobs.
204                          */
205                         char                    lli_jobid[LUSTRE_JOBID_SIZE];
206
207                         struct mutex             lli_pcc_lock;
208                         enum lu_pcc_state_flags  lli_pcc_state;
209                         /*
210                          * @lli_pcc_generation saves the gobal PCC generation
211                          * when the file was successfully attached into PCC.
212                          * The flags of the PCC dataset are saved in
213                          * @lli_pcc_dsflags.
214                          * The gobal PCC generation will be increased when add
215                          * or delete a PCC backend, or change the configuration
216                          * parameters for PCC.
217                          * If @lli_pcc_generation is same as the gobal PCC
218                          * generation, we can use the saved flags of the PCC
219                          * dataset to determine whether need to try auto attach
220                          * safely.
221                          */
222                         __u64                    lli_pcc_generation;
223                         enum pcc_dataset_flags   lli_pcc_dsflags;
224                         struct pcc_inode        *lli_pcc_inode;
225
226                         struct mutex             lli_group_mutex;
227                         __u64                    lli_group_users;
228                         unsigned long            lli_group_gid;
229                 };
230         };
231
232         /* XXX: For following frequent used members, although they maybe special
233          *      used for non-directory object, it is some time-wasting to check
234          *      whether the object is directory or not before using them. On the
235          *      other hand, currently, sizeof(f) > sizeof(d), it cannot reduce
236          *      the "ll_inode_info" size even if moving those members into u.f.
237          *      So keep them out side.
238          *
239          *      In the future, if more members are added only for directory,
240          *      some of the following members can be moved into u.f.
241          */
242         struct cl_object                *lli_clob;
243
244         /* mutex to request for layout lock exclusively. */
245         struct mutex                    lli_layout_mutex;
246         /* Layout version, protected by lli_layout_lock */
247         __u32                           lli_layout_gen;
248         spinlock_t                      lli_layout_lock;
249
250         __u32                           lli_projid;   /* project id */
251
252         struct rw_semaphore             lli_xattrs_list_rwsem;
253         struct mutex                    lli_xattrs_enq_lock;
254         struct list_head                lli_xattrs; /* ll_xattr_entry->xe_list */
255 };
256
257 static inline __u32 ll_layout_version_get(struct ll_inode_info *lli)
258 {
259         __u32 gen;
260
261         spin_lock(&lli->lli_layout_lock);
262         gen = lli->lli_layout_gen;
263         spin_unlock(&lli->lli_layout_lock);
264
265         return gen;
266 }
267
268 static inline void ll_layout_version_set(struct ll_inode_info *lli, __u32 gen)
269 {
270         spin_lock(&lli->lli_layout_lock);
271         lli->lli_layout_gen = gen;
272         spin_unlock(&lli->lli_layout_lock);
273 }
274
275 enum ll_file_flags {
276         /* File data is modified. */
277         LLIF_DATA_MODIFIED      = 0,
278         /* File is being restored */
279         LLIF_FILE_RESTORING     = 1,
280         /* Xattr cache is attached to the file */
281         LLIF_XATTR_CACHE        = 2,
282         /* Project inherit */
283         LLIF_PROJECT_INHERIT    = 3,
284         /* update atime from MDS even if it's older than local inode atime. */
285         LLIF_UPDATE_ATIME       = 4,
286
287 };
288
289 static inline void ll_file_set_flag(struct ll_inode_info *lli,
290                                     enum ll_file_flags flag)
291 {
292         set_bit(flag, &lli->lli_flags);
293 }
294
295 static inline void ll_file_clear_flag(struct ll_inode_info *lli,
296                                       enum ll_file_flags flag)
297 {
298         clear_bit(flag, &lli->lli_flags);
299 }
300
301 static inline bool ll_file_test_flag(struct ll_inode_info *lli,
302                                      enum ll_file_flags flag)
303 {
304         return test_bit(flag, &lli->lli_flags);
305 }
306
307 static inline bool ll_file_test_and_clear_flag(struct ll_inode_info *lli,
308                                                enum ll_file_flags flag)
309 {
310         return test_and_clear_bit(flag, &lli->lli_flags);
311 }
312
313 int ll_xattr_cache_destroy(struct inode *inode);
314
315 int ll_xattr_cache_get(struct inode *inode,
316                         const char *name,
317                         char *buffer,
318                         size_t size,
319                         __u64 valid);
320
321 static inline bool obd_connect_has_secctx(struct obd_connect_data *data)
322 {
323 #if defined(HAVE_SECURITY_DENTRY_INIT_SECURITY) && defined(CONFIG_SECURITY)
324         return data->ocd_connect_flags & OBD_CONNECT_FLAGS2 &&
325                data->ocd_connect_flags2 & OBD_CONNECT2_FILE_SECCTX;
326 #else
327         return false;
328 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
329 }
330
331 static inline void obd_connect_set_secctx(struct obd_connect_data *data)
332 {
333 #if defined(HAVE_SECURITY_DENTRY_INIT_SECURITY) && defined(CONFIG_SECURITY)
334         data->ocd_connect_flags2 |= OBD_CONNECT2_FILE_SECCTX;
335 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
336 }
337
338 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
339                             const char **secctx_name, void **secctx,
340                             __u32 *secctx_size);
341 int ll_inode_init_security(struct dentry *dentry, struct inode *inode,
342                            struct inode *dir);
343
344 int ll_listsecurity(struct inode *inode, char *secctx_name,
345                     size_t secctx_name_size);
346
347 /*
348  * Locking to guarantee consistency of non-atomic updates to long long i_size,
349  * consistency between file size and KMS.
350  *
351  * Implemented by ->lli_size_mutex and ->lsm_lock, nested in that order.
352  */
353
354 void ll_inode_size_lock(struct inode *inode);
355 void ll_inode_size_unlock(struct inode *inode);
356
357 static inline struct ll_inode_info *ll_i2info(struct inode *inode)
358 {
359         return container_of(inode, struct ll_inode_info, lli_vfs_inode);
360 }
361
362 static inline struct pcc_inode *ll_i2pcci(struct inode *inode)
363 {
364         return ll_i2info(inode)->lli_pcc_inode;
365 }
366
367 /* default to use at least 16M for fast read if possible */
368 #define RA_REMAIN_WINDOW_MIN                    MiB_TO_PAGES(16UL)
369
370 /* default to about 64M of readahead on a given system. */
371 #define SBI_DEFAULT_READAHEAD_MAX               MiB_TO_PAGES(64UL)
372
373 /* default to read-ahead full files smaller than 2MB on the second read */
374 #define SBI_DEFAULT_READAHEAD_WHOLE_MAX         MiB_TO_PAGES(2UL)
375
376 enum ra_stat {
377         RA_STAT_HIT = 0,
378         RA_STAT_MISS,
379         RA_STAT_DISTANT_READPAGE,
380         RA_STAT_MISS_IN_WINDOW,
381         RA_STAT_FAILED_GRAB_PAGE,
382         RA_STAT_FAILED_MATCH,
383         RA_STAT_DISCARDED,
384         RA_STAT_ZERO_LEN,
385         RA_STAT_ZERO_WINDOW,
386         RA_STAT_EOF,
387         RA_STAT_MAX_IN_FLIGHT,
388         RA_STAT_WRONG_GRAB_PAGE,
389         RA_STAT_FAILED_REACH_END,
390         RA_STAT_ASYNC,
391         RA_STAT_FAILED_FAST_READ,
392         _NR_RA_STAT,
393 };
394
395 struct ll_ra_info {
396         atomic_t        ra_cur_pages;
397         unsigned long   ra_max_pages;
398         unsigned long   ra_max_pages_per_file;
399         unsigned long   ra_max_read_ahead_whole_pages;
400         struct workqueue_struct  *ll_readahead_wq;
401         /*
402          * Max number of active works for readahead workqueue,
403          * default is 0 which make workqueue init number itself,
404          * unless there is a specific need for throttling the
405          * number of active work items, specifying '0' is recommended.
406          */
407         unsigned int ra_async_max_active;
408         /* Threshold to control when to trigger async readahead */
409         unsigned long ra_async_pages_per_file_threshold;
410 };
411
412 /* ra_io_arg will be filled in the beginning of ll_readahead with
413  * ras_lock, then the following ll_read_ahead_pages will read RA
414  * pages according to this arg, all the items in this structure are
415  * counted by page index.
416  */
417 struct ra_io_arg {
418         pgoff_t ria_start; /* start offset of read-ahead*/
419         pgoff_t ria_end; /* end offset of read-ahead*/
420         unsigned long ria_reserved; /* reserved pages for read-ahead */
421         pgoff_t ria_end_min; /* minimum end to cover current read */
422         bool ria_eof; /* reach end of file */
423         /* If stride read pattern is detected, ria_stoff means where
424          * stride read is started. Note: for normal read-ahead, the
425          * value here is meaningless, and also it will not be accessed*/
426         unsigned long ria_stoff;
427         /* ria_length and ria_bytes are the length and pages length in the
428          * stride I/O mode. And they will also be used to check whether
429          * it is stride I/O read-ahead in the read-ahead pages*/
430         unsigned long ria_length;
431         unsigned long ria_bytes;
432 };
433
434 /* LL_HIST_MAX=32 causes an overflow */
435 #define LL_HIST_MAX 28
436 #define LL_HIST_START 12 /* buckets start at 2^12 = 4k */
437 #define LL_PROCESS_HIST_MAX 10
438 struct per_process_info {
439         pid_t pid;
440         struct obd_histogram pp_r_hist;
441         struct obd_histogram pp_w_hist;
442 };
443
444 /* pp_extents[LL_PROCESS_HIST_MAX] will hold the combined process info */
445 struct ll_rw_extents_info {
446         struct per_process_info pp_extents[LL_PROCESS_HIST_MAX + 1];
447 };
448
449 #define LL_OFFSET_HIST_MAX 100
450 struct ll_rw_process_info {
451         pid_t                     rw_pid;
452         int                       rw_op;
453         loff_t                    rw_range_start;
454         loff_t                    rw_range_end;
455         loff_t                    rw_last_file_pos;
456         loff_t                    rw_offset;
457         size_t                    rw_smallest_extent;
458         size_t                    rw_largest_extent;
459         struct ll_file_data      *rw_last_file;
460 };
461
462 enum stats_track_type {
463         STATS_TRACK_ALL = 0,  /* track all processes */
464         STATS_TRACK_PID,      /* track process with this pid */
465         STATS_TRACK_PPID,     /* track processes with this ppid */
466         STATS_TRACK_GID,      /* track processes with this gid */
467         STATS_TRACK_LAST,
468 };
469
470 /* flags for sbi->ll_flags */
471 #define LL_SBI_NOLCK             0x01 /* DLM locking disabled (directio-only) */
472 #define LL_SBI_CHECKSUM          0x02 /* checksum each page as it's written */
473 #define LL_SBI_FLOCK             0x04
474 #define LL_SBI_USER_XATTR        0x08 /* support user xattr */
475 #define LL_SBI_ACL               0x10 /* support ACL */
476 /*      LL_SBI_RMT_CLIENT        0x40    remote client */
477 #define LL_SBI_MDS_CAPA          0x80 /* support mds capa, obsolete */
478 #define LL_SBI_OSS_CAPA         0x100 /* support oss capa, obsolete */
479 #define LL_SBI_LOCALFLOCK       0x200 /* Local flocks support by kernel */
480 #define LL_SBI_LRU_RESIZE       0x400 /* lru resize support */
481 #define LL_SBI_LAZYSTATFS       0x800 /* lazystatfs mount option */
482 /*      LL_SBI_SOM_PREVIEW     0x1000    SOM preview mount option, obsolete */
483 #define LL_SBI_32BIT_API       0x2000 /* generate 32 bit inodes. */
484 #define LL_SBI_64BIT_HASH      0x4000 /* support 64-bits dir hash/offset */
485 #define LL_SBI_AGL_ENABLED     0x8000 /* enable agl */
486 #define LL_SBI_VERBOSE        0x10000 /* verbose mount/umount */
487 #define LL_SBI_LAYOUT_LOCK    0x20000 /* layout lock support */
488 #define LL_SBI_USER_FID2PATH  0x40000 /* allow fid2path by unprivileged users */
489 #define LL_SBI_XATTR_CACHE    0x80000 /* support for xattr cache */
490 #define LL_SBI_NOROOTSQUASH  0x100000 /* do not apply root squash */
491 #define LL_SBI_ALWAYS_PING   0x200000 /* always ping even if server
492                                        * suppress_pings */
493 #define LL_SBI_FAST_READ     0x400000 /* fast read support */
494 #define LL_SBI_FILE_SECCTX   0x800000 /* set file security context at create */
495 /*      LL_SBI_PIO          0x1000000    parallel IO support, introduced in
496                                          2.10, abandoned */
497 #define LL_SBI_TINY_WRITE   0x2000000 /* tiny write support */
498 #define LL_SBI_FILE_HEAT    0x4000000 /* file heat support */
499 #define LL_SBI_FLAGS {  \
500         "nolck",        \
501         "checksum",     \
502         "flock",        \
503         "user_xattr",   \
504         "acl",          \
505         "???",          \
506         "???",          \
507         "mds_capa",     \
508         "oss_capa",     \
509         "flock",        \
510         "lru_resize",   \
511         "lazy_statfs",  \
512         "som",          \
513         "32bit_api",    \
514         "64bit_hash",   \
515         "agl",          \
516         "verbose",      \
517         "layout",       \
518         "user_fid2path",\
519         "xattr_cache",  \
520         "norootsquash", \
521         "always_ping",  \
522         "fast_read",    \
523         "file_secctx",  \
524         "pio",          \
525         "tiny_write",   \
526         "file_heat",    \
527 }
528
529 /* This is embedded into llite super-blocks to keep track of connect
530  * flags (capabilities) supported by all imports given mount is
531  * connected to. */
532 struct lustre_client_ocd {
533         /* This is conjunction of connect_flags across all imports
534          * (LOVs) this mount is connected to. This field is updated by
535          * cl_ocd_update() under ->lco_lock. */
536         __u64                    lco_flags;
537         struct mutex             lco_lock;
538         struct obd_export       *lco_md_exp;
539         struct obd_export       *lco_dt_exp;
540 };
541
542 struct ll_sb_info {
543         /* this protects pglist and ra_info.  It isn't safe to
544          * grab from interrupt contexts */
545         spinlock_t               ll_lock;
546         spinlock_t               ll_pp_extent_lock; /* pp_extent entry*/
547         spinlock_t               ll_process_lock; /* ll_rw_process_info */
548         struct obd_uuid          ll_sb_uuid;
549         struct obd_export       *ll_md_exp;
550         struct obd_export       *ll_dt_exp;
551         struct obd_device       *ll_md_obd;
552         struct obd_device       *ll_dt_obd;
553         struct dentry           *ll_debugfs_entry;
554         struct lu_fid            ll_root_fid; /* root object fid */
555
556         int                       ll_flags;
557         unsigned int              ll_xattr_cache_enabled:1,
558                                   ll_xattr_cache_set:1, /* already set to 0/1 */
559                                   ll_client_common_fill_super_succeeded:1,
560                                   ll_checksum_set:1;
561
562         struct lustre_client_ocd  ll_lco;
563
564         struct lprocfs_stats     *ll_stats; /* lprocfs stats counter */
565
566         /* Used to track "unstable" pages on a client, and maintain a
567          * LRU list of clean pages. An "unstable" page is defined as
568          * any page which is sent to a server as part of a bulk request,
569          * but is uncommitted to stable storage. */
570         struct cl_client_cache   *ll_cache;
571
572         struct lprocfs_stats     *ll_ra_stats;
573
574         struct ll_ra_info         ll_ra_info;
575         unsigned int              ll_namelen;
576         struct file_operations   *ll_fop;
577
578         struct lu_site           *ll_site;
579         struct cl_device         *ll_cl;
580         /* Statistics */
581         struct ll_rw_extents_info ll_rw_extents_info;
582         int                       ll_extent_process_count;
583         struct ll_rw_process_info ll_rw_process_info[LL_PROCESS_HIST_MAX];
584         unsigned int              ll_offset_process_count;
585         struct ll_rw_process_info ll_rw_offset_info[LL_OFFSET_HIST_MAX];
586         unsigned int              ll_rw_offset_entry_count;
587         int                       ll_stats_track_id;
588         enum stats_track_type     ll_stats_track_type;
589         int                       ll_rw_stats_on;
590
591         /* metadata stat-ahead */
592         unsigned int              ll_sa_running_max;/* max concurrent
593                                                      * statahead instances */
594         unsigned int              ll_sa_max;     /* max statahead RPCs */
595         atomic_t                  ll_sa_total;   /* statahead thread started
596                                                   * count */
597         atomic_t                  ll_sa_wrong;   /* statahead thread stopped for
598                                                   * low hit ratio */
599         atomic_t                  ll_sa_running; /* running statahead thread
600                                                   * count */
601         atomic_t                  ll_agl_total;  /* AGL thread started count */
602
603         dev_t                     ll_sdev_orig; /* save s_dev before assign for
604                                                  * clustred nfs */
605         /* root squash */
606         struct root_squash_info   ll_squash;
607         struct path               ll_mnt;
608
609         /* st_blksize returned by stat(2), when non-zero */
610         unsigned int              ll_stat_blksize;
611
612         /* maximum relative age of cached statfs results */
613         unsigned int              ll_statfs_max_age;
614
615         struct kset               ll_kset;      /* sysfs object */
616         struct completion         ll_kobj_unregister;
617
618         /* File heat */
619         unsigned int              ll_heat_decay_weight;
620         unsigned int              ll_heat_period_second;
621
622         /* filesystem fsname */
623         char                      ll_fsname[LUSTRE_MAXFSNAME + 1];
624
625         /* Persistent Client Cache */
626         struct pcc_super          ll_pcc_super;
627 };
628
629 #define SBI_DEFAULT_HEAT_DECAY_WEIGHT   ((80 * 256 + 50) / 100)
630 #define SBI_DEFAULT_HEAT_PERIOD_SECOND  (60)
631 /*
632  * per file-descriptor read-ahead data.
633  */
634 struct ll_readahead_state {
635         spinlock_t  ras_lock;
636         /* End byte that read(2) try to read.  */
637         unsigned long ras_last_read_end;
638         /*
639          * number of bytes read after last read-ahead window reset. As window
640          * is reset on each seek, this is effectively a number of consecutive
641          * accesses. Maybe ->ras_accessed_in_window is better name.
642          *
643          * XXX nikita: window is also reset (by ras_update()) when Lustre
644          * believes that memory pressure evicts read-ahead pages. In that
645          * case, it probably doesn't make sense to expand window to
646          * PTLRPC_MAX_BRW_PAGES on the third access.
647          */
648         unsigned long ras_consecutive_bytes;
649         /*
650          * number of read requests after the last read-ahead window reset
651          * As window is reset on each seek, this is effectively the number
652          * on consecutive read request and is used to trigger read-ahead.
653          */
654         unsigned long ras_consecutive_requests;
655         /*
656          * Parameters of current read-ahead window. Handled by
657          * ras_update(). On the initial access to the file or after a seek,
658          * window is reset to 0. After 3 consecutive accesses, window is
659          * expanded to PTLRPC_MAX_BRW_PAGES. Afterwards, window is enlarged by
660          * PTLRPC_MAX_BRW_PAGES chunks up to ->ra_max_pages.
661          */
662         pgoff_t ras_window_start, ras_window_len;
663         /*
664          * Optimal RPC size. It decides how many pages will be sent
665          * for each read-ahead.
666          */
667         unsigned long ras_rpc_size;
668         /*
669          * Where next read-ahead should start at. This lies within read-ahead
670          * window. Read-ahead window is read in pieces rather than at once
671          * because: 1. lustre limits total number of pages under read-ahead by
672          * ->ra_max_pages (see ll_ra_count_get()), 2. client cannot read pages
673          * not covered by DLM lock.
674          */
675         pgoff_t ras_next_readahead;
676         /*
677          * Total number of ll_file_read requests issued, reads originating
678          * due to mmap are not counted in this total.  This value is used to
679          * trigger full file read-ahead after multiple reads to a small file.
680          */
681         unsigned long ras_requests;
682         /*
683          * The following 3 items are used for detecting the stride I/O
684          * mode.
685          * In stride I/O mode,
686          * ...............|-----data-----|****gap*****|--------|******|....
687          *    offset      |-stride_bytes-|-stride_gap-|
688          * ras_stride_offset = offset;
689          * ras_stride_length = stride_bytes + stride_gap;
690          * ras_stride_bytes = stride_bytes;
691          * Note: all these three items are counted by bytes.
692          */
693         unsigned long ras_stride_length;
694         unsigned long ras_stride_bytes;
695         unsigned long ras_stride_offset;
696         /*
697          * number of consecutive stride request count, and it is similar as
698          * ras_consecutive_requests, but used for stride I/O mode.
699          * Note: only more than 2 consecutive stride request are detected,
700          * stride read-ahead will be enable
701          */
702         unsigned long ras_consecutive_stride_requests;
703         /* index of the last page that async readahead starts */
704         pgoff_t ras_async_last_readpage;
705         /* whether we should increase readahead window */
706         bool ras_need_increase_window;
707         /* whether ra miss check should be skipped */
708         bool ras_no_miss_check;
709 };
710
711 struct ll_readahead_work {
712         /** File to readahead */
713         struct file                     *lrw_file;
714         /** Start bytes */
715         unsigned long                    lrw_start;
716         /** End bytes */
717         unsigned long                    lrw_end;
718
719         /* async worker to handler read */
720         struct work_struct               lrw_readahead_work;
721 };
722
723 extern struct kmem_cache *ll_file_data_slab;
724 struct lustre_handle;
725 struct ll_file_data {
726         struct ll_readahead_state fd_ras;
727         struct ll_grouplock fd_grouplock;
728         __u64 lfd_pos;
729         __u32 fd_flags;
730         fmode_t fd_omode;
731         /* openhandle if lease exists for this file.
732          * Borrow lli->lli_och_mutex to protect assignment */
733         struct obd_client_handle *fd_lease_och;
734         struct obd_client_handle *fd_och;
735         struct file *fd_file;
736         /* Indicate whether need to report failure when close.
737          * true: failure is known, not report again.
738          * false: unknown failure, should report. */
739         bool fd_write_failed;
740         bool ll_lock_no_expand;
741         rwlock_t fd_lock; /* protect lcc list */
742         struct list_head fd_lccs; /* list of ll_cl_context */
743         /* Used by mirrored file to lead IOs to a specific mirror, usually
744          * for mirror resync. 0 means default. */
745         __u32 fd_designated_mirror;
746         /* The layout version when resync starts. Resync I/O should carry this
747          * layout version for verification to OST objects */
748         __u32 fd_layout_version;
749         struct pcc_file fd_pcc_file;
750 };
751
752 void llite_tunables_unregister(void);
753 int llite_tunables_register(void);
754
755 static inline struct inode *ll_info2i(struct ll_inode_info *lli)
756 {
757         return &lli->lli_vfs_inode;
758 }
759
760 __u32 ll_i2suppgid(struct inode *i);
761 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
762
763 static inline int ll_need_32bit_api(struct ll_sb_info *sbi)
764 {
765 #if BITS_PER_LONG == 32
766         return 1;
767 #elif defined(CONFIG_COMPAT)
768         if (unlikely(sbi->ll_flags & LL_SBI_32BIT_API))
769                 return true;
770
771 # ifdef CONFIG_X86_X32
772         /* in_compat_syscall() returns true when called from a kthread
773          * and CONFIG_X86_X32 is enabled, which is wrong. So check
774          * whether the caller comes from a syscall (ie. not a kthread)
775          * before calling in_compat_syscall(). */
776         if (current->flags & PF_KTHREAD)
777                 return false;
778 # endif
779
780         return unlikely(in_compat_syscall());
781 #else
782         return unlikely(sbi->ll_flags & LL_SBI_32BIT_API);
783 #endif
784 }
785
786 static inline bool ll_sbi_has_fast_read(struct ll_sb_info *sbi)
787 {
788         return !!(sbi->ll_flags & LL_SBI_FAST_READ);
789 }
790
791 static inline bool ll_sbi_has_tiny_write(struct ll_sb_info *sbi)
792 {
793         return !!(sbi->ll_flags & LL_SBI_TINY_WRITE);
794 }
795
796 static inline bool ll_sbi_has_file_heat(struct ll_sb_info *sbi)
797 {
798         return !!(sbi->ll_flags & LL_SBI_FILE_HEAT);
799 }
800
801 void ll_ras_enter(struct file *f, unsigned long pos, unsigned long count);
802
803 /* llite/lcommon_misc.c */
804 int cl_ocd_update(struct obd_device *host, struct obd_device *watched,
805                   enum obd_notify_event ev, void *owner);
806 int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
807                      struct ll_grouplock *lg);
808 void cl_put_grouplock(struct ll_grouplock *lg);
809
810 /* llite/lproc_llite.c */
811 int ll_debugfs_register_super(struct super_block *sb, const char *name);
812 void ll_debugfs_unregister_super(struct super_block *sb);
813 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, long count);
814
815 enum {
816         LPROC_LL_READ_BYTES,
817         LPROC_LL_WRITE_BYTES,
818         LPROC_LL_READ,
819         LPROC_LL_WRITE,
820         LPROC_LL_IOCTL,
821         LPROC_LL_OPEN,
822         LPROC_LL_RELEASE,
823         LPROC_LL_MMAP,
824         LPROC_LL_FAULT,
825         LPROC_LL_MKWRITE,
826         LPROC_LL_LLSEEK,
827         LPROC_LL_FSYNC,
828         LPROC_LL_READDIR,
829         LPROC_LL_SETATTR,
830         LPROC_LL_TRUNC,
831         LPROC_LL_FLOCK,
832         LPROC_LL_GETATTR,
833         LPROC_LL_CREATE,
834         LPROC_LL_LINK,
835         LPROC_LL_UNLINK,
836         LPROC_LL_SYMLINK,
837         LPROC_LL_MKDIR,
838         LPROC_LL_RMDIR,
839         LPROC_LL_MKNOD,
840         LPROC_LL_RENAME,
841         LPROC_LL_STATFS,
842         LPROC_LL_SETXATTR,
843         LPROC_LL_GETXATTR,
844         LPROC_LL_GETXATTR_HITS,
845         LPROC_LL_LISTXATTR,
846         LPROC_LL_REMOVEXATTR,
847         LPROC_LL_INODE_PERM,
848         LPROC_LL_FILE_OPCODES
849 };
850
851 /* llite/dir.c */
852 struct ll_dir_chain {
853 };
854
855 static inline void ll_dir_chain_init(struct ll_dir_chain *chain)
856 {
857 }
858
859 static inline void ll_dir_chain_fini(struct ll_dir_chain *chain)
860 {
861 }
862
863 extern const struct file_operations ll_dir_operations;
864 extern const struct inode_operations ll_dir_inode_operations;
865 #ifdef HAVE_DIR_CONTEXT
866 int ll_dir_read(struct inode *inode, __u64 *pos, struct md_op_data *op_data,
867                 struct dir_context *ctx);
868 #else
869 int ll_dir_read(struct inode *inode, __u64 *pos, struct md_op_data *op_data,
870                 void *cookie, filldir_t filldir);
871 #endif
872 int ll_get_mdt_idx(struct inode *inode);
873 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid);
874 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
875                              __u64 offset, struct ll_dir_chain *chain);
876 void ll_release_page(struct inode *inode, struct page *page, bool remove);
877
878 /* llite/namei.c */
879 extern const struct inode_operations ll_special_inode_operations;
880
881 struct inode *ll_iget(struct super_block *sb, ino_t hash,
882                       struct lustre_md *lic);
883 int ll_test_inode_by_fid(struct inode *inode, void *opaque);
884 int ll_md_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
885                        void *data, int flag);
886 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de);
887 int ll_rmdir_entry(struct inode *dir, char *name, int namelen);
888 void ll_update_times(struct ptlrpc_request *request, struct inode *inode);
889
890 /* llite/rw.c */
891 int ll_writepage(struct page *page, struct writeback_control *wbc);
892 int ll_writepages(struct address_space *, struct writeback_control *wbc);
893 int ll_readpage(struct file *file, struct page *page);
894 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
895                            struct cl_page *page, struct file *file);
896 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras);
897 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io);
898
899 enum lcc_type;
900 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
901                enum lcc_type type);
902 void ll_cl_remove(struct file *file, const struct lu_env *env);
903 struct ll_cl_context *ll_cl_find(struct file *file);
904
905 extern const struct address_space_operations ll_aops;
906
907 /* llite/file.c */
908 extern struct file_operations ll_file_operations;
909 extern struct file_operations ll_file_operations_flock;
910 extern struct file_operations ll_file_operations_noflock;
911 extern struct inode_operations ll_file_inode_operations;
912 extern int ll_have_md_lock(struct inode *inode, __u64 *bits,
913                            enum ldlm_mode l_req_mode);
914 extern enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
915                                       struct lustre_handle *lockh, __u64 flags,
916                                       enum ldlm_mode mode);
917
918 int ll_file_open(struct inode *inode, struct file *file);
919 int ll_file_release(struct inode *inode, struct file *file);
920 int ll_release_openhandle(struct dentry *, struct lookup_intent *);
921 int ll_md_real_close(struct inode *inode, fmode_t fmode);
922 extern void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
923                               struct ll_file_data *file, loff_t pos,
924                               size_t count, int rw);
925 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
926 int ll_getattr(const struct path *path, struct kstat *stat,
927                u32 request_mask, unsigned int flags);
928 #else
929 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat);
930 #endif
931 int ll_getattr_dentry(struct dentry *de, struct kstat *stat);
932 struct posix_acl *ll_get_acl(struct inode *inode, int type);
933 #ifdef HAVE_IOP_SET_ACL
934 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
935 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type);
936 #else  /* !CONFIG_LUSTRE_FS_POSIX_ACL */
937 #define ll_set_acl NULL
938 #endif /* CONFIG_LUSTRE_FS_POSIX_ACL */
939
940 #endif
941 int ll_migrate(struct inode *parent, struct file *file,
942                struct lmv_user_md *lum, const char *name);
943 int ll_get_fid_by_name(struct inode *parent, const char *name,
944                        int namelen, struct lu_fid *fid, struct inode **inode);
945 int ll_inode_permission(struct inode *inode, int mask);
946 int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa);
947 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
948                         unsigned long arg);
949 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
950                         unsigned long arg);
951
952 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
953                              __u64 flags, struct lov_user_md *lum,
954                              int lum_size);
955 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
956                              struct lov_mds_md **lmm, int *lmm_size,
957                              struct ptlrpc_request **request);
958 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
959                      int set_default);
960 int ll_dir_getstripe(struct inode *inode, void **lmmp,
961                      int *lmm_size, struct ptlrpc_request **request,
962                      u64 valid);
963 int ll_fsync(struct file *file, loff_t start, loff_t end, int data);
964 int ll_merge_attr(const struct lu_env *env, struct inode *inode);
965 int ll_fid2path(struct inode *inode, void __user *arg);
966 int ll_data_version(struct inode *inode, __u64 *data_version, int flags);
967 int ll_hsm_release(struct inode *inode);
968 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss);
969 void ll_io_set_mirror(struct cl_io *io, const struct file *file);
970
971 /* llite/dcache.c */
972
973 int ll_d_init(struct dentry *de);
974 extern const struct dentry_operations ll_d_ops;
975 void ll_intent_drop_lock(struct lookup_intent *);
976 void ll_intent_release(struct lookup_intent *);
977 void ll_invalidate_aliases(struct inode *);
978 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry);
979 int ll_revalidate_it_finish(struct ptlrpc_request *request,
980                             struct lookup_intent *it, struct dentry *de);
981
982 /* llite/llite_lib.c */
983 extern struct super_operations lustre_super_operations;
984
985 void ll_lli_init(struct ll_inode_info *lli);
986 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt);
987 void ll_put_super(struct super_block *sb);
988 void ll_kill_super(struct super_block *sb);
989 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock);
990 void ll_dir_clear_lsm_md(struct inode *inode);
991 void ll_clear_inode(struct inode *inode);
992 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
993                    enum op_xvalid xvalid, bool hsm_import);
994 int ll_setattr(struct dentry *de, struct iattr *attr);
995 int ll_statfs(struct dentry *de, struct kstatfs *sfs);
996 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
997                        u32 flags);
998 int ll_update_inode(struct inode *inode, struct lustre_md *md);
999 void ll_update_inode_flags(struct inode *inode, int ext_flags);
1000 int ll_read_inode2(struct inode *inode, void *opaque);
1001 void ll_delete_inode(struct inode *inode);
1002 int ll_iocontrol(struct inode *inode, struct file *file,
1003                  unsigned int cmd, unsigned long arg);
1004 int ll_flush_ctx(struct inode *inode);
1005 void ll_umount_begin(struct super_block *sb);
1006 int ll_remount_fs(struct super_block *sb, int *flags, char *data);
1007 #ifdef HAVE_SUPEROPS_USE_DENTRY
1008 int ll_show_options(struct seq_file *seq, struct dentry *dentry);
1009 #else
1010 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs);
1011 #endif
1012 void ll_dirty_page_discard_warn(struct page *page, int ioret);
1013 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
1014                   struct super_block *, struct lookup_intent *);
1015 int ll_obd_statfs(struct inode *inode, void __user *arg);
1016 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *max_mdsize);
1017 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *default_mdsize);
1018 int ll_set_default_mdsize(struct ll_sb_info *sbi, int default_mdsize);
1019
1020 void ll_unlock_md_op_lsm(struct md_op_data *op_data);
1021 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
1022                                       struct inode *i1, struct inode *i2,
1023                                       const char *name, size_t namelen,
1024                                       __u32 mode, enum md_op_code opc,
1025                                       void *data);
1026 void ll_finish_md_op_data(struct md_op_data *op_data);
1027 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg);
1028 void ll_compute_rootsquash_state(struct ll_sb_info *sbi);
1029 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
1030                         struct lov_user_md **kbuf);
1031 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req);
1032
1033 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
1034                         struct lookup_intent *it);
1035
1036 /* Compute expected user md size when passing in a md from user space */
1037 static inline ssize_t ll_lov_user_md_size(const struct lov_user_md *lum)
1038 {
1039         switch (lum->lmm_magic) {
1040         case LOV_USER_MAGIC_V1:
1041                 return sizeof(struct lov_user_md_v1);
1042         case LOV_USER_MAGIC_V3:
1043                 return sizeof(struct lov_user_md_v3);
1044         case LOV_USER_MAGIC_SPECIFIC:
1045                 if (lum->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
1046                         return -EINVAL;
1047
1048                 return lov_user_md_size(lum->lmm_stripe_count,
1049                                         LOV_USER_MAGIC_SPECIFIC);
1050         case LOV_USER_MAGIC_COMP_V1:
1051                 return ((struct lov_comp_md_v1 *)lum)->lcm_size;
1052         case LOV_USER_MAGIC_FOREIGN:
1053                 return foreign_size(lum);
1054         }
1055
1056         return -EINVAL;
1057 }
1058
1059 /* llite/llite_nfs.c */
1060 extern struct export_operations lustre_export_operations;
1061 __u32 get_uuid2int(const char *name, int len);
1062 struct inode *search_inode_for_lustre(struct super_block *sb,
1063                                       const struct lu_fid *fid);
1064 int ll_dir_get_parent_fid(struct inode *dir, struct lu_fid *parent_fid);
1065
1066 /* llite/symlink.c */
1067 extern struct inode_operations ll_fast_symlink_inode_operations;
1068
1069 /**
1070  * IO arguments for various VFS I/O interfaces.
1071  */
1072 struct vvp_io_args {
1073         /** normal/sendfile/splice */
1074         enum vvp_io_subtype via_io_subtype;
1075
1076         union {
1077                 struct {
1078                         struct kiocb      *via_iocb;
1079                         struct iov_iter   *via_iter;
1080                 } normal;
1081                 struct {
1082                         struct pipe_inode_info  *via_pipe;
1083                         unsigned int       via_flags;
1084                 } splice;
1085         } u;
1086 };
1087
1088 enum lcc_type {
1089         LCC_RW = 1,
1090         LCC_MMAP
1091 };
1092
1093 struct ll_cl_context {
1094         struct list_head         lcc_list;
1095         void                    *lcc_cookie;
1096         const struct lu_env     *lcc_env;
1097         struct cl_io            *lcc_io;
1098         struct cl_page          *lcc_page;
1099         enum lcc_type            lcc_type;
1100 };
1101
1102 struct ll_thread_info {
1103         struct vvp_io_args      lti_args;
1104         struct ra_io_arg        lti_ria;
1105         struct ll_cl_context    lti_io_ctx;
1106 };
1107
1108 extern struct lu_context_key ll_thread_key;
1109
1110 static inline struct ll_thread_info *ll_env_info(const struct lu_env *env)
1111 {
1112         struct ll_thread_info *lti;
1113
1114         lti = lu_context_key_get(&env->le_ctx, &ll_thread_key);
1115         LASSERT(lti != NULL);
1116
1117         return lti;
1118 }
1119
1120 static inline struct vvp_io_args *ll_env_args(const struct lu_env *env,
1121                                               enum vvp_io_subtype type)
1122 {
1123         struct vvp_io_args *via = &ll_env_info(env)->lti_args;
1124
1125         via->via_io_subtype = type;
1126
1127         return via;
1128 }
1129
1130 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1131                 struct vvp_io_args *args);
1132
1133 /* llite/llite_mmap.c */
1134
1135 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last);
1136 int ll_file_mmap(struct file * file, struct vm_area_struct * vma);
1137 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
1138                      unsigned long addr, size_t count);
1139 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
1140                                size_t count);
1141
1142 #define    ll_s2sbi(sb)        (s2lsi(sb)->lsi_llsbi)
1143
1144 /* don't need an addref as the sb_info should be holding one */
1145 static inline struct obd_export *ll_s2dtexp(struct super_block *sb)
1146 {
1147         return ll_s2sbi(sb)->ll_dt_exp;
1148 }
1149
1150 /* don't need an addref as the sb_info should be holding one */
1151 static inline struct obd_export *ll_s2mdexp(struct super_block *sb)
1152 {
1153         return ll_s2sbi(sb)->ll_md_exp;
1154 }
1155
1156 static inline struct client_obd *sbi2mdc(struct ll_sb_info *sbi)
1157 {
1158         struct obd_device *obd = sbi->ll_md_exp->exp_obd;
1159         if (obd == NULL)
1160                 LBUG();
1161         return &obd->u.cli;
1162 }
1163
1164 // FIXME: replace the name of this with LL_SB to conform to kernel stuff
1165 static inline struct ll_sb_info *ll_i2sbi(struct inode *inode)
1166 {
1167         return ll_s2sbi(inode->i_sb);
1168 }
1169
1170 static inline struct obd_export *ll_i2dtexp(struct inode *inode)
1171 {
1172         return ll_s2dtexp(inode->i_sb);
1173 }
1174
1175 static inline struct obd_export *ll_i2mdexp(struct inode *inode)
1176 {
1177         return ll_s2mdexp(inode->i_sb);
1178 }
1179
1180 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
1181 {
1182         struct lu_fid *fid;
1183
1184         LASSERT(inode != NULL);
1185         fid = &ll_i2info(inode)->lli_fid;
1186
1187         return fid;
1188 }
1189
1190 static inline bool ll_dir_striped(struct inode *inode)
1191 {
1192         LASSERT(inode);
1193         return S_ISDIR(inode->i_mode) &&
1194                lmv_dir_striped(ll_i2info(inode)->lli_lsm_md);
1195 }
1196
1197 static inline loff_t ll_file_maxbytes(struct inode *inode)
1198 {
1199         struct cl_object *obj = ll_i2info(inode)->lli_clob;
1200
1201         if (obj == NULL)
1202                 return MAX_LFS_FILESIZE;
1203
1204         return min_t(loff_t, cl_object_maxbytes(obj), MAX_LFS_FILESIZE);
1205 }
1206
1207 /* llite/xattr.c */
1208 extern const struct xattr_handler *ll_xattr_handlers[];
1209
1210 #define XATTR_USER_T            1
1211 #define XATTR_TRUSTED_T         2
1212 #define XATTR_SECURITY_T        3
1213 #define XATTR_ACL_ACCESS_T      4
1214 #define XATTR_ACL_DEFAULT_T     5
1215 #define XATTR_LUSTRE_T          6
1216 #define XATTR_OTHER_T           7
1217
1218 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size);
1219 int ll_xattr_list(struct inode *inode, const char *name, int type,
1220                   void *buffer, size_t size, u64 valid);
1221 const struct xattr_handler *get_xattr_type(const char *name);
1222
1223 /**
1224  * Common IO arguments for various VFS I/O interfaces.
1225  */
1226 int cl_sb_init(struct super_block *sb);
1227 int cl_sb_fini(struct super_block *sb);
1228
1229 enum ras_update_flags {
1230         LL_RAS_HIT  = 0x1,
1231         LL_RAS_MMAP = 0x2
1232 };
1233 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len);
1234 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which);
1235
1236 /* statahead.c */
1237
1238 #define LL_SA_RPC_MIN           2
1239 #define LL_SA_RPC_DEF           32
1240 #define LL_SA_RPC_MAX           512
1241
1242 /* XXX: If want to support more concurrent statahead instances,
1243  *      please consider to decentralize the RPC lists attached
1244  *      on related import, such as imp_{sending,delayed}_list.
1245  *      LU-11079 */
1246 #define LL_SA_RUNNING_MAX       256
1247 #define LL_SA_RUNNING_DEF       16
1248
1249 #define LL_SA_CACHE_BIT         5
1250 #define LL_SA_CACHE_SIZE        (1 << LL_SA_CACHE_BIT)
1251 #define LL_SA_CACHE_MASK        (LL_SA_CACHE_SIZE - 1)
1252
1253 /* per inode struct, for dir only */
1254 struct ll_statahead_info {
1255         struct dentry          *sai_dentry;
1256         atomic_t                sai_refcount;   /* when access this struct, hold
1257                                                  * refcount */
1258         unsigned int            sai_max;        /* max ahead of lookup */
1259         __u64                   sai_sent;       /* stat requests sent count */
1260         __u64                   sai_replied;    /* stat requests which received
1261                                                  * reply */
1262         __u64                   sai_index;      /* index of statahead entry */
1263         __u64                   sai_index_wait; /* index of entry which is the
1264                                                  * caller is waiting for */
1265         __u64                   sai_hit;        /* hit count */
1266         __u64                   sai_miss;       /* miss count:
1267                                                  * for "ls -al" case, includes
1268                                                  * hidden dentry miss;
1269                                                  * for "ls -l" case, it does not
1270                                                  * include hidden dentry miss.
1271                                                  * "sai_miss_hidden" is used for
1272                                                  * the later case.
1273                                                  */
1274         unsigned int            sai_consecutive_miss; /* consecutive miss */
1275         unsigned int            sai_miss_hidden;/* "ls -al", but first dentry
1276                                                  * is not a hidden one */
1277         unsigned int            sai_skip_hidden;/* skipped hidden dentry count
1278                                                  */
1279         unsigned int            sai_ls_all:1,   /* "ls -al", do stat-ahead for
1280                                                  * hidden entries */
1281                                 sai_agl_valid:1,/* AGL is valid for the dir */
1282                                 sai_in_readpage:1;/* statahead is in readdir()*/
1283         wait_queue_head_t       sai_waitq;      /* stat-ahead wait queue */
1284         struct ptlrpc_thread    sai_thread;     /* stat-ahead thread */
1285         struct ptlrpc_thread    sai_agl_thread; /* AGL thread */
1286         struct list_head        sai_interim_entries; /* entries which got async
1287                                                       * stat reply, but not
1288                                                       * instantiated */
1289         struct list_head        sai_entries;    /* completed entries */
1290         struct list_head        sai_agls;       /* AGLs to be sent */
1291         struct list_head        sai_cache[LL_SA_CACHE_SIZE];
1292         spinlock_t              sai_cache_lock[LL_SA_CACHE_SIZE];
1293         atomic_t                sai_cache_count; /* entry count in cache */
1294 };
1295
1296 int ll_statahead(struct inode *dir, struct dentry **dentry, bool unplug);
1297 void ll_authorize_statahead(struct inode *dir, void *key);
1298 void ll_deauthorize_statahead(struct inode *dir, void *key);
1299
1300 /* glimpse.c */
1301 blkcnt_t dirty_cnt(struct inode *inode);
1302
1303 int cl_glimpse_size0(struct inode *inode, int agl);
1304 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
1305                     struct inode *inode, struct cl_object *clob, int agl);
1306
1307 static inline int cl_glimpse_size(struct inode *inode)
1308 {
1309         return cl_glimpse_size0(inode, 0);
1310 }
1311
1312 /* AGL is 'asychronous glimpse lock', which is a speculative lock taken as
1313  * part of statahead */
1314 static inline int cl_agl(struct inode *inode)
1315 {
1316         return cl_glimpse_size0(inode, 1);
1317 }
1318
1319 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise);
1320
1321 int cl_io_get(struct inode *inode, struct lu_env **envout,
1322               struct cl_io **ioout, __u16 *refcheck);
1323
1324 static inline int ll_glimpse_size(struct inode *inode)
1325 {
1326         struct ll_inode_info *lli = ll_i2info(inode);
1327         int rc;
1328
1329         down_read(&lli->lli_glimpse_sem);
1330         rc = cl_glimpse_size(inode);
1331         lli->lli_glimpse_time = ktime_get();
1332         up_read(&lli->lli_glimpse_sem);
1333         return rc;
1334 }
1335
1336 /* dentry may statahead when statahead is enabled and current process has opened
1337  * parent directory, and this dentry hasn't accessed statahead cache before */
1338 static inline bool
1339 dentry_may_statahead(struct inode *dir, struct dentry *dentry)
1340 {
1341         struct ll_inode_info  *lli;
1342         struct ll_dentry_data *ldd;
1343
1344         if (ll_i2sbi(dir)->ll_sa_max == 0)
1345                 return false;
1346
1347         lli = ll_i2info(dir);
1348
1349         /* statahead is not allowed for this dir, there may be three causes:
1350          * 1. dir is not opened.
1351          * 2. statahead hit ratio is too low.
1352          * 3. previous stat started statahead thread failed. */
1353         if (!lli->lli_sa_enabled)
1354                 return false;
1355
1356         /* not the same process, don't statahead */
1357         if (lli->lli_opendir_pid != current_pid())
1358                 return false;
1359
1360         /*
1361          * When stating a dentry, kernel may trigger 'revalidate' or 'lookup'
1362          * multiple times, eg. for 'getattr', 'getxattr' and etc.
1363          * For patchless client, lookup intent is not accurate, which may
1364          * misguide statahead. For example:
1365          * The 'revalidate' call for 'getattr' and 'getxattr' of a dentry will
1366          * have the same intent -- IT_GETATTR, while one dentry should access
1367          * statahead cache once, otherwise statahead windows is messed up.
1368          * The solution is as following:
1369          * Assign 'lld_sa_generation' with 'lli_sa_generation' when a dentry
1370          * IT_GETATTR for the first time, and subsequent IT_GETATTR will
1371          * bypass interacting with statahead cache by checking
1372          * 'lld_sa_generation == lli->lli_sa_generation'.
1373          */
1374         ldd = ll_d2d(dentry);
1375         if (ldd != NULL && ldd->lld_sa_generation == lli->lli_sa_generation)
1376                 return false;
1377
1378         return true;
1379 }
1380
1381 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
1382                        enum cl_fsync_mode mode, int ignore_layout);
1383
1384 static inline int ll_file_nolock(const struct file *file)
1385 {
1386         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1387         struct inode *inode = file_inode((struct file *)file);
1388
1389         LASSERT(fd != NULL);
1390         return ((fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
1391                 (ll_i2sbi(inode)->ll_flags & LL_SBI_NOLCK));
1392 }
1393
1394 static inline void ll_set_lock_data(struct obd_export *exp, struct inode *inode,
1395                                     struct lookup_intent *it, __u64 *bits)
1396 {
1397         if (!it->it_lock_set) {
1398                 struct lustre_handle handle;
1399
1400                 /* If this inode is a remote object, it will get two
1401                  * separate locks in different namespaces, Master MDT,
1402                  * where the name entry is, will grant LOOKUP lock,
1403                  * remote MDT, where the object is, will grant
1404                  * UPDATE|PERM lock. The inode will be attched to both
1405                  * LOOKUP and PERM locks, so revoking either locks will
1406                  * case the dcache being cleared */
1407                 if (it->it_remote_lock_mode) {
1408                         handle.cookie = it->it_remote_lock_handle;
1409                         CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID
1410                                "(%p) for remote lock %#llx\n",
1411                                PFID(ll_inode2fid(inode)), inode,
1412                                handle.cookie);
1413                         md_set_lock_data(exp, &handle, inode, NULL);
1414                 }
1415
1416                 handle.cookie = it->it_lock_handle;
1417
1418                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)"
1419                        " for lock %#llx\n",
1420                        PFID(ll_inode2fid(inode)), inode, handle.cookie);
1421
1422                 md_set_lock_data(exp, &handle, inode, &it->it_lock_bits);
1423                 it->it_lock_set = 1;
1424         }
1425
1426         if (bits != NULL)
1427                 *bits = it->it_lock_bits;
1428 }
1429
1430 static inline int d_lustre_invalid(const struct dentry *dentry)
1431 {
1432         struct ll_dentry_data *lld = ll_d2d(dentry);
1433
1434         return (lld == NULL) || lld->lld_invalid;
1435 }
1436
1437 static inline void __d_lustre_invalidate(struct dentry *dentry)
1438 {
1439         struct ll_dentry_data *lld = ll_d2d(dentry);
1440
1441         if (lld != NULL)
1442                 lld->lld_invalid = 1;
1443 }
1444
1445 /*
1446  * Mark dentry INVALID, if dentry refcount is zero (this is normally case for
1447  * ll_md_blocking_ast), unhash this dentry, and let dcache to reclaim it later;
1448  * else dput() of the last refcount will unhash this dentry and kill it.
1449  */
1450 static inline void d_lustre_invalidate(struct dentry *dentry, int nested)
1451 {
1452         CDEBUG(D_DENTRY, "invalidate dentry %.*s (%p) parent %p inode %p "
1453                "refc %d\n", dentry->d_name.len, dentry->d_name.name, dentry,
1454                dentry->d_parent, dentry->d_inode, ll_d_count(dentry));
1455
1456         spin_lock_nested(&dentry->d_lock,
1457                          nested ? DENTRY_D_LOCK_NESTED : DENTRY_D_LOCK_NORMAL);
1458         __d_lustre_invalidate(dentry);
1459         /*
1460          * We should be careful about dentries created by d_obtain_alias().
1461          * These dentries are not put in the dentry tree, instead they are
1462          * linked to sb->s_anon through dentry->d_hash.
1463          * shrink_dcache_for_umount() shrinks the tree and sb->s_anon list.
1464          * If we unhashed such a dentry, unmount would not be able to find
1465          * it and busy inodes would be reported.
1466          */
1467         if (ll_d_count(dentry) == 0 && !(dentry->d_flags & DCACHE_DISCONNECTED))
1468                 __d_drop(dentry);
1469         spin_unlock(&dentry->d_lock);
1470 }
1471
1472 static inline void d_lustre_revalidate(struct dentry *dentry)
1473 {
1474         spin_lock(&dentry->d_lock);
1475         LASSERT(ll_d2d(dentry) != NULL);
1476         ll_d2d(dentry)->lld_invalid = 0;
1477         spin_unlock(&dentry->d_lock);
1478 }
1479
1480 static inline dev_t ll_compat_encode_dev(dev_t dev)
1481 {
1482         /* The compat_sys_*stat*() syscalls will fail unless the
1483          * device majors and minors are both less than 256. Note that
1484          * the value returned here will be passed through
1485          * old_encode_dev() in cp_compat_stat(). And so we are not
1486          * trying to return a valid compat (u16) device number, just
1487          * one that will pass the old_valid_dev() check. */
1488
1489         return MKDEV(MAJOR(dev) & 0xff, MINOR(dev) & 0xff);
1490 }
1491
1492 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf);
1493 int ll_layout_refresh(struct inode *inode, __u32 *gen);
1494 int ll_layout_restore(struct inode *inode, loff_t start, __u64 length);
1495 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
1496                            struct lu_extent *ext);
1497
1498 int ll_xattr_init(void);
1499 void ll_xattr_fini(void);
1500
1501 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1502                     struct cl_page *page, enum cl_req_type crt);
1503
1504 int ll_getparent(struct file *file, struct getparent __user *arg);
1505
1506 /* lcommon_cl.c */
1507 int cl_setattr_ost(struct cl_object *obj, const struct iattr *attr,
1508                    enum op_xvalid xvalid, unsigned int attr_flags);
1509
1510 extern struct lu_env *cl_inode_fini_env;
1511 extern __u16 cl_inode_fini_refcheck;
1512
1513 int cl_file_inode_init(struct inode *inode, struct lustre_md *md);
1514 void cl_inode_fini(struct inode *inode);
1515
1516 u64 cl_fid_build_ino(const struct lu_fid *fid, int api32);
1517 u32 cl_fid_build_gen(const struct lu_fid *fid);
1518
1519 static inline struct pcc_super *ll_i2pccs(struct inode *inode)
1520 {
1521         return &ll_i2sbi(inode)->ll_pcc_super;
1522 }
1523
1524 static inline struct pcc_super *ll_info2pccs(struct ll_inode_info *lli)
1525 {
1526         return ll_i2pccs(ll_info2i(lli));
1527 }
1528
1529 #endif /* LLITE_INTERNAL_H */