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