Whamcloud - gitweb
LU-6943 clio: get rid of cl_req
[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 #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 40meg of readahead on a given system.  That much tied
338  * up in 512k readahead requests serviced at 40ms each is about 1GB/s. */
339 #define SBI_DEFAULT_READAHEAD_MAX (40UL << (20 - PAGE_CACHE_SHIFT))
340
341 /* default to read-ahead full files smaller than 2MB on the second read */
342 #define SBI_DEFAULT_READAHEAD_WHOLE_MAX (2UL << (20 - PAGE_CACHE_SHIFT))
343
344 enum ra_stat {
345         RA_STAT_HIT = 0,
346         RA_STAT_MISS,
347         RA_STAT_DISTANT_READPAGE,
348         RA_STAT_MISS_IN_WINDOW,
349         RA_STAT_FAILED_GRAB_PAGE,
350         RA_STAT_FAILED_MATCH,
351         RA_STAT_DISCARDED,
352         RA_STAT_ZERO_LEN,
353         RA_STAT_ZERO_WINDOW,
354         RA_STAT_EOF,
355         RA_STAT_MAX_IN_FLIGHT,
356         RA_STAT_WRONG_GRAB_PAGE,
357         RA_STAT_FAILED_REACH_END,
358         _NR_RA_STAT,
359 };
360
361 struct ll_ra_info {
362         atomic_t        ra_cur_pages;
363         unsigned long   ra_max_pages;
364         unsigned long   ra_max_pages_per_file;
365         unsigned long   ra_max_read_ahead_whole_pages;
366 };
367
368 /* ra_io_arg will be filled in the beginning of ll_readahead with
369  * ras_lock, then the following ll_read_ahead_pages will read RA
370  * pages according to this arg, all the items in this structure are
371  * counted by page index.
372  */
373 struct ra_io_arg {
374         unsigned long ria_start;  /* start offset of read-ahead*/
375         unsigned long ria_end;    /* end offset of read-ahead*/
376         /* If stride read pattern is detected, ria_stoff means where
377          * stride read is started. Note: for normal read-ahead, the
378          * value here is meaningless, and also it will not be accessed*/
379         pgoff_t ria_stoff;
380         /* ria_length and ria_pages are the length and pages length in the
381          * stride I/O mode. And they will also be used to check whether
382          * it is stride I/O read-ahead in the read-ahead pages*/
383         unsigned long ria_length;
384         unsigned long ria_pages;
385 };
386
387 /* LL_HIST_MAX=32 causes an overflow */
388 #define LL_HIST_MAX 28
389 #define LL_HIST_START 12 /* buckets start at 2^12 = 4k */
390 #define LL_PROCESS_HIST_MAX 10
391 struct per_process_info {
392         pid_t pid;
393         struct obd_histogram pp_r_hist;
394         struct obd_histogram pp_w_hist;
395 };
396
397 /* pp_extents[LL_PROCESS_HIST_MAX] will hold the combined process info */
398 struct ll_rw_extents_info {
399         struct per_process_info pp_extents[LL_PROCESS_HIST_MAX + 1];
400 };
401
402 #define LL_OFFSET_HIST_MAX 100
403 struct ll_rw_process_info {
404         pid_t                     rw_pid;
405         int                       rw_op;
406         loff_t                    rw_range_start;
407         loff_t                    rw_range_end;
408         loff_t                    rw_last_file_pos;
409         loff_t                    rw_offset;
410         size_t                    rw_smallest_extent;
411         size_t                    rw_largest_extent;
412         struct ll_file_data      *rw_last_file;
413 };
414
415 enum stats_track_type {
416         STATS_TRACK_ALL = 0,  /* track all processes */
417         STATS_TRACK_PID,      /* track process with this pid */
418         STATS_TRACK_PPID,     /* track processes with this ppid */
419         STATS_TRACK_GID,      /* track processes with this gid */
420         STATS_TRACK_LAST,
421 };
422
423 /* flags for sbi->ll_flags */
424 #define LL_SBI_NOLCK             0x01 /* DLM locking disabled (directio-only) */
425 #define LL_SBI_CHECKSUM          0x02 /* checksum each page as it's written */
426 #define LL_SBI_FLOCK             0x04
427 #define LL_SBI_USER_XATTR        0x08 /* support user xattr */
428 #define LL_SBI_ACL               0x10 /* support ACL */
429 #define LL_SBI_RMT_CLIENT        0x40 /* remote client */
430 #define LL_SBI_MDS_CAPA          0x80 /* support mds capa, obsolete */
431 #define LL_SBI_OSS_CAPA         0x100 /* support oss capa, obsolete */
432 #define LL_SBI_LOCALFLOCK       0x200 /* Local flocks support by kernel */
433 #define LL_SBI_LRU_RESIZE       0x400 /* lru resize support */
434 #define LL_SBI_LAZYSTATFS       0x800 /* lazystatfs mount option */
435 /*      LL_SBI_SOM_PREVIEW     0x1000    SOM preview mount option, obsolete */
436 #define LL_SBI_32BIT_API       0x2000 /* generate 32 bit inodes. */
437 #define LL_SBI_64BIT_HASH      0x4000 /* support 64-bits dir hash/offset */
438 #define LL_SBI_AGL_ENABLED     0x8000 /* enable agl */
439 #define LL_SBI_VERBOSE        0x10000 /* verbose mount/umount */
440 #define LL_SBI_LAYOUT_LOCK    0x20000 /* layout lock support */
441 #define LL_SBI_USER_FID2PATH  0x40000 /* allow fid2path by unprivileged users */
442 #define LL_SBI_XATTR_CACHE    0x80000 /* support for xattr cache */
443 #define LL_SBI_NOROOTSQUASH  0x100000 /* do not apply root squash */
444
445 #define LL_SBI_FLAGS {  \
446         "nolck",        \
447         "checksum",     \
448         "flock",        \
449         "user_xattr",   \
450         "acl",          \
451         "???",          \
452         "rmt_client",   \
453         "mds_capa",     \
454         "oss_capa",     \
455         "flock",        \
456         "lru_resize",   \
457         "lazy_statfs",  \
458         "som",          \
459         "32bit_api",    \
460         "64bit_hash",   \
461         "agl",          \
462         "verbose",      \
463         "layout",       \
464         "user_fid2path",\
465         "xattr_cache",  \
466         "norootsquash", \
467 }
468
469 #define RCE_HASHES      32
470
471 struct rmtacl_ctl_entry {
472         struct list_head       rce_list;
473         pid_t            rce_key; /* hash key */
474         int              rce_ops; /* acl operation type */
475 };
476
477 struct rmtacl_ctl_table {
478         spinlock_t      rct_lock;
479         struct list_head        rct_entries[RCE_HASHES];
480 };
481
482 #define EE_HASHES       32
483
484 struct eacl_entry {
485         struct list_head            ee_list;
486         pid_t                 ee_key; /* hash key */
487         struct lu_fid         ee_fid;
488         int                   ee_type; /* ACL type for ACCESS or DEFAULT */
489         ext_acl_xattr_header *ee_acl;
490 };
491
492 struct eacl_table {
493         spinlock_t      et_lock;
494         struct list_head        et_entries[EE_HASHES];
495 };
496
497
498 /* This is embedded into llite super-blocks to keep track of connect
499  * flags (capabilities) supported by all imports given mount is
500  * connected to. */
501 struct lustre_client_ocd {
502         /* This is conjunction of connect_flags across all imports
503          * (LOVs) this mount is connected to. This field is updated by
504          * cl_ocd_update() under ->lco_lock. */
505         __u64                    lco_flags;
506         struct mutex             lco_lock;
507         struct obd_export       *lco_md_exp;
508         struct obd_export       *lco_dt_exp;
509 };
510
511 struct ll_sb_info {
512         /* this protects pglist and ra_info.  It isn't safe to
513          * grab from interrupt contexts */
514         spinlock_t                ll_lock;
515         spinlock_t                ll_pp_extent_lock; /* pp_extent entry*/
516         spinlock_t                ll_process_lock; /* ll_rw_process_info */
517         struct obd_uuid           ll_sb_uuid;
518         struct obd_export        *ll_md_exp;
519         struct obd_export        *ll_dt_exp;
520         struct proc_dir_entry*    ll_proc_root;
521         struct lu_fid             ll_root_fid; /* root object fid */
522
523         int                       ll_flags;
524         unsigned int              ll_umounting:1,
525                                   ll_xattr_cache_enabled:1,
526                                   ll_client_common_fill_super_succeeded:1;
527
528         /* per-conn chain of SBs */
529         struct list_head                ll_conn_chain;
530         struct lustre_client_ocd  ll_lco;
531
532         /*please don't ask -p*/
533         struct list_head        ll_orphan_dentry_list;
534
535         struct lprocfs_stats     *ll_stats; /* lprocfs stats counter */
536
537         /* Used to track "unstable" pages on a client, and maintain a
538          * LRU list of clean pages. An "unstable" page is defined as
539          * any page which is sent to a server as part of a bulk request,
540          * but is uncommitted to stable storage. */
541         struct cl_client_cache   *ll_cache;
542
543         struct lprocfs_stats     *ll_ra_stats;
544
545         struct ll_ra_info         ll_ra_info;
546         unsigned int              ll_namelen;
547         struct file_operations   *ll_fop;
548
549         unsigned int              ll_md_brw_pages; /* readdir pages per RPC */
550
551         struct lu_site           *ll_site;
552         struct cl_device         *ll_cl;
553         /* Statistics */
554         struct ll_rw_extents_info ll_rw_extents_info;
555         int                       ll_extent_process_count;
556         struct ll_rw_process_info ll_rw_process_info[LL_PROCESS_HIST_MAX];
557         unsigned int              ll_offset_process_count;
558         struct ll_rw_process_info ll_rw_offset_info[LL_OFFSET_HIST_MAX];
559         unsigned int              ll_rw_offset_entry_count;
560         int                       ll_stats_track_id;
561         enum stats_track_type     ll_stats_track_type;
562         int                       ll_rw_stats_on;
563
564         /* metadata stat-ahead */
565         unsigned int              ll_sa_max;     /* max statahead RPCs */
566         atomic_t                  ll_sa_total;   /* statahead thread started
567                                                   * count */
568         atomic_t                  ll_sa_wrong;   /* statahead thread stopped for
569                                                   * low hit ratio */
570         atomic_t                  ll_sa_running; /* running statahead thread
571                                                   * count */
572         atomic_t                  ll_agl_total;  /* AGL thread started count */
573
574         dev_t                     ll_sdev_orig; /* save s_dev before assign for
575                                                  * clustred nfs */
576         struct rmtacl_ctl_table   ll_rct;
577         struct eacl_table         ll_et;
578
579         /* root squash */
580         struct root_squash_info   ll_squash;
581 };
582
583 /*
584  * per file-descriptor read-ahead data.
585  */
586 struct ll_readahead_state {
587         spinlock_t  ras_lock;
588         /*
589          * index of the last page that read(2) needed and that wasn't in the
590          * cache. Used by ras_update() to detect seeks.
591          *
592          * XXX nikita: if access seeks into cached region, Lustre doesn't see
593          * this.
594          */
595         unsigned long   ras_last_readpage;
596         /*
597          * number of pages read after last read-ahead window reset. As window
598          * is reset on each seek, this is effectively a number of consecutive
599          * accesses. Maybe ->ras_accessed_in_window is better name.
600          *
601          * XXX nikita: window is also reset (by ras_update()) when Lustre
602          * believes that memory pressure evicts read-ahead pages. In that
603          * case, it probably doesn't make sense to expand window to
604          * PTLRPC_MAX_BRW_PAGES on the third access.
605          */
606         unsigned long   ras_consecutive_pages;
607         /*
608          * number of read requests after the last read-ahead window reset
609          * As window is reset on each seek, this is effectively the number
610          * on consecutive read request and is used to trigger read-ahead.
611          */
612         unsigned long   ras_consecutive_requests;
613         /*
614          * Parameters of current read-ahead window. Handled by
615          * ras_update(). On the initial access to the file or after a seek,
616          * window is reset to 0. After 3 consecutive accesses, window is
617          * expanded to PTLRPC_MAX_BRW_PAGES. Afterwards, window is enlarged by
618          * PTLRPC_MAX_BRW_PAGES chunks up to ->ra_max_pages.
619          */
620         unsigned long   ras_window_start, ras_window_len;
621         /*
622          * Where next read-ahead should start at. This lies within read-ahead
623          * window. Read-ahead window is read in pieces rather than at once
624          * because: 1. lustre limits total number of pages under read-ahead by
625          * ->ra_max_pages (see ll_ra_count_get()), 2. client cannot read pages
626          * not covered by DLM lock.
627          */
628         unsigned long   ras_next_readahead;
629         /*
630          * Total number of ll_file_read requests issued, reads originating
631          * due to mmap are not counted in this total.  This value is used to
632          * trigger full file read-ahead after multiple reads to a small file.
633          */
634         unsigned long   ras_requests;
635         /*
636          * Page index with respect to the current request, these value
637          * will not be accurate when dealing with reads issued via mmap.
638          */
639         unsigned long   ras_request_index;
640         /*
641          * The following 3 items are used for detecting the stride I/O
642          * mode.
643          * In stride I/O mode,
644          * ...............|-----data-----|****gap*****|--------|******|....
645          *    offset      |-stride_pages-|-stride_gap-|
646          * ras_stride_offset = offset;
647          * ras_stride_length = stride_pages + stride_gap;
648          * ras_stride_pages = stride_pages;
649          * Note: all these three items are counted by pages.
650          */
651         unsigned long   ras_stride_length;
652         unsigned long   ras_stride_pages;
653         pgoff_t         ras_stride_offset;
654         /*
655          * number of consecutive stride request count, and it is similar as
656          * ras_consecutive_requests, but used for stride I/O mode.
657          * Note: only more than 2 consecutive stride request are detected,
658          * stride read-ahead will be enable
659          */
660         unsigned long   ras_consecutive_stride_requests;
661 };
662
663 extern struct kmem_cache *ll_file_data_slab;
664 struct lustre_handle;
665 struct ll_file_data {
666         struct ll_readahead_state fd_ras;
667         struct ll_grouplock fd_grouplock;
668         __u64 lfd_pos;
669         __u32 fd_flags;
670         fmode_t fd_omode;
671         /* openhandle if lease exists for this file.
672          * Borrow lli->lli_och_mutex to protect assignment */
673         struct obd_client_handle *fd_lease_och;
674         struct obd_client_handle *fd_och;
675         struct file *fd_file;
676         /* Indicate whether need to report failure when close.
677          * true: failure is known, not report again.
678          * false: unknown failure, should report. */
679         bool fd_write_failed;
680         rwlock_t fd_lock; /* protect lcc list */
681         struct list_head fd_lccs; /* list of ll_cl_context */
682 };
683
684 extern spinlock_t inode_lock;
685
686 extern struct proc_dir_entry *proc_lustre_fs_root;
687
688 static inline struct inode *ll_info2i(struct ll_inode_info *lli)
689 {
690         return &lli->lli_vfs_inode;
691 }
692
693 __u32 ll_i2suppgid(struct inode *i);
694 void ll_i2gids(__u32 *suppgids, struct inode *i1,struct inode *i2);
695
696 static inline int ll_need_32bit_api(struct ll_sb_info *sbi)
697 {
698 #if BITS_PER_LONG == 32
699         return 1;
700 #elif defined(CONFIG_COMPAT)
701         return unlikely(is_compat_task() || (sbi->ll_flags & LL_SBI_32BIT_API));
702 #else
703         return unlikely(sbi->ll_flags & LL_SBI_32BIT_API);
704 #endif
705 }
706
707 void ll_ras_enter(struct file *f);
708
709 /* llite/lcommon_misc.c */
710 int cl_ocd_update(struct obd_device *host, struct obd_device *watched,
711                   enum obd_notify_event ev, void *owner, void *data);
712 int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
713                      struct ll_grouplock *lg);
714 void cl_put_grouplock(struct ll_grouplock *lg);
715
716 /* llite/lproc_llite.c */
717 #ifdef CONFIG_PROC_FS
718 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
719                                 struct super_block *sb, char *osc, char *mdc);
720 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi);
721 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count);
722 extern struct lprocfs_vars lprocfs_llite_obd_vars[];
723 #else
724 static inline int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
725                         struct super_block *sb, char *osc, char *mdc){return 0;}
726 static inline void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi) {}
727 static void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count) {}
728 #endif
729
730 enum {
731         LPROC_LL_DIRTY_HITS,
732         LPROC_LL_DIRTY_MISSES,
733         LPROC_LL_READ_BYTES,
734         LPROC_LL_WRITE_BYTES,
735         LPROC_LL_BRW_READ,
736         LPROC_LL_BRW_WRITE,
737         LPROC_LL_IOCTL,
738         LPROC_LL_OPEN,
739         LPROC_LL_RELEASE,
740         LPROC_LL_MAP,
741         LPROC_LL_LLSEEK,
742         LPROC_LL_FSYNC,
743         LPROC_LL_READDIR,
744         LPROC_LL_SETATTR,
745         LPROC_LL_TRUNC,
746         LPROC_LL_FLOCK,
747         LPROC_LL_GETATTR,
748         LPROC_LL_CREATE,
749         LPROC_LL_LINK,
750         LPROC_LL_UNLINK,
751         LPROC_LL_SYMLINK,
752         LPROC_LL_MKDIR,
753         LPROC_LL_RMDIR,
754         LPROC_LL_MKNOD,
755         LPROC_LL_RENAME,
756         LPROC_LL_STAFS,
757         LPROC_LL_ALLOC_INODE,
758         LPROC_LL_SETXATTR,
759         LPROC_LL_GETXATTR,
760         LPROC_LL_GETXATTR_HITS,
761         LPROC_LL_LISTXATTR,
762         LPROC_LL_REMOVEXATTR,
763         LPROC_LL_INODE_PERM,
764         LPROC_LL_FILE_OPCODES
765 };
766
767 /* llite/dir.c */
768 struct ll_dir_chain {
769 };
770
771 static inline void ll_dir_chain_init(struct ll_dir_chain *chain)
772 {
773 }
774
775 static inline void ll_dir_chain_fini(struct ll_dir_chain *chain)
776 {
777 }
778
779 extern const struct file_operations ll_dir_operations;
780 extern const struct inode_operations ll_dir_inode_operations;
781 #ifdef HAVE_DIR_CONTEXT
782 int ll_dir_read(struct inode *inode, __u64 *pos, struct md_op_data *op_data,
783                 struct dir_context *ctx);
784 #else
785 int ll_dir_read(struct inode *inode, __u64 *pos, struct md_op_data *op_data,
786                 void *cookie, filldir_t filldir);
787 #endif
788 int ll_get_mdt_idx(struct inode *inode);
789 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid);
790 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
791                              __u64 offset, struct ll_dir_chain *chain);
792 void ll_release_page(struct inode *inode, struct page *page, bool remove);
793
794 /* llite/namei.c */
795 extern const struct inode_operations ll_special_inode_operations;
796
797 struct inode *ll_iget(struct super_block *sb, ino_t hash,
798                       struct lustre_md *lic);
799 int ll_test_inode_by_fid(struct inode *inode, void *opaque);
800 int ll_md_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
801                        void *data, int flag);
802 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de);
803 int ll_rmdir_entry(struct inode *dir, char *name, int namelen);
804 void ll_update_times(struct ptlrpc_request *request, struct inode *inode);
805
806 /* llite/rw.c */
807 int ll_writepage(struct page *page, struct writeback_control *wbc);
808 int ll_writepages(struct address_space *, struct writeback_control *wbc);
809 int ll_readpage(struct file *file, struct page *page);
810 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras);
811 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io);
812 struct ll_cl_context *ll_cl_find(struct file *file);
813 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io);
814 void ll_cl_remove(struct file *file, const struct lu_env *env);
815
816 #ifndef MS_HAS_NEW_AOPS
817 extern const struct address_space_operations ll_aops;
818 #else
819 extern const struct address_space_operations_ext ll_aops;
820 #endif
821
822 /* llite/file.c */
823 extern struct file_operations ll_file_operations;
824 extern struct file_operations ll_file_operations_flock;
825 extern struct file_operations ll_file_operations_noflock;
826 extern struct inode_operations ll_file_inode_operations;
827 extern int ll_have_md_lock(struct inode *inode, __u64 *bits,
828                            enum ldlm_mode l_req_mode);
829 extern enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
830                                       struct lustre_handle *lockh, __u64 flags,
831                                       enum ldlm_mode mode);
832
833 int ll_file_open(struct inode *inode, struct file *file);
834 int ll_file_release(struct inode *inode, struct file *file);
835 int ll_release_openhandle(struct dentry *, struct lookup_intent *);
836 int ll_md_real_close(struct inode *inode, fmode_t fmode);
837 extern void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
838                               struct ll_file_data *file, loff_t pos,
839                               size_t count, int rw);
840 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat);
841 struct posix_acl *ll_get_acl(struct inode *inode, int type);
842 int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
843                const char *name, int namelen);
844 int ll_get_fid_by_name(struct inode *parent, const char *name,
845                        int namelen, struct lu_fid *fid, struct inode **inode);
846 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
847 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags);
848 #else
849 # ifndef HAVE_INODE_PERMISION_2ARGS
850 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd);
851 # else
852 int ll_inode_permission(struct inode *inode, int mask);
853 # endif
854 #endif
855
856 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
857                              __u64  flags, struct lov_user_md *lum,
858                              int lum_size);
859 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
860                              struct lov_mds_md **lmm, int *lmm_size,
861                              struct ptlrpc_request **request);
862 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
863                      int set_default);
864 int ll_dir_getstripe(struct inode *inode, void **lmmp,
865                      int *lmm_size, struct ptlrpc_request **request,
866                      u64 valid);
867 #ifdef HAVE_FILE_FSYNC_4ARGS
868 int ll_fsync(struct file *file, loff_t start, loff_t end, int data);
869 #elif defined(HAVE_FILE_FSYNC_2ARGS)
870 int ll_fsync(struct file *file, int data);
871 #else
872 int ll_fsync(struct file *file, struct dentry *dentry, int data);
873 #endif
874 int ll_merge_attr(const struct lu_env *env, struct inode *inode);
875 int ll_fid2path(struct inode *inode, void __user *arg);
876 int ll_data_version(struct inode *inode, __u64 *data_version, int flags);
877 int ll_hsm_release(struct inode *inode);
878 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss);
879
880 /* llite/dcache.c */
881
882 int ll_d_init(struct dentry *de);
883 extern const struct dentry_operations ll_d_ops;
884 void ll_intent_drop_lock(struct lookup_intent *);
885 void ll_intent_release(struct lookup_intent *);
886 void ll_invalidate_aliases(struct inode *);
887 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry);
888 int ll_revalidate_it_finish(struct ptlrpc_request *request,
889                             struct lookup_intent *it, struct dentry *de);
890
891 /* llite/llite_lib.c */
892 extern struct super_operations lustre_super_operations;
893
894 void ll_lli_init(struct ll_inode_info *lli);
895 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt);
896 void ll_put_super(struct super_block *sb);
897 void ll_kill_super(struct super_block *sb);
898 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock);
899 void ll_clear_inode(struct inode *inode);
900 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import);
901 int ll_setattr(struct dentry *de, struct iattr *attr);
902 int ll_statfs(struct dentry *de, struct kstatfs *sfs);
903 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
904                        __u64 max_age, __u32 flags);
905 int ll_update_inode(struct inode *inode, struct lustre_md *md);
906 int ll_read_inode2(struct inode *inode, void *opaque);
907 void ll_delete_inode(struct inode *inode);
908 int ll_iocontrol(struct inode *inode, struct file *file,
909                  unsigned int cmd, unsigned long arg);
910 int ll_flush_ctx(struct inode *inode);
911 void ll_umount_begin(struct super_block *sb);
912 int ll_remount_fs(struct super_block *sb, int *flags, char *data);
913 #ifdef HAVE_SUPEROPS_USE_DENTRY
914 int ll_show_options(struct seq_file *seq, struct dentry *dentry);
915 #else
916 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs);
917 #endif
918 void ll_dirty_page_discard_warn(struct page *page, int ioret);
919 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
920                   struct super_block *, struct lookup_intent *);
921 void lustre_dump_dentry(struct dentry *, int recur);
922 int ll_obd_statfs(struct inode *inode, void __user *arg);
923 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *max_mdsize);
924 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *default_mdsize);
925 int ll_set_default_mdsize(struct ll_sb_info *sbi, int default_mdsize);
926 int ll_process_config(struct lustre_cfg *lcfg);
927
928 enum {
929         LUSTRE_OPC_MKDIR        = 0,
930         LUSTRE_OPC_SYMLINK      = 1,
931         LUSTRE_OPC_MKNOD        = 2,
932         LUSTRE_OPC_CREATE       = 3,
933         LUSTRE_OPC_ANY          = 5,
934 };
935
936 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
937                                       struct inode *i1, struct inode *i2,
938                                       const char *name, size_t namelen,
939                                       __u32 mode, __u32 opc, void *data);
940 void ll_finish_md_op_data(struct md_op_data *op_data);
941 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg);
942 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen);
943 void ll_compute_rootsquash_state(struct ll_sb_info *sbi);
944 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
945                         struct lov_user_md **kbuf);
946 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req);
947
948 /* Compute expected user md size when passing in a md from user space */
949 static inline ssize_t ll_lov_user_md_size(const struct lov_user_md *lum)
950 {
951         switch (lum->lmm_magic) {
952         case LOV_USER_MAGIC_V1:
953                 return sizeof(struct lov_user_md_v1);
954         case LOV_USER_MAGIC_V3:
955                 return sizeof(struct lov_user_md_v3);
956         case LOV_USER_MAGIC_SPECIFIC:
957                 if (lum->lmm_stripe_count > LOV_MAX_STRIPE_COUNT)
958                         return -EINVAL;
959
960                 return lov_user_md_size(lum->lmm_stripe_count,
961                                         LOV_USER_MAGIC_SPECIFIC);
962         }
963
964         return -EINVAL;
965 }
966
967 /* llite/llite_nfs.c */
968 extern struct export_operations lustre_export_operations;
969 __u32 get_uuid2int(const char *name, int len);
970 struct inode *search_inode_for_lustre(struct super_block *sb,
971                                       const struct lu_fid *fid);
972 int ll_dir_get_parent_fid(struct inode *dir, struct lu_fid *parent_fid);
973
974 /* llite/symlink.c */
975 extern struct inode_operations ll_fast_symlink_inode_operations;
976
977 /**
978  * IO arguments for various VFS I/O interfaces.
979  */
980 struct vvp_io_args {
981         /** normal/sendfile/splice */
982         enum vvp_io_subtype via_io_subtype;
983
984         union {
985                 struct {
986                         struct kiocb      *via_iocb;
987                         struct iov_iter   *via_iter;
988                 } normal;
989                 struct {
990                         struct pipe_inode_info  *via_pipe;
991                         unsigned int       via_flags;
992                 } splice;
993         } u;
994 };
995
996 struct ll_cl_context {
997         struct list_head         lcc_list;
998         void                    *lcc_cookie;
999         const struct lu_env     *lcc_env;
1000         struct cl_io            *lcc_io;
1001         struct cl_page          *lcc_page;
1002 };
1003
1004 struct ll_thread_info {
1005         struct iovec            lti_local_iov;
1006         struct vvp_io_args      lti_args;
1007         struct ra_io_arg        lti_ria;
1008         struct kiocb            lti_kiocb;
1009         struct ll_cl_context    lti_io_ctx;
1010 };
1011
1012 extern struct lu_context_key ll_thread_key;
1013
1014 static inline struct ll_thread_info *ll_env_info(const struct lu_env *env)
1015 {
1016         struct ll_thread_info *lti;
1017
1018         lti = lu_context_key_get(&env->le_ctx, &ll_thread_key);
1019         LASSERT(lti != NULL);
1020
1021         return lti;
1022 }
1023
1024 static inline struct vvp_io_args *ll_env_args(const struct lu_env *env,
1025                                               enum vvp_io_subtype type)
1026 {
1027         struct vvp_io_args *via = &ll_env_info(env)->lti_args;
1028
1029         via->via_io_subtype = type;
1030
1031         return via;
1032 }
1033
1034 /* llite/llite_mmap.c */
1035
1036 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last);
1037 int ll_file_mmap(struct file * file, struct vm_area_struct * vma);
1038 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
1039                      unsigned long addr, size_t count);
1040 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
1041                                size_t count);
1042
1043 static inline void ll_invalidate_page(struct page *vmpage)
1044 {
1045         struct address_space *mapping = vmpage->mapping;
1046         loff_t offset = vmpage->index << PAGE_CACHE_SHIFT;
1047
1048         LASSERT(PageLocked(vmpage));
1049         if (mapping == NULL)
1050                 return;
1051
1052         /*
1053          * truncate_complete_page() calls
1054          * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
1055          */
1056         ll_teardown_mmaps(mapping, offset, offset + PAGE_CACHE_SIZE);
1057         truncate_complete_page(mapping, vmpage);
1058 }
1059
1060 #define    ll_s2sbi(sb)        (s2lsi(sb)->lsi_llsbi)
1061
1062 /* don't need an addref as the sb_info should be holding one */
1063 static inline struct obd_export *ll_s2dtexp(struct super_block *sb)
1064 {
1065         return ll_s2sbi(sb)->ll_dt_exp;
1066 }
1067
1068 /* don't need an addref as the sb_info should be holding one */
1069 static inline struct obd_export *ll_s2mdexp(struct super_block *sb)
1070 {
1071         return ll_s2sbi(sb)->ll_md_exp;
1072 }
1073
1074 static inline struct client_obd *sbi2mdc(struct ll_sb_info *sbi)
1075 {
1076         struct obd_device *obd = sbi->ll_md_exp->exp_obd;
1077         if (obd == NULL)
1078                 LBUG();
1079         return &obd->u.cli;
1080 }
1081
1082 // FIXME: replace the name of this with LL_SB to conform to kernel stuff
1083 static inline struct ll_sb_info *ll_i2sbi(struct inode *inode)
1084 {
1085         return ll_s2sbi(inode->i_sb);
1086 }
1087
1088 static inline struct obd_export *ll_i2dtexp(struct inode *inode)
1089 {
1090         return ll_s2dtexp(inode->i_sb);
1091 }
1092
1093 static inline struct obd_export *ll_i2mdexp(struct inode *inode)
1094 {
1095         return ll_s2mdexp(inode->i_sb);
1096 }
1097
1098 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
1099 {
1100         struct lu_fid *fid;
1101
1102         LASSERT(inode != NULL);
1103         fid = &ll_i2info(inode)->lli_fid;
1104
1105         return fid;
1106 }
1107
1108 static inline loff_t ll_file_maxbytes(struct inode *inode)
1109 {
1110         struct cl_object *obj = ll_i2info(inode)->lli_clob;
1111
1112         if (obj == NULL)
1113                 return MAX_LFS_FILESIZE;
1114
1115         return min_t(loff_t, cl_object_maxbytes(obj), MAX_LFS_FILESIZE);
1116 }
1117
1118 /* llite/xattr.c */
1119 int ll_setxattr(struct dentry *dentry, const char *name,
1120                 const void *value, size_t size, int flags);
1121 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
1122                     void *buffer, size_t size);
1123 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size);
1124 int ll_removexattr(struct dentry *dentry, const char *name);
1125
1126 /* llite/remote_perm.c */
1127 extern struct kmem_cache *ll_remote_perm_cachep;
1128 extern struct kmem_cache *ll_rmtperm_hash_cachep;
1129
1130 void free_rmtperm_hash(struct hlist_head *hash);
1131 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm);
1132 int lustre_check_remote_perm(struct inode *inode, int mask);
1133
1134 /**
1135  * Common IO arguments for various VFS I/O interfaces.
1136  */
1137 int cl_sb_init(struct super_block *sb);
1138 int cl_sb_fini(struct super_block *sb);
1139
1140 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len);
1141 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which);
1142
1143 /* llite/llite_rmtacl.c */
1144 #ifdef CONFIG_FS_POSIX_ACL
1145 u64 rce_ops2valid(int ops);
1146 struct rmtacl_ctl_entry *rct_search(struct rmtacl_ctl_table *rct, pid_t key);
1147 int rct_add(struct rmtacl_ctl_table *rct, pid_t key, int ops);
1148 int rct_del(struct rmtacl_ctl_table *rct, pid_t key);
1149 void rct_init(struct rmtacl_ctl_table *rct);
1150 void rct_fini(struct rmtacl_ctl_table *rct);
1151
1152 void ee_free(struct eacl_entry *ee);
1153 int ee_add(struct eacl_table *et, pid_t key, struct lu_fid *fid, int type,
1154            ext_acl_xattr_header *header);
1155 struct eacl_entry *et_search_del(struct eacl_table *et, pid_t key,
1156                                  struct lu_fid *fid, int type);
1157 void et_search_free(struct eacl_table *et, pid_t key);
1158 void et_init(struct eacl_table *et);
1159 void et_fini(struct eacl_table *et);
1160 #endif
1161
1162 /* statahead.c */
1163
1164 #define LL_SA_RPC_MIN           2
1165 #define LL_SA_RPC_DEF           32
1166 #define LL_SA_RPC_MAX           8192
1167
1168 #define LL_SA_CACHE_BIT         5
1169 #define LL_SA_CACHE_SIZE        (1 << LL_SA_CACHE_BIT)
1170 #define LL_SA_CACHE_MASK        (LL_SA_CACHE_SIZE - 1)
1171
1172 /* per inode struct, for dir only */
1173 struct ll_statahead_info {
1174         struct dentry          *sai_dentry;
1175         atomic_t                sai_refcount;   /* when access this struct, hold
1176                                                  * refcount */
1177         unsigned int            sai_max;        /* max ahead of lookup */
1178         __u64                   sai_sent;       /* stat requests sent count */
1179         __u64                   sai_replied;    /* stat requests which received
1180                                                  * reply */
1181         __u64                   sai_index;      /* index of statahead entry */
1182         __u64                   sai_index_wait; /* index of entry which is the
1183                                                  * caller is waiting for */
1184         __u64                   sai_hit;        /* hit count */
1185         __u64                   sai_miss;       /* miss count:
1186                                                  * for "ls -al" case, includes
1187                                                  * hidden dentry miss;
1188                                                  * for "ls -l" case, it does not
1189                                                  * include hidden dentry miss.
1190                                                  * "sai_miss_hidden" is used for
1191                                                  * the later case.
1192                                                  */
1193         unsigned int            sai_consecutive_miss; /* consecutive miss */
1194         unsigned int            sai_miss_hidden;/* "ls -al", but first dentry
1195                                                  * is not a hidden one */
1196         unsigned int            sai_skip_hidden;/* skipped hidden dentry count
1197                                                  */
1198         unsigned int            sai_ls_all:1,   /* "ls -al", do stat-ahead for
1199                                                  * hidden entries */
1200                                 sai_agl_valid:1,/* AGL is valid for the dir */
1201                                 sai_in_readpage:1;/* statahead is in readdir()*/
1202         wait_queue_head_t       sai_waitq;      /* stat-ahead wait queue */
1203         struct ptlrpc_thread    sai_thread;     /* stat-ahead thread */
1204         struct ptlrpc_thread    sai_agl_thread; /* AGL thread */
1205         struct list_head        sai_interim_entries; /* entries which got async
1206                                                       * stat reply, but not
1207                                                       * instantiated */
1208         struct list_head        sai_entries;    /* completed entries */
1209         struct list_head        sai_agls;       /* AGLs to be sent */
1210         struct list_head        sai_cache[LL_SA_CACHE_SIZE];
1211         spinlock_t              sai_cache_lock[LL_SA_CACHE_SIZE];
1212         atomic_t                sai_cache_count; /* entry count in cache */
1213 };
1214
1215 int ll_statahead(struct inode *dir, struct dentry **dentry, bool unplug);
1216 void ll_authorize_statahead(struct inode *dir, void *key);
1217 void ll_deauthorize_statahead(struct inode *dir, void *key);
1218
1219 /* glimpse.c */
1220 blkcnt_t dirty_cnt(struct inode *inode);
1221
1222 int cl_glimpse_size0(struct inode *inode, int agl);
1223 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
1224                     struct inode *inode, struct cl_object *clob, int agl);
1225
1226 static inline int cl_glimpse_size(struct inode *inode)
1227 {
1228         return cl_glimpse_size0(inode, 0);
1229 }
1230
1231 static inline int cl_agl(struct inode *inode)
1232 {
1233         return cl_glimpse_size0(inode, 1);
1234 }
1235
1236 static inline int ll_glimpse_size(struct inode *inode)
1237 {
1238         struct ll_inode_info *lli = ll_i2info(inode);
1239         int rc;
1240
1241         down_read(&lli->lli_glimpse_sem);
1242         rc = cl_glimpse_size(inode);
1243         lli->lli_glimpse_time = cfs_time_current();
1244         up_read(&lli->lli_glimpse_sem);
1245         return rc;
1246 }
1247
1248 /* dentry may statahead when statahead is enabled and current process has opened
1249  * parent directory, and this dentry hasn't accessed statahead cache before */
1250 static inline bool
1251 dentry_may_statahead(struct inode *dir, struct dentry *dentry)
1252 {
1253         struct ll_inode_info  *lli;
1254         struct ll_dentry_data *ldd;
1255
1256         if (ll_i2sbi(dir)->ll_sa_max == 0)
1257                 return false;
1258
1259         lli = ll_i2info(dir);
1260
1261         /* statahead is not allowed for this dir, there may be three causes:
1262          * 1. dir is not opened.
1263          * 2. statahead hit ratio is too low.
1264          * 3. previous stat started statahead thread failed. */
1265         if (!lli->lli_sa_enabled)
1266                 return false;
1267
1268         /* not the same process, don't statahead */
1269         if (lli->lli_opendir_pid != current_pid())
1270                 return false;
1271
1272         /*
1273          * When stating a dentry, kernel may trigger 'revalidate' or 'lookup'
1274          * multiple times, eg. for 'getattr', 'getxattr' and etc.
1275          * For patchless client, lookup intent is not accurate, which may
1276          * misguide statahead. For example:
1277          * The 'revalidate' call for 'getattr' and 'getxattr' of a dentry will
1278          * have the same intent -- IT_GETATTR, while one dentry should access
1279          * statahead cache once, otherwise statahead windows is messed up.
1280          * The solution is as following:
1281          * Assign 'lld_sa_generation' with 'lli_sa_generation' when a dentry
1282          * IT_GETATTR for the first time, and subsequent IT_GETATTR will
1283          * bypass interacting with statahead cache by checking
1284          * 'lld_sa_generation == lli->lli_sa_generation'.
1285          */
1286         ldd = ll_d2d(dentry);
1287         if (ldd != NULL && ldd->lld_sa_generation == lli->lli_sa_generation)
1288                 return false;
1289
1290         return true;
1291 }
1292
1293 /* llite ioctl register support rountine */
1294 enum llioc_iter {
1295         LLIOC_CONT = 0,
1296         LLIOC_STOP
1297 };
1298
1299 #define LLIOC_MAX_CMD           256
1300
1301 /*
1302  * Rules to write a callback function:
1303  *
1304  * Parameters:
1305  *  @magic: Dynamic ioctl call routine will feed this vaule with the pointer
1306  *      returned to ll_iocontrol_register.  Callback functions should use this
1307  *      data to check the potential collasion of ioctl cmd. If collasion is
1308  *      found, callback function should return LLIOC_CONT.
1309  *  @rcp: The result of ioctl command.
1310  *
1311  *  Return values:
1312  *      If @magic matches the pointer returned by ll_iocontrol_data, the
1313  *      callback should return LLIOC_STOP; return LLIOC_STOP otherwise.
1314  */
1315 typedef enum llioc_iter (*llioc_callback_t)(struct inode *inode,
1316                 struct file *file, unsigned int cmd, unsigned long arg,
1317                 void *magic, int *rcp);
1318
1319 /* export functions */
1320 /* Register ioctl block dynamatically for a regular file.
1321  *
1322  * @cmd: the array of ioctl command set
1323  * @count: number of commands in the @cmd
1324  * @cb: callback function, it will be called if an ioctl command is found to
1325  *      belong to the command list @cmd.
1326  *
1327  * Return vaule:
1328  *      A magic pointer will be returned if success;
1329  *      otherwise, NULL will be returned.
1330  * */
1331 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd);
1332 void ll_iocontrol_unregister(void *magic);
1333
1334 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
1335                        enum cl_fsync_mode mode, int ignore_layout);
1336
1337 /** direct write pages */
1338 struct ll_dio_pages {
1339         /** page array to be written. we don't support
1340          * partial pages except the last one. */
1341         struct page **ldp_pages;
1342         /* offset of each page */
1343         loff_t       *ldp_offsets;
1344         /** if ldp_offsets is NULL, it means a sequential
1345          * pages to be written, then this is the file offset
1346          * of the * first page. */
1347         loff_t        ldp_start_offset;
1348         /** how many bytes are to be written. */
1349         size_t        ldp_size;
1350         /** # of pages in the array. */
1351         int           ldp_nr;
1352 };
1353
1354 extern ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
1355                                   int rw, struct inode *inode,
1356                                   struct ll_dio_pages *pv);
1357
1358 static inline int ll_file_nolock(const struct file *file)
1359 {
1360         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1361         struct inode *inode = file->f_path.dentry->d_inode;
1362
1363         LASSERT(fd != NULL);
1364         return ((fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
1365                 (ll_i2sbi(inode)->ll_flags & LL_SBI_NOLCK));
1366 }
1367
1368 static inline void ll_set_lock_data(struct obd_export *exp, struct inode *inode,
1369                                     struct lookup_intent *it, __u64 *bits)
1370 {
1371         if (!it->d.lustre.it_lock_set) {
1372                 struct lustre_handle handle;
1373
1374                 /* If this inode is a remote object, it will get two
1375                  * separate locks in different namespaces, Master MDT,
1376                  * where the name entry is, will grant LOOKUP lock,
1377                  * remote MDT, where the object is, will grant
1378                  * UPDATE|PERM lock. The inode will be attched to both
1379                  * LOOKUP and PERM locks, so revoking either locks will
1380                  * case the dcache being cleared */
1381                 if (it->d.lustre.it_remote_lock_mode) {
1382                         handle.cookie = it->d.lustre.it_remote_lock_handle;
1383                         CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID
1384                                "(%p) for remote lock "LPX64"\n",
1385                                PFID(ll_inode2fid(inode)), inode,
1386                                handle.cookie);
1387                         md_set_lock_data(exp, &handle.cookie, inode, NULL);
1388                 }
1389
1390                 handle.cookie = it->d.lustre.it_lock_handle;
1391
1392                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)"
1393                        " for lock "LPX64"\n",
1394                        PFID(ll_inode2fid(inode)), inode, handle.cookie);
1395
1396                 md_set_lock_data(exp, &handle.cookie, inode,
1397                                  &it->d.lustre.it_lock_bits);
1398                 it->d.lustre.it_lock_set = 1;
1399         }
1400
1401         if (bits != NULL)
1402                 *bits = it->d.lustre.it_lock_bits;
1403 }
1404
1405 static inline void ll_lock_dcache(struct inode *inode)
1406 {
1407 #ifdef HAVE_DCACHE_LOCK
1408         spin_lock(&dcache_lock);
1409 #else
1410         spin_lock(&inode->i_lock);
1411 #endif
1412 }
1413
1414 static inline void ll_unlock_dcache(struct inode *inode)
1415 {
1416 #ifdef HAVE_DCACHE_LOCK
1417         spin_unlock(&dcache_lock);
1418 #else
1419         spin_unlock(&inode->i_lock);
1420 #endif
1421 }
1422
1423 static inline int d_lustre_invalid(const struct dentry *dentry)
1424 {
1425         struct ll_dentry_data *lld = ll_d2d(dentry);
1426
1427         return (lld == NULL) || lld->lld_invalid;
1428 }
1429
1430 static inline void __d_lustre_invalidate(struct dentry *dentry)
1431 {
1432         struct ll_dentry_data *lld = ll_d2d(dentry);
1433
1434         if (lld != NULL)
1435                 lld->lld_invalid = 1;
1436 }
1437
1438 /*
1439  * Mark dentry INVALID, if dentry refcount is zero (this is normally case for
1440  * ll_md_blocking_ast), unhash this dentry, and let dcache to reclaim it later;
1441  * else dput() of the last refcount will unhash this dentry and kill it.
1442  */
1443 static inline void d_lustre_invalidate(struct dentry *dentry, int nested)
1444 {
1445         CDEBUG(D_DENTRY, "invalidate dentry %.*s (%p) parent %p inode %p "
1446                "refc %d\n", dentry->d_name.len, dentry->d_name.name, dentry,
1447                dentry->d_parent, dentry->d_inode, ll_d_count(dentry));
1448
1449         spin_lock_nested(&dentry->d_lock,
1450                          nested ? DENTRY_D_LOCK_NESTED : DENTRY_D_LOCK_NORMAL);
1451         __d_lustre_invalidate(dentry);
1452         /*
1453          * We should be careful about dentries created by d_obtain_alias().
1454          * These dentries are not put in the dentry tree, instead they are
1455          * linked to sb->s_anon through dentry->d_hash.
1456          * shrink_dcache_for_umount() shrinks the tree and sb->s_anon list.
1457          * If we unhashed such a dentry, unmount would not be able to find
1458          * it and busy inodes would be reported.
1459          */
1460         if (ll_d_count(dentry) == 0 && !(dentry->d_flags & DCACHE_DISCONNECTED))
1461                 __d_drop(dentry);
1462         spin_unlock(&dentry->d_lock);
1463 }
1464
1465 static inline void d_lustre_revalidate(struct dentry *dentry)
1466 {
1467         spin_lock(&dentry->d_lock);
1468         LASSERT(ll_d2d(dentry) != NULL);
1469         ll_d2d(dentry)->lld_invalid = 0;
1470         spin_unlock(&dentry->d_lock);
1471 }
1472
1473 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf);
1474 int ll_layout_refresh(struct inode *inode, __u32 *gen);
1475 int ll_layout_restore(struct inode *inode, loff_t start, __u64 length);
1476
1477 int ll_xattr_init(void);
1478 void ll_xattr_fini(void);
1479
1480 int ll_getxattr_common(struct inode *inode, const char *name,
1481                        void *buffer, size_t size, __u64 valid);
1482 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1483                     struct cl_page *page, enum cl_req_type crt);
1484
1485 int ll_getparent(struct file *file, struct getparent __user *arg);
1486
1487 /* lcommon_cl.c */
1488 int cl_setattr_ost(struct cl_object *obj, const struct iattr *attr,
1489                    unsigned int attr_flags);
1490
1491 extern struct lu_env *cl_inode_fini_env;
1492 extern int cl_inode_fini_refcheck;
1493
1494 int cl_file_inode_init(struct inode *inode, struct lustre_md *md);
1495 void cl_inode_fini(struct inode *inode);
1496
1497 u64 cl_fid_build_ino(const struct lu_fid *fid, int api32);
1498 u32 cl_fid_build_gen(const struct lu_fid *fid);
1499
1500 #endif /* LLITE_INTERNAL_H */