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