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