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