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