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