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