Whamcloud - gitweb
d8f64b12c4d03769005386dce945cdee789454d3
[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, 2013, 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
44 /* for struct cl_lock_descr and struct cl_io */
45 #include <cl_object.h>
46 #include <lclient.h>
47 #include <lustre_mdc.h>
48 #include <linux/lustre_intent.h>
49 #include <linux/compat.h>
50
51 #ifndef FMODE_EXEC
52 #define FMODE_EXEC 0
53 #endif
54
55 #ifndef VM_FAULT_RETRY
56 #define VM_FAULT_RETRY 0
57 #endif
58
59 /* Kernel 3.1 kills LOOKUP_CONTINUE, LOOKUP_PARENT is equivalent to it.
60  * seem kernel commit 49084c3bb2055c401f3493c13edae14d49128ca0 */
61 #ifndef LOOKUP_CONTINUE
62 #define LOOKUP_CONTINUE LOOKUP_PARENT
63 #endif
64
65 /** Only used on client-side for indicating the tail of dir hash/offset. */
66 #define LL_DIR_END_OFF          0x7fffffffffffffffULL
67 #define LL_DIR_END_OFF_32BIT    0x7fffffffUL
68
69 #define LL_IT2STR(it) ((it) ? ldlm_it2str((it)->it_op) : "0")
70 #define LUSTRE_FPRIVATE(file) ((file)->private_data)
71
72 struct ll_dentry_data {
73         int                             lld_cwd_count;
74         int                             lld_mnt_count;
75         struct obd_client_handle        lld_cwd_och;
76         struct obd_client_handle        lld_mnt_och;
77         struct lookup_intent            *lld_it;
78         unsigned int                    lld_sa_generation;
79         unsigned int                    lld_invalid:1;
80         struct rcu_head                 lld_rcu_head;
81 };
82
83 #define ll_d2d(de) ((struct ll_dentry_data*)((de)->d_fsdata))
84
85 extern struct file_operations ll_pgcache_seq_fops;
86
87 #define LLI_INODE_MAGIC                 0x111d0de5
88 #define LLI_INODE_DEAD                  0xdeadd00d
89
90 /* remote client permission cache */
91 #define REMOTE_PERM_HASHSIZE 16
92
93 struct ll_getname_data {
94         char            *lgd_name;      /* points to a buffer with NAME_MAX+1 size */
95         struct lu_fid    lgd_fid;       /* target fid we are looking for */
96         int              lgd_found;     /* inode matched? */
97 };
98
99 /* llite setxid/access permission for user on remote client */
100 struct ll_remote_perm {
101         cfs_hlist_node_t        lrp_list;
102         uid_t                   lrp_uid;
103         gid_t                   lrp_gid;
104         uid_t                   lrp_fsuid;
105         gid_t                   lrp_fsgid;
106         int                     lrp_access_perm; /* MAY_READ/WRITE/EXEC, this
107                                                     is access permission with
108                                                     lrp_fsuid/lrp_fsgid. */
109 };
110
111 enum lli_flags {
112         /* MDS has an authority for the Size-on-MDS attributes. */
113         LLIF_MDS_SIZE_LOCK      = (1 << 0),
114         /* Epoch close is postponed. */
115         LLIF_EPOCH_PENDING      = (1 << 1),
116         /* DONE WRITING is allowed. */
117         LLIF_DONE_WRITING       = (1 << 2),
118         /* Sizeon-on-MDS attributes are changed. An attribute update needs to
119          * be sent to MDS. */
120         LLIF_SOM_DIRTY          = (1 << 3),
121         /* File is contented */
122         LLIF_CONTENDED          = (1 << 4),
123         /* Truncate uses server lock for this file */
124         LLIF_SRVLOCK            = (1 << 5),
125         /* File data is modified. */
126         LLIF_DATA_MODIFIED      = (1 << 6),
127         /* File is being restored */
128         LLIF_FILE_RESTORING     = (1 << 7),
129         /* Xattr cache is attached to the file */
130         LLIF_XATTR_CACHE        = (1 << 8),
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         cfs_hlist_head_t                *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         /* Parent fid for accessing default stripe data on parent directory
147          * for allocating OST objects after a mknod() and later open-by-FID. */
148         struct lu_fid                   lli_pfid;
149
150         cfs_list_t                      lli_close_list;
151         cfs_list_t                      lli_oss_capas;
152         /* open count currently used by capability only, indicate whether
153          * capability needs renewal */
154         cfs_atomic_t                    lli_open_count;
155         struct obd_capa                *lli_mds_capa;
156         cfs_time_t                      lli_rmtperm_time;
157
158         /* handle is to be sent to MDS later on done_writing and setattr.
159          * Open handle data are needed for the recovery to reconstruct
160          * the inode state on the MDS. XXX: recovery is not ready yet. */
161         struct obd_client_handle       *lli_pending_och;
162
163         /* We need all three because every inode may be opened in different
164          * modes */
165         struct obd_client_handle       *lli_mds_read_och;
166         struct obd_client_handle       *lli_mds_write_och;
167         struct obd_client_handle       *lli_mds_exec_och;
168         __u64                           lli_open_fd_read_count;
169         __u64                           lli_open_fd_write_count;
170         __u64                           lli_open_fd_exec_count;
171         /* Protects access to och pointers and their usage counters */
172         struct mutex                    lli_och_mutex;
173
174         struct inode                    lli_vfs_inode;
175
176         /* the most recent timestamps obtained from mds */
177         struct ost_lvb                  lli_lvb;
178         spinlock_t                      lli_agl_lock;
179
180         /* Try to make the d::member and f::member are aligned. Before using
181          * these members, make clear whether it is directory or not. */
182         union {
183                 /* for directory */
184                 struct {
185                         /* serialize normal readdir and statahead-readdir. */
186                         struct mutex                    d_readdir_mutex;
187
188                         /* metadata statahead */
189                         /* since parent-child threads can share the same @file
190                          * struct, "opendir_key" is the token when dir close for
191                          * case of parent exit before child -- it is me should
192                          * cleanup the dir readahead. */
193                         void                           *d_opendir_key;
194                         struct ll_statahead_info       *d_sai;
195                         struct posix_acl               *d_def_acl;
196                         /* protect statahead stuff. */
197                         spinlock_t                      d_sa_lock;
198                         /* "opendir_pid" is the token when lookup/revalid
199                          * -- I am the owner of dir statahead. */
200                         pid_t                           d_opendir_pid;
201                 } d;
202
203 #define lli_readdir_mutex       u.d.d_readdir_mutex
204 #define lli_opendir_key         u.d.d_opendir_key
205 #define lli_sai                 u.d.d_sai
206 #define lli_def_acl             u.d.d_def_acl
207 #define lli_sa_lock             u.d.d_sa_lock
208 #define lli_opendir_pid         u.d.d_opendir_pid
209
210                 /* for non-directory */
211                 struct {
212                         struct semaphore                f_size_sem;
213                         void                            *f_size_sem_owner;
214                         char                            *f_symlink_name;
215                         __u64                           f_maxbytes;
216                         /*
217                          * struct rw_semaphore {
218                          *    signed long       count;     // align d.d_def_acl
219                          *    spinlock_t        wait_lock; // align d.d_sa_lock
220                          *    struct list_head wait_list;
221                          * }
222                          */
223                         struct rw_semaphore             f_trunc_sem;
224                         struct mutex                    f_write_mutex;
225
226                         struct rw_semaphore             f_glimpse_sem;
227                         cfs_time_t                      f_glimpse_time;
228                         cfs_list_t                      f_agl_list;
229                         __u64                           f_agl_index;
230
231                         /* for writepage() only to communicate to fsync */
232                         int                             f_async_rc;
233
234                         /* volatile file criteria is based on file name, this
235                          * flag is used to keep the test result, so the strcmp
236                          * is done only once
237                          */
238                         bool                            f_volatile;
239                         /*
240                          * whenever a process try to read/write the file, the
241                          * jobid of the process will be saved here, and it'll
242                          * be packed into the write PRC when flush later.
243                          *
244                          * so the read/write statistics for jobid will not be
245                          * accurate if the file is shared by different jobs.
246                          */
247                         char                     f_jobid[JOBSTATS_JOBID_SIZE];
248                 } f;
249
250 #define lli_size_sem            u.f.f_size_sem
251 #define lli_size_sem_owner      u.f.f_size_sem_owner
252 #define lli_symlink_name        u.f.f_symlink_name
253 #define lli_maxbytes            u.f.f_maxbytes
254 #define lli_trunc_sem           u.f.f_trunc_sem
255 #define lli_write_mutex         u.f.f_write_mutex
256 #define lli_glimpse_sem         u.f.f_glimpse_sem
257 #define lli_glimpse_time        u.f.f_glimpse_time
258 #define lli_agl_list            u.f.f_agl_list
259 #define lli_agl_index           u.f.f_agl_index
260 #define lli_async_rc            u.f.f_async_rc
261 #define lli_jobid               u.f.f_jobid
262 #define lli_volatile            u.f.f_volatile
263
264         } u;
265
266         /* XXX: For following frequent used members, although they maybe special
267          *      used for non-directory object, it is some time-wasting to check
268          *      whether the object is directory or not before using them. On the
269          *      other hand, currently, sizeof(f) > sizeof(d), it cannot reduce
270          *      the "ll_inode_info" size even if moving those members into u.f.
271          *      So keep them out side.
272          *
273          *      In the future, if more members are added only for directory,
274          *      some of the following members can be moved into u.f.
275          */
276         bool                            lli_has_smd;
277         struct cl_object               *lli_clob;
278
279         /* mutex to request for layout lock exclusively. */
280         struct mutex                    lli_layout_mutex;
281         /* valid only inside LAYOUT ibits lock, protected by lli_layout_mutex */
282         __u32                           lli_layout_gen;
283
284         struct rw_semaphore             lli_xattrs_list_rwsem;
285         struct mutex                    lli_xattrs_enq_lock;
286         struct list_head                lli_xattrs; /* ll_xattr_entry->xe_list */
287 };
288
289 int ll_xattr_cache_destroy(struct inode *inode);
290
291 int ll_xattr_cache_get(struct inode *inode,
292                         const char *name,
293                         char *buffer,
294                         size_t size,
295                         __u64 valid);
296
297 /*
298  * Locking to guarantee consistency of non-atomic updates to long long i_size,
299  * consistency between file size and KMS.
300  *
301  * Implemented by ->lli_size_sem and ->lsm_lock, nested in that order.
302  */
303
304 void ll_inode_size_lock(struct inode *inode);
305 void ll_inode_size_unlock(struct inode *inode);
306
307 // FIXME: replace the name of this with LL_I to conform to kernel stuff
308 // static inline struct ll_inode_info *LL_I(struct inode *inode)
309 static inline struct ll_inode_info *ll_i2info(struct inode *inode)
310 {
311         return container_of(inode, struct ll_inode_info, lli_vfs_inode);
312 }
313
314 /* default to about 40meg of readahead on a given system.  That much tied
315  * up in 512k readahead requests serviced at 40ms each is about 1GB/s. */
316 #define SBI_DEFAULT_READAHEAD_MAX (40UL << (20 - PAGE_CACHE_SHIFT))
317
318 /* default to read-ahead full files smaller than 2MB on the second read */
319 #define SBI_DEFAULT_READAHEAD_WHOLE_MAX (2UL << (20 - PAGE_CACHE_SHIFT))
320
321 enum ra_stat {
322         RA_STAT_HIT = 0,
323         RA_STAT_MISS,
324         RA_STAT_DISTANT_READPAGE,
325         RA_STAT_MISS_IN_WINDOW,
326         RA_STAT_FAILED_GRAB_PAGE,
327         RA_STAT_FAILED_MATCH,
328         RA_STAT_DISCARDED,
329         RA_STAT_ZERO_LEN,
330         RA_STAT_ZERO_WINDOW,
331         RA_STAT_EOF,
332         RA_STAT_MAX_IN_FLIGHT,
333         RA_STAT_WRONG_GRAB_PAGE,
334         _NR_RA_STAT,
335 };
336
337 struct ll_ra_info {
338         cfs_atomic_t              ra_cur_pages;
339         unsigned long             ra_max_pages;
340         unsigned long             ra_max_pages_per_file;
341         unsigned long             ra_max_read_ahead_whole_pages;
342 };
343
344 /* ra_io_arg will be filled in the beginning of ll_readahead with
345  * ras_lock, then the following ll_read_ahead_pages will read RA
346  * pages according to this arg, all the items in this structure are
347  * counted by page index.
348  */
349 struct ra_io_arg {
350         unsigned long ria_start;  /* start offset of read-ahead*/
351         unsigned long ria_end;    /* end offset of read-ahead*/
352         /* If stride read pattern is detected, ria_stoff means where
353          * stride read is started. Note: for normal read-ahead, the
354          * value here is meaningless, and also it will not be accessed*/
355         pgoff_t ria_stoff;
356         /* ria_length and ria_pages are the length and pages length in the
357          * stride I/O mode. And they will also be used to check whether
358          * it is stride I/O read-ahead in the read-ahead pages*/
359         unsigned long ria_length;
360         unsigned long ria_pages;
361 };
362
363 /* LL_HIST_MAX=32 causes an overflow */
364 #define LL_HIST_MAX 28
365 #define LL_HIST_START 12 /* buckets start at 2^12 = 4k */
366 #define LL_PROCESS_HIST_MAX 10
367 struct per_process_info {
368         pid_t pid;
369         struct obd_histogram pp_r_hist;
370         struct obd_histogram pp_w_hist;
371 };
372
373 /* pp_extents[LL_PROCESS_HIST_MAX] will hold the combined process info */
374 struct ll_rw_extents_info {
375         struct per_process_info pp_extents[LL_PROCESS_HIST_MAX + 1];
376 };
377
378 #define LL_OFFSET_HIST_MAX 100
379 struct ll_rw_process_info {
380         pid_t                     rw_pid;
381         int                       rw_op;
382         loff_t                    rw_range_start;
383         loff_t                    rw_range_end;
384         loff_t                    rw_last_file_pos;
385         loff_t                    rw_offset;
386         size_t                    rw_smallest_extent;
387         size_t                    rw_largest_extent;
388         struct ll_file_data      *rw_last_file;
389 };
390
391 enum stats_track_type {
392         STATS_TRACK_ALL = 0,  /* track all processes */
393         STATS_TRACK_PID,      /* track process with this pid */
394         STATS_TRACK_PPID,     /* track processes with this ppid */
395         STATS_TRACK_GID,      /* track processes with this gid */
396         STATS_TRACK_LAST,
397 };
398
399 /* flags for sbi->ll_flags */
400 #define LL_SBI_NOLCK             0x01 /* DLM locking disabled (directio-only) */
401 #define LL_SBI_CHECKSUM          0x02 /* checksum each page as it's written */
402 #define LL_SBI_FLOCK             0x04
403 #define LL_SBI_USER_XATTR        0x08 /* support user xattr */
404 #define LL_SBI_ACL               0x10 /* support ACL */
405 #define LL_SBI_RMT_CLIENT        0x40 /* remote client */
406 #define LL_SBI_MDS_CAPA          0x80 /* support mds capa */
407 #define LL_SBI_OSS_CAPA         0x100 /* support oss capa */
408 #define LL_SBI_LOCALFLOCK       0x200 /* Local flocks support by kernel */
409 #define LL_SBI_LRU_RESIZE       0x400 /* lru resize support */
410 #define LL_SBI_LAZYSTATFS       0x800 /* lazystatfs mount option */
411 #define LL_SBI_SOM_PREVIEW     0x1000 /* SOM preview mount option */
412 #define LL_SBI_32BIT_API       0x2000 /* generate 32 bit inodes. */
413 #define LL_SBI_64BIT_HASH      0x4000 /* support 64-bits dir hash/offset */
414 #define LL_SBI_AGL_ENABLED     0x8000 /* enable agl */
415 #define LL_SBI_VERBOSE        0x10000 /* verbose mount/umount */
416 #define LL_SBI_LAYOUT_LOCK    0x20000 /* layout lock support */
417 #define LL_SBI_USER_FID2PATH  0x40000 /* allow fid2path by unprivileged users */
418 #define LL_SBI_XATTR_CACHE    0x80000 /* support for xattr cache */
419
420 #define LL_SBI_FLAGS {  \
421         "nolck",        \
422         "checksum",     \
423         "flock",        \
424         "xattr",        \
425         "acl",          \
426         "???",          \
427         "rmt_client",   \
428         "mds_capa",     \
429         "oss_capa",     \
430         "flock",        \
431         "lru_resize",   \
432         "lazy_statfs",  \
433         "som",          \
434         "32bit_api",    \
435         "64bit_hash",   \
436         "agl",          \
437         "verbose",      \
438         "layout",       \
439         "user_fid2path",\
440         "xattr",        \
441 }
442
443 /* default value for ll_sb_info->contention_time */
444 #define SBI_DEFAULT_CONTENTION_SECONDS     60
445 /* default value for lockless_truncate_enable */
446 #define SBI_DEFAULT_LOCKLESS_TRUNCATE_ENABLE 1
447 #define RCE_HASHES      32
448
449 struct rmtacl_ctl_entry {
450         cfs_list_t       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         cfs_list_t      rct_entries[RCE_HASHES];
458 };
459
460 #define EE_HASHES       32
461
462 struct eacl_entry {
463         cfs_list_t            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         cfs_list_t      et_entries[EE_HASHES];
473 };
474
475 struct ll_sb_info {
476         cfs_list_t                ll_list;
477         /* this protects pglist and ra_info.  It isn't safe to
478          * grab from interrupt contexts */
479         spinlock_t                ll_lock;
480         spinlock_t                ll_pp_extent_lock; /* pp_extent entry*/
481         spinlock_t                ll_process_lock; /* ll_rw_process_info */
482         struct obd_uuid           ll_sb_uuid;
483         struct obd_export        *ll_md_exp;
484         struct obd_export        *ll_dt_exp;
485         struct proc_dir_entry*    ll_proc_root;
486         struct lu_fid             ll_root_fid; /* root object fid */
487
488         int                       ll_flags;
489         unsigned int              ll_umounting:1,
490                                   ll_xattr_cache_enabled:1;
491         cfs_list_t                ll_conn_chain; /* per-conn chain of SBs */
492         struct lustre_client_ocd  ll_lco;
493
494         cfs_list_t                ll_orphan_dentry_list; /*please don't ask -p*/
495         struct ll_close_queue    *ll_lcq;
496
497         struct lprocfs_stats     *ll_stats; /* lprocfs stats counter */
498
499         struct cl_client_cache    ll_cache;
500
501         struct lprocfs_stats     *ll_ra_stats;
502
503         struct ll_ra_info         ll_ra_info;
504         unsigned int              ll_namelen;
505         struct file_operations   *ll_fop;
506
507         /* =0 - hold lock over whole read/write
508          * >0 - max. chunk to be read/written w/o lock re-acquiring */
509         unsigned long             ll_max_rw_chunk;
510         unsigned int              ll_md_brw_size; /* used by readdir */
511
512         struct lu_site           *ll_site;
513         struct cl_device         *ll_cl;
514         /* Statistics */
515         struct ll_rw_extents_info ll_rw_extents_info;
516         int                       ll_extent_process_count;
517         struct ll_rw_process_info ll_rw_process_info[LL_PROCESS_HIST_MAX];
518         unsigned int              ll_offset_process_count;
519         struct ll_rw_process_info ll_rw_offset_info[LL_OFFSET_HIST_MAX];
520         unsigned int              ll_rw_offset_entry_count;
521         int                       ll_stats_track_id;
522         enum stats_track_type     ll_stats_track_type;
523         int                       ll_rw_stats_on;
524
525         /* metadata stat-ahead */
526         unsigned int              ll_sa_max;     /* max statahead RPCs */
527         atomic_t                  ll_sa_total;   /* statahead thread started
528                                                   * count */
529         atomic_t                  ll_sa_wrong;   /* statahead thread stopped for
530                                                   * low hit ratio */
531         atomic_t                  ll_agl_total;  /* AGL thread started count */
532
533         dev_t                     ll_sdev_orig; /* save s_dev before assign for
534                                                  * clustred nfs */
535         struct rmtacl_ctl_table   ll_rct;
536         struct eacl_table         ll_et;
537 };
538
539 #define LL_DEFAULT_MAX_RW_CHUNK      (32 * 1024 * 1024)
540
541 struct ll_ra_read {
542         pgoff_t             lrr_start;
543         pgoff_t             lrr_count;
544         struct task_struct *lrr_reader;
545         cfs_list_t          lrr_linkage;
546 };
547
548 /*
549  * per file-descriptor read-ahead data.
550  */
551 struct ll_readahead_state {
552         spinlock_t  ras_lock;
553         /*
554          * index of the last page that read(2) needed and that wasn't in the
555          * cache. Used by ras_update() to detect seeks.
556          *
557          * XXX nikita: if access seeks into cached region, Lustre doesn't see
558          * this.
559          */
560         unsigned long   ras_last_readpage;
561         /*
562          * number of pages read after last read-ahead window reset. As window
563          * is reset on each seek, this is effectively a number of consecutive
564          * accesses. Maybe ->ras_accessed_in_window is better name.
565          *
566          * XXX nikita: window is also reset (by ras_update()) when Lustre
567          * believes that memory pressure evicts read-ahead pages. In that
568          * case, it probably doesn't make sense to expand window to
569          * PTLRPC_MAX_BRW_PAGES on the third access.
570          */
571         unsigned long   ras_consecutive_pages;
572         /*
573          * number of read requests after the last read-ahead window reset
574          * As window is reset on each seek, this is effectively the number
575          * on consecutive read request and is used to trigger read-ahead.
576          */
577         unsigned long   ras_consecutive_requests;
578         /*
579          * Parameters of current read-ahead window. Handled by
580          * ras_update(). On the initial access to the file or after a seek,
581          * window is reset to 0. After 3 consecutive accesses, window is
582          * expanded to PTLRPC_MAX_BRW_PAGES. Afterwards, window is enlarged by
583          * PTLRPC_MAX_BRW_PAGES chunks up to ->ra_max_pages.
584          */
585         unsigned long   ras_window_start, ras_window_len;
586         /*
587          * Where next read-ahead should start at. This lies within read-ahead
588          * window. Read-ahead window is read in pieces rather than at once
589          * because: 1. lustre limits total number of pages under read-ahead by
590          * ->ra_max_pages (see ll_ra_count_get()), 2. client cannot read pages
591          * not covered by DLM lock.
592          */
593         unsigned long   ras_next_readahead;
594         /*
595          * Total number of ll_file_read requests issued, reads originating
596          * due to mmap are not counted in this total.  This value is used to
597          * trigger full file read-ahead after multiple reads to a small file.
598          */
599         unsigned long   ras_requests;
600         /*
601          * Page index with respect to the current request, these value
602          * will not be accurate when dealing with reads issued via mmap.
603          */
604         unsigned long   ras_request_index;
605         /*
606          * list of struct ll_ra_read's one per read(2) call current in
607          * progress against this file descriptor. Used by read-ahead code,
608          * protected by ->ras_lock.
609          */
610         cfs_list_t      ras_read_beads;
611         /*
612          * The following 3 items are used for detecting the stride I/O
613          * mode.
614          * In stride I/O mode,
615          * ...............|-----data-----|****gap*****|--------|******|....
616          *    offset      |-stride_pages-|-stride_gap-|
617          * ras_stride_offset = offset;
618          * ras_stride_length = stride_pages + stride_gap;
619          * ras_stride_pages = stride_pages;
620          * Note: all these three items are counted by pages.
621          */
622         unsigned long   ras_stride_length;
623         unsigned long   ras_stride_pages;
624         pgoff_t         ras_stride_offset;
625         /*
626          * number of consecutive stride request count, and it is similar as
627          * ras_consecutive_requests, but used for stride I/O mode.
628          * Note: only more than 2 consecutive stride request are detected,
629          * stride read-ahead will be enable
630          */
631         unsigned long   ras_consecutive_stride_requests;
632 };
633
634 extern struct kmem_cache *ll_file_data_slab;
635 struct lustre_handle;
636 struct ll_file_data {
637         struct ll_readahead_state fd_ras;
638         struct ccc_grouplock fd_grouplock;
639         __u64 lfd_pos;
640         __u32 fd_flags;
641         fmode_t fd_omode;
642         /* openhandle if lease exists for this file.
643          * Borrow lli->lli_och_mutex to protect assignment */
644         struct obd_client_handle *fd_lease_och;
645         struct obd_client_handle *fd_och;
646         struct file *fd_file;
647         /* Indicate whether need to report failure when close.
648          * true: failure is known, not report again.
649          * false: unknown failure, should report. */
650         bool fd_write_failed;
651 };
652
653 struct lov_stripe_md;
654
655 extern spinlock_t inode_lock;
656
657 extern struct proc_dir_entry *proc_lustre_fs_root;
658
659 static inline struct inode *ll_info2i(struct ll_inode_info *lli)
660 {
661         return &lli->lli_vfs_inode;
662 }
663
664 struct it_cb_data {
665         struct inode  *icbd_parent;
666         struct dentry **icbd_childp;
667         obd_id        hash;
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 #else
678         return unlikely(
679 #ifdef CONFIG_COMPAT
680                 is_compat_task() ||
681 #endif
682                 (sbi->ll_flags & LL_SBI_32BIT_API)
683         );
684 #endif
685 }
686
687 #define LLAP_MAGIC 98764321
688
689 extern struct kmem_cache *ll_async_page_slab;
690 extern size_t ll_async_page_slab_size;
691
692 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar);
693 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar);
694 struct ll_ra_read *ll_ra_read_get(struct file *f);
695
696 /* llite/lproc_llite.c */
697 #ifdef LPROCFS
698 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
699                                 struct super_block *sb, char *osc, char *mdc);
700 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi);
701 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count);
702 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars);
703 #else
704 static inline int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
705                         struct super_block *sb, char *osc, char *mdc){return 0;}
706 static inline void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi) {}
707 static void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count) {}
708 static void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
709 {
710         memset(lvars, 0, sizeof(*lvars));
711 }
712 #endif
713
714
715 /* llite/dir.c */
716 void ll_release_page(struct page *page, int remove);
717 extern struct file_operations ll_dir_operations;
718 extern struct inode_operations ll_dir_inode_operations;
719 struct page *ll_get_dir_page(struct inode *dir, __u64 hash,
720                              struct ll_dir_chain *chain);
721 int ll_dir_read(struct inode *inode, __u64 *_pos, void *cookie,
722                 filldir_t filldir);
723
724 int ll_get_mdt_idx(struct inode *inode);
725 /* llite/namei.c */
726 int ll_objects_destroy(struct ptlrpc_request *request,
727                        struct inode *dir);
728 struct inode *ll_iget(struct super_block *sb, ino_t hash,
729                       struct lustre_md *lic);
730 int ll_md_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
731                        void *data, int flag);
732 #ifndef HAVE_IOP_ATOMIC_OPEN
733 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
734                                         int lookup_flags);
735 #endif
736 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de);
737 int ll_rmdir_entry(struct inode *dir, char *name, int namelen);
738
739 /* llite/rw.c */
740 int ll_prepare_write(struct file *, struct page *, unsigned from, unsigned to);
741 int ll_commit_write(struct file *, struct page *, unsigned from, unsigned to);
742 int ll_writepage(struct page *page, struct writeback_control *wbc);
743 int ll_writepages(struct address_space *, struct writeback_control *wbc);
744 void ll_removepage(struct page *page);
745 int ll_readpage(struct file *file, struct page *page);
746 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras);
747 int ll_file_punch(struct inode *, loff_t, int);
748 ssize_t ll_file_lockless_io(struct file *, char *, size_t, loff_t *, int);
749 void ll_clear_file_contended(struct inode*);
750 int ll_sync_page_range(struct inode *, struct address_space *, loff_t, size_t);
751 int ll_readahead(const struct lu_env *env, struct cl_io *io,
752                  struct ll_readahead_state *ras, struct address_space *mapping,
753                  struct cl_page_list *queue, int flags);
754
755 /* llite/file.c */
756 extern struct file_operations ll_file_operations;
757 extern struct file_operations ll_file_operations_flock;
758 extern struct file_operations ll_file_operations_noflock;
759 extern struct inode_operations ll_file_inode_operations;
760 extern int ll_inode_revalidate_it(struct dentry *, struct lookup_intent *,
761                                   __u64);
762 extern int ll_have_md_lock(struct inode *inode, __u64 *bits,
763                            ldlm_mode_t l_req_mode);
764 extern ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
765                                    struct lustre_handle *lockh, __u64 flags,
766                                    ldlm_mode_t mode);
767 int __ll_inode_revalidate_it(struct dentry *, struct lookup_intent *,
768                              __u64 bits);
769 #ifdef HAVE_IOP_ATOMIC_OPEN
770 int ll_revalidate_nd(struct dentry *dentry, unsigned int flags);
771 #else
772 int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd);
773 #endif
774 int ll_file_open(struct inode *inode, struct file *file);
775 int ll_file_release(struct inode *inode, struct file *file);
776 int ll_glimpse_ioctl(struct ll_sb_info *sbi,
777                      struct lov_stripe_md *lsm, lstat_t *st);
778 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch);
779 int ll_local_open(struct file *file,
780                   struct lookup_intent *it, struct ll_file_data *fd,
781                   struct obd_client_handle *och);
782 int ll_release_openhandle(struct dentry *, struct lookup_intent *);
783 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
784                 struct file *file);
785 int ll_md_real_close(struct inode *inode, int flags);
786 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
787                       struct obd_client_handle **och, unsigned long flags);
788 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data);
789 int ll_som_update(struct inode *inode, struct md_op_data *op_data);
790 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
791                      __u64 ioepoch, int sync);
792 int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data,
793                   struct md_open_data **mod);
794 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
795                           struct lustre_handle *fh);
796 extern void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
797                               struct ll_file_data *file, loff_t pos,
798                               size_t count, int rw);
799 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
800                struct lookup_intent *it, struct kstat *stat);
801 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat);
802 struct ll_file_data *ll_file_data_get(void);
803 struct posix_acl * ll_get_acl(struct inode *inode, int type);
804
805 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
806 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags);
807 #else
808 # ifndef HAVE_INODE_PERMISION_2ARGS
809 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd);
810 # else
811 int ll_inode_permission(struct inode *inode, int mask);
812 # endif
813 #endif
814
815 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
816                              __u64  flags, struct lov_user_md *lum,
817                              int lum_size);
818 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
819                              struct lov_mds_md **lmm, int *lmm_size,
820                              struct ptlrpc_request **request);
821 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
822                      int set_default);
823 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
824                      int *lmm_size, struct ptlrpc_request **request);
825 #ifdef HAVE_FILE_FSYNC_4ARGS
826 int ll_fsync(struct file *file, loff_t start, loff_t end, int data);
827 #elif defined(HAVE_FILE_FSYNC_2ARGS)
828 int ll_fsync(struct file *file, int data);
829 #else
830 int ll_fsync(struct file *file, struct dentry *dentry, int data);
831 #endif
832 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
833               int num_bytes);
834 int ll_merge_lvb(const struct lu_env *env, struct inode *inode);
835 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg);
836 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
837 int ll_fid2path(struct inode *inode, void *arg);
838 int ll_data_version(struct inode *inode, __u64 *data_version, int flags);
839 int ll_hsm_release(struct inode *inode);
840
841 struct obd_client_handle *ll_lease_open(struct inode *inode, struct file *file,
842                                         fmode_t mode, __u64 flags);
843 int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
844                    bool *lease_broken);
845
846 /* llite/dcache.c */
847
848 int ll_d_init(struct dentry *de);
849 extern struct dentry_operations ll_d_ops;
850 void ll_intent_drop_lock(struct lookup_intent *);
851 void ll_intent_release(struct lookup_intent *);
852 void ll_invalidate_aliases(struct inode *);
853 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft);
854 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry);
855 #ifdef HAVE_D_COMPARE_7ARGS
856 int ll_dcompare(const struct dentry *parent, const struct inode *pinode,
857                 const struct dentry *dentry, const struct inode *inode,
858                 unsigned int len, const char *str, const struct qstr *d_name);
859 #else
860 int ll_dcompare(struct dentry *parent, struct qstr *d_name, struct qstr *name);
861 #endif
862 int ll_revalidate_it_finish(struct ptlrpc_request *request,
863                             struct lookup_intent *it, struct dentry *de);
864
865 /* llite/llite_lib.c */
866 extern struct super_operations lustre_super_operations;
867
868 char *ll_read_opt(const char *opt, char *data);
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 struct inode *ll_inode_from_lock(struct ldlm_lock *lock);
875 void ll_clear_inode(struct inode *inode);
876 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import);
877 int ll_setattr(struct dentry *de, struct iattr *attr);
878 int ll_statfs(struct dentry *de, struct kstatfs *sfs);
879 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
880                        __u64 max_age, __u32 flags);
881 void ll_update_inode(struct inode *inode, struct lustre_md *md);
882 void ll_read_inode2(struct inode *inode, void *opaque);
883 void ll_delete_inode(struct inode *inode);
884 int ll_iocontrol(struct inode *inode, struct file *file,
885                  unsigned int cmd, unsigned long arg);
886 int ll_flush_ctx(struct inode *inode);
887 void ll_umount_begin(struct super_block *sb);
888 int ll_remount_fs(struct super_block *sb, int *flags, char *data);
889 #ifdef HAVE_SUPEROPS_USE_DENTRY
890 int ll_show_options(struct seq_file *seq, struct dentry *dentry);
891 #else
892 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs);
893 #endif
894 void ll_dirty_page_discard_warn(struct page *page, int ioret);
895 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
896                   struct super_block *, struct lookup_intent *);
897 void lustre_dump_dentry(struct dentry *, int recur);
898 void lustre_dump_inode(struct inode *);
899 int ll_obd_statfs(struct inode *inode, void *arg);
900 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *max_mdsize);
901 int ll_process_config(struct lustre_cfg *lcfg);
902 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
903                                       struct inode *i1, struct inode *i2,
904                                       const char *name, int namelen,
905                                       int mode, __u32 opc, void *data);
906 void ll_finish_md_op_data(struct md_op_data *op_data);
907 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg);
908 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen);
909
910 /* llite/llite_nfs.c */
911 extern struct export_operations lustre_export_operations;
912 __u32 get_uuid2int(const char *name, int len);
913 struct inode *search_inode_for_lustre(struct super_block *sb,
914                                       const struct lu_fid *fid);
915
916 /* llite/special.c */
917 extern struct inode_operations ll_special_inode_operations;
918 extern struct file_operations ll_special_chr_inode_fops;
919 extern struct file_operations ll_special_chr_file_fops;
920 extern struct file_operations ll_special_blk_inode_fops;
921 extern struct file_operations ll_special_fifo_inode_fops;
922 extern struct file_operations ll_special_fifo_file_fops;
923 extern struct file_operations ll_special_sock_inode_fops;
924
925 /* llite/symlink.c */
926 extern struct inode_operations ll_fast_symlink_inode_operations;
927
928 /* llite/llite_close.c */
929 struct ll_close_queue {
930         spinlock_t              lcq_lock;
931         cfs_list_t              lcq_head;
932         wait_queue_head_t       lcq_waitq;
933         struct completion       lcq_comp;
934         cfs_atomic_t            lcq_stop;
935 };
936
937 struct ccc_object *cl_inode2ccc(struct inode *inode);
938
939
940 void vvp_write_pending (struct ccc_object *club, struct ccc_page *page);
941 void vvp_write_complete(struct ccc_object *club, struct ccc_page *page);
942
943 /* specific achitecture can implement only part of this list */
944 enum vvp_io_subtype {
945         /** normal IO */
946         IO_NORMAL,
947         /** io called from .sendfile */
948         IO_SENDFILE,
949         /** io started from splice_{read|write} */
950         IO_SPLICE
951 };
952
953 /* IO subtypes */
954 struct vvp_io {
955         /** io subtype */
956         enum vvp_io_subtype    cui_io_subtype;
957
958         union {
959                 struct {
960                         read_actor_t      cui_actor;
961                         void             *cui_target;
962                 } sendfile;
963                 struct {
964                         struct pipe_inode_info *cui_pipe;
965                         unsigned int            cui_flags;
966                 } splice;
967                 struct vvp_fault_io {
968                         /**
969                          * Inode modification time that is checked across DLM
970                          * lock request.
971                          */
972                         time_t                 ft_mtime;
973                         struct vm_area_struct *ft_vma;
974                         /**
975                          *  locked page returned from vvp_io
976                          */
977                         struct page            *ft_vmpage;
978                         struct vm_fault_api {
979                                 /**
980                                  * kernel fault info
981                                  */
982                                 struct vm_fault *ft_vmf;
983                                 /**
984                                  * fault API used bitflags for return code.
985                                  */
986                                 unsigned int    ft_flags;
987                         } fault;
988                 } fault;
989         } u;
990         /**
991          * Read-ahead state used by read and page-fault IO contexts.
992          */
993         struct ll_ra_read    cui_bead;
994         /**
995          * Set when cui_bead has been initialized.
996          */
997         int                  cui_ra_window_set;
998         /**
999          * Partially truncated page, that vvp_io_trunc_start() keeps locked
1000          * across truncate.
1001          */
1002         struct cl_page      *cui_partpage;
1003 };
1004
1005 /**
1006  * IO arguments for various VFS I/O interfaces.
1007  */
1008 struct vvp_io_args {
1009         /** normal/sendfile/splice */
1010         enum vvp_io_subtype via_io_subtype;
1011
1012         union {
1013                 struct {
1014                         struct kiocb      *via_iocb;
1015                         struct iovec      *via_iov;
1016                         unsigned long      via_nrsegs;
1017                 } normal;
1018                 struct {
1019                         read_actor_t       via_actor;
1020                         void              *via_target;
1021                 } sendfile;
1022                 struct {
1023                         struct pipe_inode_info  *via_pipe;
1024                         unsigned int       via_flags;
1025                 } splice;
1026         } u;
1027 };
1028
1029 struct ll_cl_context {
1030         void           *lcc_cookie;
1031         struct cl_io   *lcc_io;
1032         struct cl_page *lcc_page;
1033         struct lu_env  *lcc_env;
1034         int             lcc_refcheck;
1035         int             lcc_created;
1036 };
1037
1038 struct vvp_thread_info {
1039         struct ost_lvb       vti_lvb;
1040         struct cl_2queue     vti_queue;
1041         struct iovec         vti_local_iov;
1042         struct vvp_io_args   vti_args;
1043         struct ra_io_arg     vti_ria;
1044         struct kiocb         vti_kiocb;
1045         struct ll_cl_context vti_io_ctx;
1046 };
1047
1048 static inline struct vvp_thread_info *vvp_env_info(const struct lu_env *env)
1049 {
1050         extern struct lu_context_key vvp_key;
1051         struct vvp_thread_info      *info;
1052
1053         info = lu_context_key_get(&env->le_ctx, &vvp_key);
1054         LASSERT(info != NULL);
1055         return info;
1056 }
1057
1058 static inline struct vvp_io_args *vvp_env_args(const struct lu_env *env,
1059                                                enum vvp_io_subtype type)
1060 {
1061         struct vvp_io_args *ret = &vvp_env_info(env)->vti_args;
1062
1063         ret->via_io_subtype = type;
1064
1065         return ret;
1066 }
1067
1068 struct vvp_session {
1069         struct vvp_io         vs_ios;
1070 };
1071
1072 static inline struct vvp_session *vvp_env_session(const struct lu_env *env)
1073 {
1074         extern struct lu_context_key vvp_session_key;
1075         struct vvp_session *ses;
1076
1077         ses = lu_context_key_get(env->le_ses, &vvp_session_key);
1078         LASSERT(ses != NULL);
1079         return ses;
1080 }
1081
1082 static inline struct vvp_io *vvp_env_io(const struct lu_env *env)
1083 {
1084         return &vvp_env_session(env)->vs_ios;
1085 }
1086
1087 void ll_queue_done_writing(struct inode *inode, unsigned long flags);
1088 void ll_close_thread_shutdown(struct ll_close_queue *lcq);
1089 int ll_close_thread_start(struct ll_close_queue **lcq_ret);
1090
1091 /* llite/llite_mmap.c */
1092 typedef struct rb_root  rb_root_t;
1093 typedef struct rb_node  rb_node_t;
1094
1095 struct ll_lock_tree_node;
1096 struct ll_lock_tree {
1097         rb_root_t                       lt_root;
1098         cfs_list_t                      lt_locked_list;
1099         struct ll_file_data            *lt_fd;
1100 };
1101
1102 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last);
1103 int ll_file_mmap(struct file * file, struct vm_area_struct * vma);
1104 struct ll_lock_tree_node * ll_node_from_inode(struct inode *inode, __u64 start,
1105                                               __u64 end, ldlm_mode_t mode);
1106 void policy_from_vma(ldlm_policy_data_t *policy,
1107                 struct vm_area_struct *vma, unsigned long addr, size_t count);
1108 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
1109                                size_t count);
1110
1111 static inline void ll_invalidate_page(struct page *vmpage)
1112 {
1113         struct address_space *mapping = vmpage->mapping;
1114         loff_t offset = vmpage->index << PAGE_CACHE_SHIFT;
1115
1116         LASSERT(PageLocked(vmpage));
1117         if (mapping == NULL)
1118                 return;
1119
1120         ll_teardown_mmaps(mapping, offset, offset + PAGE_CACHE_SIZE);
1121         truncate_complete_page(mapping, vmpage);
1122 }
1123
1124 #define    ll_s2sbi(sb)        (s2lsi(sb)->lsi_llsbi)
1125
1126 /* don't need an addref as the sb_info should be holding one */
1127 static inline struct obd_export *ll_s2dtexp(struct super_block *sb)
1128 {
1129         return ll_s2sbi(sb)->ll_dt_exp;
1130 }
1131
1132 /* don't need an addref as the sb_info should be holding one */
1133 static inline struct obd_export *ll_s2mdexp(struct super_block *sb)
1134 {
1135         return ll_s2sbi(sb)->ll_md_exp;
1136 }
1137
1138 static inline struct client_obd *sbi2mdc(struct ll_sb_info *sbi)
1139 {
1140         struct obd_device *obd = sbi->ll_md_exp->exp_obd;
1141         if (obd == NULL)
1142                 LBUG();
1143         return &obd->u.cli;
1144 }
1145
1146 // FIXME: replace the name of this with LL_SB to conform to kernel stuff
1147 static inline struct ll_sb_info *ll_i2sbi(struct inode *inode)
1148 {
1149         return ll_s2sbi(inode->i_sb);
1150 }
1151
1152 static inline struct obd_export *ll_i2dtexp(struct inode *inode)
1153 {
1154         return ll_s2dtexp(inode->i_sb);
1155 }
1156
1157 static inline struct obd_export *ll_i2mdexp(struct inode *inode)
1158 {
1159         return ll_s2mdexp(inode->i_sb);
1160 }
1161
1162 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
1163 {
1164         struct lu_fid *fid;
1165
1166         LASSERT(inode != NULL);
1167         fid = &ll_i2info(inode)->lli_fid;
1168
1169         return fid;
1170 }
1171
1172 static inline int ll_mds_max_easize(struct super_block *sb)
1173 {
1174         return sbi2mdc(ll_s2sbi(sb))->cl_max_mds_easize;
1175 }
1176
1177 static inline __u64 ll_file_maxbytes(struct inode *inode)
1178 {
1179         return ll_i2info(inode)->lli_maxbytes;
1180 }
1181
1182 /* llite/xattr.c */
1183 int ll_setxattr(struct dentry *dentry, const char *name,
1184                 const void *value, size_t size, int flags);
1185 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
1186                     void *buffer, size_t size);
1187 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size);
1188 int ll_removexattr(struct dentry *dentry, const char *name);
1189
1190 /* llite/remote_perm.c */
1191 extern struct kmem_cache *ll_remote_perm_cachep;
1192 extern struct kmem_cache *ll_rmtperm_hash_cachep;
1193
1194 cfs_hlist_head_t *alloc_rmtperm_hash(void);
1195 void free_rmtperm_hash(cfs_hlist_head_t *hash);
1196 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm);
1197 int lustre_check_remote_perm(struct inode *inode, int mask);
1198
1199 /* llite/llite_capa.c */
1200 extern cfs_timer_t ll_capa_timer;
1201
1202 int ll_capa_thread_start(void);
1203 void ll_capa_thread_stop(void);
1204 void ll_capa_timer_callback(unsigned long unused);
1205
1206 struct obd_capa *ll_add_capa(struct inode *inode, struct obd_capa *ocapa);
1207 int ll_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa);
1208
1209 void ll_capa_open(struct inode *inode);
1210 void ll_capa_close(struct inode *inode);
1211
1212 struct obd_capa *ll_mdscapa_get(struct inode *inode);
1213 struct obd_capa *ll_osscapa_get(struct inode *inode, __u64 opc);
1214
1215 void ll_truncate_free_capa(struct obd_capa *ocapa);
1216 void ll_clear_inode_capas(struct inode *inode);
1217 void ll_print_capa_stat(struct ll_sb_info *sbi);
1218
1219 /* llite/llite_cl.c */
1220 extern struct lu_device_type vvp_device_type;
1221
1222 /**
1223  * Common IO arguments for various VFS I/O interfaces.
1224  */
1225 int cl_sb_init(struct super_block *sb);
1226 int cl_sb_fini(struct super_block *sb);
1227 enum cl_lock_mode  vvp_mode_from_vma(struct vm_area_struct *vma);
1228 void ll_io_init(struct cl_io *io, const struct file *file, int write);
1229
1230 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1231                 struct ll_readahead_state *ras, unsigned long index,
1232                 unsigned hit);
1233 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len);
1234 int ll_is_file_contended(struct file *file);
1235 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which);
1236
1237 /* llite/llite_rmtacl.c */
1238 #ifdef CONFIG_FS_POSIX_ACL
1239 obd_valid rce_ops2valid(int ops);
1240 struct rmtacl_ctl_entry *rct_search(struct rmtacl_ctl_table *rct, pid_t key);
1241 int rct_add(struct rmtacl_ctl_table *rct, pid_t key, int ops);
1242 int rct_del(struct rmtacl_ctl_table *rct, pid_t key);
1243 void rct_init(struct rmtacl_ctl_table *rct);
1244 void rct_fini(struct rmtacl_ctl_table *rct);
1245
1246 void ee_free(struct eacl_entry *ee);
1247 int ee_add(struct eacl_table *et, pid_t key, struct lu_fid *fid, int type,
1248            ext_acl_xattr_header *header);
1249 struct eacl_entry *et_search_del(struct eacl_table *et, pid_t key,
1250                                  struct lu_fid *fid, int type);
1251 void et_search_free(struct eacl_table *et, pid_t key);
1252 void et_init(struct eacl_table *et);
1253 void et_fini(struct eacl_table *et);
1254 #endif
1255
1256 /* statahead.c */
1257
1258 #define LL_SA_RPC_MIN           2
1259 #define LL_SA_RPC_DEF           32
1260 #define LL_SA_RPC_MAX           8192
1261
1262 #define LL_SA_CACHE_BIT         5
1263 #define LL_SA_CACHE_SIZE        (1 << LL_SA_CACHE_BIT)
1264 #define LL_SA_CACHE_MASK        (LL_SA_CACHE_SIZE - 1)
1265
1266 /* per inode struct, for dir only */
1267 struct ll_statahead_info {
1268         struct inode           *sai_inode;
1269         cfs_atomic_t            sai_refcount;   /* when access this struct, hold
1270                                                  * refcount */
1271         unsigned int            sai_generation; /* generation for statahead */
1272         unsigned int            sai_max;        /* max ahead of lookup */
1273         __u64                   sai_sent;       /* stat requests sent count */
1274         __u64                   sai_replied;    /* stat requests which received
1275                                                  * reply */
1276         __u64                   sai_index;      /* index of statahead entry */
1277         __u64                   sai_index_wait; /* index of entry which is the
1278                                                  * caller is waiting for */
1279         __u64                   sai_hit;        /* hit count */
1280         __u64                   sai_miss;       /* miss count:
1281                                                  * for "ls -al" case, it includes
1282                                                  * hidden dentry miss;
1283                                                  * for "ls -l" case, it does not
1284                                                  * include hidden dentry miss.
1285                                                  * "sai_miss_hidden" is used for
1286                                                  * the later case.
1287                                                  */
1288         unsigned int            sai_consecutive_miss; /* consecutive miss */
1289         unsigned int            sai_miss_hidden;/* "ls -al", but first dentry
1290                                                  * is not a hidden one */
1291         unsigned int            sai_skip_hidden;/* skipped hidden dentry count */
1292         unsigned int            sai_ls_all:1,   /* "ls -al", do stat-ahead for
1293                                                  * hidden entries */
1294                                 sai_in_readpage:1,/* statahead is in readdir()*/
1295                                 sai_agl_valid:1;/* AGL is valid for the dir */
1296         wait_queue_head_t       sai_waitq;      /* stat-ahead wait queue */
1297         struct ptlrpc_thread    sai_thread;     /* stat-ahead thread */
1298         struct ptlrpc_thread    sai_agl_thread; /* AGL thread */
1299         cfs_list_t              sai_entries;    /* entry list */
1300         cfs_list_t              sai_entries_received; /* entries returned */
1301         cfs_list_t              sai_entries_stated;   /* entries stated */
1302         cfs_list_t              sai_entries_agl; /* AGL entries to be sent */
1303         cfs_list_t              sai_cache[LL_SA_CACHE_SIZE];
1304         spinlock_t              sai_cache_lock[LL_SA_CACHE_SIZE];
1305         cfs_atomic_t            sai_cache_count; /* entry count in cache */
1306 };
1307
1308 int do_statahead_enter(struct inode *dir, struct dentry **dentry,
1309                        int only_unplug);
1310 void ll_stop_statahead(struct inode *dir, void *key);
1311
1312 static inline int ll_glimpse_size(struct inode *inode)
1313 {
1314         struct ll_inode_info *lli = ll_i2info(inode);
1315         int rc;
1316
1317         down_read(&lli->lli_glimpse_sem);
1318         rc = cl_glimpse_size(inode);
1319         lli->lli_glimpse_time = cfs_time_current();
1320         up_read(&lli->lli_glimpse_sem);
1321         return rc;
1322 }
1323
1324 static inline void
1325 ll_statahead_mark(struct inode *dir, struct dentry *dentry)
1326 {
1327         struct ll_inode_info     *lli = ll_i2info(dir);
1328         struct ll_statahead_info *sai = lli->lli_sai;
1329         struct ll_dentry_data    *ldd = ll_d2d(dentry);
1330
1331         /* not the same process, don't mark */
1332         if (lli->lli_opendir_pid != current_pid())
1333                 return;
1334
1335         LASSERT(ldd != NULL);
1336         if (sai != NULL)
1337                 ldd->lld_sa_generation = sai->sai_generation;
1338 }
1339
1340 static inline int
1341 ll_need_statahead(struct inode *dir, struct dentry *dentryp)
1342 {
1343         struct ll_inode_info  *lli;
1344         struct ll_dentry_data *ldd;
1345
1346         if (ll_i2sbi(dir)->ll_sa_max == 0)
1347                 return -EAGAIN;
1348
1349         lli = ll_i2info(dir);
1350         /* not the same process, don't statahead */
1351         if (lli->lli_opendir_pid != current_pid())
1352                 return -EAGAIN;
1353
1354         /* statahead has been stopped */
1355         if (lli->lli_opendir_key == NULL)
1356                 return -EAGAIN;
1357
1358         ldd = ll_d2d(dentryp);
1359         /*
1360          * When stats a dentry, the system trigger more than once "revalidate"
1361          * or "lookup", for "getattr", for "getxattr", and maybe for others.
1362          * Under patchless client mode, the operation intent is not accurate,
1363          * which maybe misguide the statahead thread. For example:
1364          * The "revalidate" call for "getattr" and "getxattr" of a dentry maybe
1365          * have the same operation intent -- "IT_GETATTR".
1366          * In fact, one dentry should has only one chance to interact with the
1367          * statahead thread, otherwise the statahead windows will be confused.
1368          * The solution is as following:
1369          * Assign "lld_sa_generation" with "sai_generation" when a dentry
1370          * "IT_GETATTR" for the first time, and the subsequent "IT_GETATTR"
1371          * will bypass interacting with statahead thread for checking:
1372          * "lld_sa_generation == lli_sai->sai_generation"
1373          */
1374         if (ldd && lli->lli_sai &&
1375             ldd->lld_sa_generation == lli->lli_sai->sai_generation)
1376                 return -EAGAIN;
1377
1378         return 1;
1379 }
1380
1381 static inline int
1382 ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int only_unplug)
1383 {
1384         int ret;
1385
1386         ret = ll_need_statahead(dir, *dentryp);
1387         if (ret <= 0)
1388                 return ret;
1389
1390         return do_statahead_enter(dir, dentryp, only_unplug);
1391 }
1392
1393 /* llite ioctl register support rountine */
1394 #ifdef __KERNEL__
1395 enum llioc_iter {
1396         LLIOC_CONT = 0,
1397         LLIOC_STOP
1398 };
1399
1400 #define LLIOC_MAX_CMD           256
1401
1402 /*
1403  * Rules to write a callback function:
1404  *
1405  * Parameters:
1406  *  @magic: Dynamic ioctl call routine will feed this vaule with the pointer
1407  *      returned to ll_iocontrol_register.  Callback functions should use this
1408  *      data to check the potential collasion of ioctl cmd. If collasion is
1409  *      found, callback function should return LLIOC_CONT.
1410  *  @rcp: The result of ioctl command.
1411  *
1412  *  Return values:
1413  *      If @magic matches the pointer returned by ll_iocontrol_data, the
1414  *      callback should return LLIOC_STOP; return LLIOC_STOP otherwise.
1415  */
1416 typedef enum llioc_iter (*llioc_callback_t)(struct inode *inode,
1417                 struct file *file, unsigned int cmd, unsigned long arg,
1418                 void *magic, int *rcp);
1419
1420 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
1421                 unsigned int cmd, unsigned long arg, int *rcp);
1422
1423 /* export functions */
1424 /* Register ioctl block dynamatically for a regular file.
1425  *
1426  * @cmd: the array of ioctl command set
1427  * @count: number of commands in the @cmd
1428  * @cb: callback function, it will be called if an ioctl command is found to
1429  *      belong to the command list @cmd.
1430  *
1431  * Return vaule:
1432  *      A magic pointer will be returned if success;
1433  *      otherwise, NULL will be returned.
1434  * */
1435 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd);
1436 void ll_iocontrol_unregister(void *magic);
1437
1438 #endif
1439
1440 /* lclient compat stuff */
1441 #define cl_inode_info ll_inode_info
1442 #define cl_i2info(info) ll_i2info(info)
1443 #define cl_inode_mode(inode) ((inode)->i_mode)
1444 #define cl_i2sbi ll_i2sbi
1445
1446 static inline struct ll_file_data *cl_iattr2fd(struct inode *inode,
1447                                                const struct iattr *attr)
1448 {
1449         LASSERT(attr->ia_valid & ATTR_FILE);
1450         return LUSTRE_FPRIVATE(attr->ia_file);
1451 }
1452
1453 static inline void cl_isize_lock(struct inode *inode)
1454 {
1455         ll_inode_size_lock(inode);
1456 }
1457
1458 static inline void cl_isize_unlock(struct inode *inode)
1459 {
1460         ll_inode_size_unlock(inode);
1461 }
1462
1463 static inline void cl_isize_write_nolock(struct inode *inode, loff_t kms)
1464 {
1465         LASSERT(down_trylock(&ll_i2info(inode)->lli_size_sem) != 0);
1466         i_size_write(inode, kms);
1467 }
1468
1469 static inline void cl_isize_write(struct inode *inode, loff_t kms)
1470 {
1471         ll_inode_size_lock(inode);
1472         i_size_write(inode, kms);
1473         ll_inode_size_unlock(inode);
1474 }
1475
1476 #define cl_isize_read(inode)             i_size_read(inode)
1477
1478 static inline int cl_merge_lvb(const struct lu_env *env, struct inode *inode)
1479 {
1480         return ll_merge_lvb(env, inode);
1481 }
1482
1483 #define cl_inode_atime(inode) LTIME_S((inode)->i_atime)
1484 #define cl_inode_ctime(inode) LTIME_S((inode)->i_ctime)
1485 #define cl_inode_mtime(inode) LTIME_S((inode)->i_mtime)
1486
1487 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt);
1488
1489 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
1490                        enum cl_fsync_mode mode, int ignore_layout);
1491
1492 /** direct write pages */
1493 struct ll_dio_pages {
1494         /** page array to be written. we don't support
1495          * partial pages except the last one. */
1496         struct page **ldp_pages;
1497         /* offset of each page */
1498         loff_t       *ldp_offsets;
1499         /** if ldp_offsets is NULL, it means a sequential
1500          * pages to be written, then this is the file offset
1501          * of the * first page. */
1502         loff_t        ldp_start_offset;
1503         /** how many bytes are to be written. */
1504         size_t        ldp_size;
1505         /** # of pages in the array. */
1506         int           ldp_nr;
1507 };
1508
1509 static inline void cl_stats_tally(struct cl_device *dev, enum cl_req_type crt,
1510                                   int rc)
1511 {
1512         int opc = (crt == CRT_READ) ? LPROC_LL_OSC_READ :
1513                                       LPROC_LL_OSC_WRITE;
1514
1515         ll_stats_ops_tally(ll_s2sbi(cl2ccc_dev(dev)->cdv_sb), opc, rc);
1516 }
1517
1518 extern ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
1519                                   int rw, struct inode *inode,
1520                                   struct ll_dio_pages *pv);
1521
1522 static inline int ll_file_nolock(const struct file *file)
1523 {
1524         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1525         struct inode *inode = file->f_dentry->d_inode;
1526
1527         LASSERT(fd != NULL);
1528         return ((fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
1529                 (ll_i2sbi(inode)->ll_flags & LL_SBI_NOLCK));
1530 }
1531
1532 static inline void ll_set_lock_data(struct obd_export *exp, struct inode *inode,
1533                                     struct lookup_intent *it, __u64 *bits)
1534 {
1535         if (!it->d.lustre.it_lock_set) {
1536                 struct lustre_handle handle;
1537
1538                 /* If this inode is a remote object, it will get two
1539                  * separate locks in different namespaces, Master MDT,
1540                  * where the name entry is, will grant LOOKUP lock,
1541                  * remote MDT, where the object is, will grant
1542                  * UPDATE|PERM lock. The inode will be attched to both
1543                  * LOOKUP and PERM locks, so revoking either locks will
1544                  * case the dcache being cleared */
1545                 if (it->d.lustre.it_remote_lock_mode) {
1546                         handle.cookie = it->d.lustre.it_remote_lock_handle;
1547                         CDEBUG(D_DLMTRACE, "setting l_data to inode %p"
1548                                "(%lu/%u) for remote lock "LPX64"\n", inode,
1549                                inode->i_ino, inode->i_generation,
1550                                handle.cookie);
1551                         md_set_lock_data(exp, &handle.cookie, inode, NULL);
1552                 }
1553                 
1554                 handle.cookie = it->d.lustre.it_lock_handle;
1555
1556                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)"
1557                        " for lock "LPX64"\n", inode, inode->i_ino,
1558                        inode->i_generation, handle.cookie);
1559
1560                 md_set_lock_data(exp, &handle.cookie, inode,
1561                                  &it->d.lustre.it_lock_bits);
1562                 it->d.lustre.it_lock_set = 1;
1563         }
1564
1565         if (bits != NULL)
1566                 *bits = it->d.lustre.it_lock_bits;
1567 }
1568
1569 static inline void ll_lock_dcache(struct inode *inode)
1570 {
1571 #ifdef HAVE_DCACHE_LOCK
1572         spin_lock(&dcache_lock);
1573 #else
1574         spin_lock(&inode->i_lock);
1575 #endif
1576 }
1577
1578 static inline void ll_unlock_dcache(struct inode *inode)
1579 {
1580 #ifdef HAVE_DCACHE_LOCK
1581         spin_unlock(&dcache_lock);
1582 #else
1583         spin_unlock(&inode->i_lock);
1584 #endif
1585 }
1586
1587 static inline int d_lustre_invalid(const struct dentry *dentry)
1588 {
1589         struct ll_dentry_data *lld = ll_d2d(dentry);
1590
1591         return (lld == NULL) || lld->lld_invalid;
1592 }
1593
1594 static inline void __d_lustre_invalidate(struct dentry *dentry)
1595 {
1596         struct ll_dentry_data *lld = ll_d2d(dentry);
1597
1598         if (lld != NULL)
1599                 lld->lld_invalid = 1;
1600 }
1601
1602 /*
1603  * Mark dentry INVALID, if dentry refcount is zero (this is normally case for
1604  * ll_md_blocking_ast), unhash this dentry, and let dcache to reclaim it later;
1605  * else dput() of the last refcount will unhash this dentry and kill it.
1606  */
1607 static inline void d_lustre_invalidate(struct dentry *dentry, int nested)
1608 {
1609         CDEBUG(D_DENTRY, "invalidate dentry %.*s (%p) parent %p inode %p "
1610                "refc %d\n", dentry->d_name.len, dentry->d_name.name, dentry,
1611                dentry->d_parent, dentry->d_inode, d_refcount(dentry));
1612
1613         spin_lock_nested(&dentry->d_lock,
1614                          nested ? DENTRY_D_LOCK_NESTED : DENTRY_D_LOCK_NORMAL);
1615         __d_lustre_invalidate(dentry);
1616         if (d_refcount(dentry) == 0)
1617                 __d_drop(dentry);
1618         spin_unlock(&dentry->d_lock);
1619 }
1620
1621 static inline void d_lustre_revalidate(struct dentry *dentry)
1622 {
1623         spin_lock(&dentry->d_lock);
1624         LASSERT(ll_d2d(dentry) != NULL);
1625         ll_d2d(dentry)->lld_invalid = 0;
1626         spin_unlock(&dentry->d_lock);
1627 }
1628
1629 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1630 /* Compatibility for old (1.8) compiled userspace quota code */
1631 struct if_quotactl_18 {
1632         __u32                   qc_cmd;
1633         __u32                   qc_type;
1634         __u32                   qc_id;
1635         __u32                   qc_stat;
1636         struct obd_dqinfo       qc_dqinfo;
1637         struct obd_dqblk        qc_dqblk;
1638         char                    obd_type[16];
1639         struct obd_uuid         obd_uuid;
1640 };
1641 #define LL_IOC_QUOTACTL_18              _IOWR('f', 162, struct if_quotactl_18 *)
1642 /* End compatibility for old (1.8) compiled userspace quota code */
1643 #else
1644 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1645 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1646
1647 enum {
1648         LL_LAYOUT_GEN_NONE  = ((__u32)-2),      /* layout lock was cancelled */
1649         LL_LAYOUT_GEN_EMPTY = ((__u32)-1)       /* for empty layout */
1650 };
1651
1652 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf);
1653 int ll_layout_refresh(struct inode *inode, __u32 *gen);
1654 int ll_layout_restore(struct inode *inode);
1655
1656 int ll_xattr_init(void);
1657 void ll_xattr_fini(void);
1658
1659 #endif /* LLITE_INTERNAL_H */