staging: lustre: llite: Replace write mutex with range lock
[linux-2.6-microblaze.git] / drivers / staging / lustre / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef LLITE_INTERNAL_H
34 #define LLITE_INTERNAL_H
35 #include "../include/lustre_debug.h"
36 #include "../include/lustre_ver.h"
37 #include "../include/lustre_disk.h"     /* for s2sbi */
38 #include "../include/lustre_eacl.h"
39
40 /* for struct cl_lock_descr and struct cl_io */
41 #include "../include/cl_object.h"
42 #include "../include/lustre_lmv.h"
43 #include "../include/lustre_mdc.h"
44 #include "../include/lustre_intent.h"
45 #include <linux/compat.h>
46 #include <linux/xattr.h>
47 #include <linux/posix_acl_xattr.h>
48 #include "vvp_internal.h"
49 #include "range_lock.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 /** Only used on client-side for indicating the tail of dir hash/offset. */
60 #define LL_DIR_END_OFF    0x7fffffffffffffffULL
61 #define LL_DIR_END_OFF_32BIT    0x7fffffffUL
62
63 #define LL_IT2STR(it) ((it) ? ldlm_it2str((it)->it_op) : "0")
64 #define LUSTRE_FPRIVATE(file) ((file)->private_data)
65
66 struct ll_dentry_data {
67         struct lookup_intent            *lld_it;
68         unsigned int                    lld_sa_generation;
69         unsigned int                    lld_invalid:1;
70         unsigned int                    lld_nfs_dentry:1;
71         struct rcu_head                 lld_rcu_head;
72 };
73
74 #define ll_d2d(de) ((struct ll_dentry_data *)((de)->d_fsdata))
75
76 #define LLI_INODE_MAGIC          0x111d0de5
77 #define LLI_INODE_DEAD            0xdeadd00d
78
79 struct ll_getname_data {
80         struct dir_context ctx;
81         char        *lgd_name;      /* points to a buffer with NAME_MAX+1 size */
82         struct lu_fid    lgd_fid;       /* target fid we are looking for */
83         int           lgd_found;     /* inode matched? */
84 };
85
86 struct ll_grouplock {
87         struct lu_env   *lg_env;
88         struct cl_io    *lg_io;
89         struct cl_lock  *lg_lock;
90         unsigned long    lg_gid;
91 };
92
93 enum lli_flags {
94         /* MDS has an authority for the Size-on-MDS attributes. */
95         LLIF_MDS_SIZE_LOCK      = (1 << 0),
96         /* Epoch close is postponed. */
97         LLIF_EPOCH_PENDING      = (1 << 1),
98         /* DONE WRITING is allowed. */
99         LLIF_DONE_WRITING       = (1 << 2),
100         /* Sizeon-on-MDS attributes are changed. An attribute update needs to
101          * be sent to MDS.
102          */
103         LLIF_SOM_DIRTY    = (1 << 3),
104         /* File data is modified. */
105         LLIF_DATA_MODIFIED      = (1 << 4),
106         /* File is being restored */
107         LLIF_FILE_RESTORING     = (1 << 5),
108         /* Xattr cache is attached to the file */
109         LLIF_XATTR_CACHE        = (1 << 6),
110 };
111
112 struct ll_inode_info {
113         __u32                           lli_inode_magic;
114         __u32                           lli_flags;
115         __u64                           lli_ioepoch;
116
117         spinlock_t                      lli_lock;
118         struct posix_acl                *lli_posix_acl;
119
120         /* identifying fields for both metadata and data stacks. */
121         struct lu_fid              lli_fid;
122         /* master inode fid for stripe directory */
123         struct lu_fid              lli_pfid;
124
125         struct list_head              lli_close_list;
126
127         /* handle is to be sent to MDS later on done_writing and setattr.
128          * Open handle data are needed for the recovery to reconstruct
129          * the inode state on the MDS. XXX: recovery is not ready yet.
130          */
131         struct obd_client_handle       *lli_pending_och;
132
133         /* We need all three because every inode may be opened in different
134          * modes
135          */
136         struct obd_client_handle       *lli_mds_read_och;
137         struct obd_client_handle       *lli_mds_write_och;
138         struct obd_client_handle       *lli_mds_exec_och;
139         __u64                      lli_open_fd_read_count;
140         __u64                      lli_open_fd_write_count;
141         __u64                      lli_open_fd_exec_count;
142         /* Protects access to och pointers and their usage counters */
143         struct mutex                    lli_och_mutex;
144
145         struct inode                    lli_vfs_inode;
146
147         /* the most recent timestamps obtained from mds */
148         s64                             lli_atime;
149         s64                             lli_mtime;
150         s64                             lli_ctime;
151         spinlock_t                      lli_agl_lock;
152
153         /* Try to make the d::member and f::member are aligned. Before using
154          * these members, make clear whether it is directory or not.
155          */
156         union {
157                 /* for directory */
158                 struct {
159                         /* serialize normal readdir and statahead-readdir. */
160                         struct mutex                    d_readdir_mutex;
161
162                         /* metadata statahead */
163                         /* since parent-child threads can share the same @file
164                          * struct, "opendir_key" is the token when dir close for
165                          * case of parent exit before child -- it is me should
166                          * cleanup the dir readahead.
167                          */
168                         void                       *d_opendir_key;
169                         struct ll_statahead_info       *d_sai;
170                         /* protect statahead stuff. */
171                         spinlock_t                      d_sa_lock;
172                         /* "opendir_pid" is the token when lookup/revalidate
173                          * -- I am the owner of dir statahead.
174                          */
175                         pid_t                      d_opendir_pid;
176                         /* stat will try to access statahead entries or start
177                          * statahead if this flag is set, and this flag will be
178                          * set upon dir open, and cleared when dir is closed,
179                          * statahead hit ratio is too low, or start statahead
180                          * thread failed.
181                          */
182                         unsigned int                    d_sa_enabled:1;
183                         /* directory stripe information */
184                         struct lmv_stripe_md            *d_lsm_md;
185                         /* striped directory size */
186                         loff_t                          d_stripe_size;
187                         /* striped directory nlink */
188                         __u64                           d_stripe_nlink;
189                 } d;
190
191 #define lli_readdir_mutex       u.d.d_readdir_mutex
192 #define lli_opendir_key  u.d.d_opendir_key
193 #define lli_sai          u.d.d_sai
194 #define lli_sa_lock          u.d.d_sa_lock
195 #define lli_sa_enabled          u.d.d_sa_enabled
196 #define lli_opendir_pid  u.d.d_opendir_pid
197 #define lli_lsm_md              u.d.d_lsm_md
198 #define lli_stripe_dir_size     u.d.d_stripe_size
199 #define lli_stripe_dir_nlink    u.d.d_stripe_nlink
200
201                 /* for non-directory */
202                 struct {
203                         struct mutex                    f_size_mutex;
204                         char                            *f_symlink_name;
205                         __u64                           f_maxbytes;
206                         /*
207                          * struct rw_semaphore {
208                          *    signed long       count;     // align d.d_def_acl
209                          *    spinlock_t        wait_lock; // align d.d_sa_lock
210                          *    struct list_head wait_list;
211                          * }
212                          */
213                         struct rw_semaphore             f_trunc_sem;
214                         struct range_lock_tree          f_write_tree;
215
216                         struct rw_semaphore             f_glimpse_sem;
217                         unsigned long                   f_glimpse_time;
218                         struct list_head                        f_agl_list;
219                         __u64                           f_agl_index;
220
221                         /* for writepage() only to communicate to fsync */
222                         int                             f_async_rc;
223
224                         /*
225                          * whenever a process try to read/write the file, the
226                          * jobid of the process will be saved here, and it'll
227                          * be packed into the write PRC when flush later.
228                          *
229                          * so the read/write statistics for jobid will not be
230                          * accurate if the file is shared by different jobs.
231                          */
232                         char                 f_jobid[JOBSTATS_JOBID_SIZE];
233                 } f;
234
235 #define lli_size_mutex          u.f.f_size_mutex
236 #define lli_symlink_name        u.f.f_symlink_name
237 #define lli_maxbytes        u.f.f_maxbytes
238 #define lli_trunc_sem      u.f.f_trunc_sem
239 #define lli_write_tree          u.f.f_write_tree
240 #define lli_glimpse_sem         u.f.f_glimpse_sem
241 #define lli_glimpse_time        u.f.f_glimpse_time
242 #define lli_agl_list            u.f.f_agl_list
243 #define lli_agl_index           u.f.f_agl_index
244 #define lli_async_rc            u.f.f_async_rc
245 #define lli_jobid               u.f.f_jobid
246
247         } u;
248
249         /* XXX: For following frequent used members, although they maybe special
250          *      used for non-directory object, it is some time-wasting to check
251          *      whether the object is directory or not before using them. On the
252          *      other hand, currently, sizeof(f) > sizeof(d), it cannot reduce
253          *      the "ll_inode_info" size even if moving those members into u.f.
254          *      So keep them out side.
255          *
256          *      In the future, if more members are added only for directory,
257          *      some of the following members can be moved into u.f.
258          */
259         bool                        lli_has_smd;
260         struct cl_object               *lli_clob;
261
262         /* mutex to request for layout lock exclusively. */
263         struct mutex                    lli_layout_mutex;
264         /* Layout version, protected by lli_layout_lock */
265         __u32                           lli_layout_gen;
266         spinlock_t                      lli_layout_lock;
267
268         struct rw_semaphore             lli_xattrs_list_rwsem;
269         struct mutex                    lli_xattrs_enq_lock;
270         struct list_head                lli_xattrs;/* ll_xattr_entry->xe_list */
271 };
272
273 static inline __u32 ll_layout_version_get(struct ll_inode_info *lli)
274 {
275         __u32 gen;
276
277         spin_lock(&lli->lli_layout_lock);
278         gen = lli->lli_layout_gen;
279         spin_unlock(&lli->lli_layout_lock);
280
281         return gen;
282 }
283
284 static inline void ll_layout_version_set(struct ll_inode_info *lli, __u32 gen)
285 {
286         spin_lock(&lli->lli_layout_lock);
287         lli->lli_layout_gen = gen;
288         spin_unlock(&lli->lli_layout_lock);
289 }
290
291 int ll_xattr_cache_destroy(struct inode *inode);
292
293 int ll_xattr_cache_get(struct inode *inode, const char *name,
294                        char *buffer, size_t size, __u64 valid);
295
296 /*
297  * Locking to guarantee consistency of non-atomic updates to long long i_size,
298  * consistency between file size and KMS.
299  *
300  * Implemented by ->lli_size_mutex and ->lsm_lock, nested in that order.
301  */
302
303 void ll_inode_size_lock(struct inode *inode);
304 void ll_inode_size_unlock(struct inode *inode);
305
306 /* FIXME: replace the name of this with LL_I to conform to kernel stuff */
307 /* static inline struct ll_inode_info *LL_I(struct inode *inode) */
308 static inline struct ll_inode_info *ll_i2info(struct inode *inode)
309 {
310         return container_of(inode, struct ll_inode_info, lli_vfs_inode);
311 }
312
313 /* default to about 40meg of readahead on a given system.  That much tied
314  * up in 512k readahead requests serviced at 40ms each is about 1GB/s.
315  */
316 #define SBI_DEFAULT_READAHEAD_MAX (40UL << (20 - PAGE_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_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         RA_STAT_FAILED_REACH_END,
335         _NR_RA_STAT,
336 };
337
338 struct ll_ra_info {
339         atomic_t              ra_cur_pages;
340         unsigned long        ra_max_pages;
341         unsigned long        ra_max_pages_per_file;
342         unsigned long        ra_max_read_ahead_whole_pages;
343 };
344
345 /* ra_io_arg will be filled in the beginning of ll_readahead with
346  * ras_lock, then the following ll_read_ahead_pages will read RA
347  * pages according to this arg, all the items in this structure are
348  * counted by page index.
349  */
350 struct ra_io_arg {
351         unsigned long ria_start;  /* start offset of read-ahead*/
352         unsigned long ria_end;    /* end offset of read-ahead*/
353         /* If stride read pattern is detected, ria_stoff means where
354          * stride read is started. Note: for normal read-ahead, the
355          * value here is meaningless, and also it will not be accessed
356          */
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          */
362         unsigned long ria_length;
363         unsigned long ria_pages;
364 };
365
366 /* LL_HIST_MAX=32 causes an overflow */
367 #define LL_HIST_MAX 28
368 #define LL_HIST_START 12 /* buckets start at 2^12 = 4k */
369 #define LL_PROCESS_HIST_MAX 10
370 struct per_process_info {
371         pid_t pid;
372         struct obd_histogram pp_r_hist;
373         struct obd_histogram pp_w_hist;
374 };
375
376 /* pp_extents[LL_PROCESS_HIST_MAX] will hold the combined process info */
377 struct ll_rw_extents_info {
378         struct per_process_info pp_extents[LL_PROCESS_HIST_MAX + 1];
379 };
380
381 #define LL_OFFSET_HIST_MAX 100
382 struct ll_rw_process_info {
383         pid_t                rw_pid;
384         int                    rw_op;
385         loff_t              rw_range_start;
386         loff_t              rw_range_end;
387         loff_t              rw_last_file_pos;
388         loff_t              rw_offset;
389         size_t              rw_smallest_extent;
390         size_t              rw_largest_extent;
391         struct ll_file_data      *rw_last_file;
392 };
393
394 enum stats_track_type {
395         STATS_TRACK_ALL = 0,  /* track all processes */
396         STATS_TRACK_PID,      /* track process with this pid */
397         STATS_TRACK_PPID,     /* track processes with this ppid */
398         STATS_TRACK_GID,      /* track processes with this gid */
399         STATS_TRACK_LAST,
400 };
401
402 /* flags for sbi->ll_flags */
403 #define LL_SBI_NOLCK         0x01 /* DLM locking disabled (directio-only) */
404 #define LL_SBI_CHECKSUM   0x02 /* checksum each page as it's written */
405 #define LL_SBI_FLOCK         0x04
406 #define LL_SBI_USER_XATTR       0x08 /* support user xattr */
407 #define LL_SBI_ACL             0x10 /* support ACL */
408 /* LL_SBI_RMT_CLIENT             0x40    remote client */
409 #define LL_SBI_MDS_CAPA          0x80 /* support mds capa, obsolete */
410 #define LL_SBI_OSS_CAPA         0x100 /* support oss capa, obsolete */
411 #define LL_SBI_LOCALFLOCK       0x200 /* Local flocks support by kernel */
412 #define LL_SBI_LRU_RESIZE       0x400 /* lru resize support */
413 #define LL_SBI_LAZYSTATFS       0x800 /* lazystatfs mount option */
414 #define LL_SBI_SOM_PREVIEW     0x1000 /* SOM preview mount option */
415 #define LL_SBI_32BIT_API       0x2000 /* generate 32 bit inodes. */
416 #define LL_SBI_64BIT_HASH      0x4000 /* support 64-bits dir hash/offset */
417 #define LL_SBI_AGL_ENABLED     0x8000 /* enable agl */
418 #define LL_SBI_VERBOSE  0x10000 /* verbose mount/umount */
419 #define LL_SBI_LAYOUT_LOCK    0x20000 /* layout lock support */
420 #define LL_SBI_USER_FID2PATH  0x40000 /* allow fid2path by unprivileged users */
421 #define LL_SBI_XATTR_CACHE    0x80000 /* support for xattr cache */
422 #define LL_SBI_NOROOTSQUASH     0x100000 /* do not apply root squash */
423
424 #define LL_SBI_FLAGS {  \
425         "nolck",        \
426         "checksum",     \
427         "flock",        \
428         "xattr",        \
429         "acl",          \
430         "???",          \
431         "???",          \
432         "mds_capa",     \
433         "oss_capa",     \
434         "flock",        \
435         "lru_resize",   \
436         "lazy_statfs",  \
437         "som",          \
438         "32bit_api",    \
439         "64bit_hash",   \
440         "agl",          \
441         "verbose",      \
442         "layout",       \
443         "user_fid2path",\
444         "xattr",        \
445         "norootsquash", \
446 }
447
448 struct ll_sb_info {
449         /* this protects pglist and ra_info.  It isn't safe to
450          * grab from interrupt contexts
451          */
452         spinlock_t                ll_lock;
453         spinlock_t                ll_pp_extent_lock; /* pp_extent entry*/
454         spinlock_t                ll_process_lock; /* ll_rw_process_info */
455         struct obd_uuid    ll_sb_uuid;
456         struct obd_export       *ll_md_exp;
457         struct obd_export       *ll_dt_exp;
458         struct dentry           *ll_debugfs_entry;
459         struct lu_fid        ll_root_fid; /* root object fid */
460
461         int                    ll_flags;
462         unsigned int              ll_umounting:1,
463                                   ll_xattr_cache_enabled:1;
464         struct lustre_client_ocd  ll_lco;
465
466         struct ll_close_queue    *ll_lcq;
467
468         struct lprocfs_stats     *ll_stats; /* lprocfs stats counter */
469
470         /*
471          * Used to track "unstable" pages on a client, and maintain a
472          * LRU list of clean pages. An "unstable" page is defined as
473          * any page which is sent to a server as part of a bulk request,
474          * but is uncommitted to stable storage.
475          */
476         struct cl_client_cache    *ll_cache;
477
478         struct lprocfs_stats     *ll_ra_stats;
479
480         struct ll_ra_info        ll_ra_info;
481         unsigned int          ll_namelen;
482         struct file_operations   *ll_fop;
483
484         unsigned int              ll_md_brw_pages; /* readdir pages per RPC */
485
486         struct lu_site     *ll_site;
487         struct cl_device         *ll_cl;
488         /* Statistics */
489         struct ll_rw_extents_info ll_rw_extents_info;
490         int                    ll_extent_process_count;
491         struct ll_rw_process_info ll_rw_process_info[LL_PROCESS_HIST_MAX];
492         unsigned int          ll_offset_process_count;
493         struct ll_rw_process_info ll_rw_offset_info[LL_OFFSET_HIST_MAX];
494         unsigned int          ll_rw_offset_entry_count;
495         int                    ll_stats_track_id;
496         enum stats_track_type     ll_stats_track_type;
497         int                    ll_rw_stats_on;
498
499         /* metadata stat-ahead */
500         unsigned int          ll_sa_max;     /* max statahead RPCs */
501         atomic_t                  ll_sa_total;   /* statahead thread started
502                                                   * count
503                                                   */
504         atomic_t                  ll_sa_wrong;   /* statahead thread stopped for
505                                                   * low hit ratio
506                                                   */
507         atomic_t                ll_sa_running;  /* running statahead thread
508                                                  * count
509                                                  */
510         atomic_t                  ll_agl_total;  /* AGL thread started count */
511
512         dev_t                     ll_sdev_orig; /* save s_dev before assign for
513                                                  * clustered nfs
514                                                  */
515         /* root squash */
516         struct root_squash_info   ll_squash;
517
518         __kernel_fsid_t           ll_fsid;
519         struct kobject           ll_kobj; /* sysfs object */
520         struct super_block      *ll_sb; /* struct super_block (for sysfs code)*/
521         struct completion        ll_kobj_unregister;
522 };
523
524 /*
525  * per file-descriptor read-ahead data.
526  */
527 struct ll_readahead_state {
528         spinlock_t  ras_lock;
529         /*
530          * index of the last page that read(2) needed and that wasn't in the
531          * cache. Used by ras_update() to detect seeks.
532          *
533          * XXX nikita: if access seeks into cached region, Lustre doesn't see
534          * this.
535          */
536         unsigned long   ras_last_readpage;
537         /*
538          * number of pages read after last read-ahead window reset. As window
539          * is reset on each seek, this is effectively a number of consecutive
540          * accesses. Maybe ->ras_accessed_in_window is better name.
541          *
542          * XXX nikita: window is also reset (by ras_update()) when Lustre
543          * believes that memory pressure evicts read-ahead pages. In that
544          * case, it probably doesn't make sense to expand window to
545          * PTLRPC_MAX_BRW_PAGES on the third access.
546          */
547         unsigned long   ras_consecutive_pages;
548         /*
549          * number of read requests after the last read-ahead window reset
550          * As window is reset on each seek, this is effectively the number
551          * on consecutive read request and is used to trigger read-ahead.
552          */
553         unsigned long   ras_consecutive_requests;
554         /*
555          * Parameters of current read-ahead window. Handled by
556          * ras_update(). On the initial access to the file or after a seek,
557          * window is reset to 0. After 3 consecutive accesses, window is
558          * expanded to PTLRPC_MAX_BRW_PAGES. Afterwards, window is enlarged by
559          * PTLRPC_MAX_BRW_PAGES chunks up to ->ra_max_pages.
560          */
561         unsigned long   ras_window_start, ras_window_len;
562         /*
563          * Where next read-ahead should start at. This lies within read-ahead
564          * window. Read-ahead window is read in pieces rather than at once
565          * because: 1. lustre limits total number of pages under read-ahead by
566          * ->ra_max_pages (see ll_ra_count_get()), 2. client cannot read pages
567          * not covered by DLM lock.
568          */
569         unsigned long   ras_next_readahead;
570         /*
571          * Total number of ll_file_read requests issued, reads originating
572          * due to mmap are not counted in this total.  This value is used to
573          * trigger full file read-ahead after multiple reads to a small file.
574          */
575         unsigned long   ras_requests;
576         /*
577          * Page index with respect to the current request, these value
578          * will not be accurate when dealing with reads issued via mmap.
579          */
580         unsigned long   ras_request_index;
581         /*
582          * The following 3 items are used for detecting the stride I/O
583          * mode.
584          * In stride I/O mode,
585          * ...............|-----data-----|****gap*****|--------|******|....
586          *    offset      |-stride_pages-|-stride_gap-|
587          * ras_stride_offset = offset;
588          * ras_stride_length = stride_pages + stride_gap;
589          * ras_stride_pages = stride_pages;
590          * Note: all these three items are counted by pages.
591          */
592         unsigned long   ras_stride_length;
593         unsigned long   ras_stride_pages;
594         pgoff_t  ras_stride_offset;
595         /*
596          * number of consecutive stride request count, and it is similar as
597          * ras_consecutive_requests, but used for stride I/O mode.
598          * Note: only more than 2 consecutive stride request are detected,
599          * stride read-ahead will be enable
600          */
601         unsigned long   ras_consecutive_stride_requests;
602 };
603
604 extern struct kmem_cache *ll_file_data_slab;
605 struct lustre_handle;
606 struct ll_file_data {
607         struct ll_readahead_state fd_ras;
608         struct ll_grouplock fd_grouplock;
609         __u64 lfd_pos;
610         __u32 fd_flags;
611         fmode_t fd_omode;
612         /* openhandle if lease exists for this file.
613          * Borrow lli->lli_och_mutex to protect assignment
614          */
615         struct obd_client_handle *fd_lease_och;
616         struct obd_client_handle *fd_och;
617         struct file *fd_file;
618         /* Indicate whether need to report failure when close.
619          * true: failure is known, not report again.
620          * false: unknown failure, should report.
621          */
622         bool fd_write_failed;
623         rwlock_t fd_lock; /* protect lcc list */
624         struct list_head fd_lccs; /* list of ll_cl_context */
625 };
626
627 struct lov_stripe_md;
628
629 extern struct dentry *llite_root;
630 extern struct kset *llite_kset;
631
632 static inline struct inode *ll_info2i(struct ll_inode_info *lli)
633 {
634         return &lli->lli_vfs_inode;
635 }
636
637 __u32 ll_i2suppgid(struct inode *i);
638 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2);
639
640 static inline int ll_need_32bit_api(struct ll_sb_info *sbi)
641 {
642 #if BITS_PER_LONG == 32
643         return 1;
644 #elif defined(CONFIG_COMPAT)
645         return unlikely(in_compat_syscall() || (sbi->ll_flags & LL_SBI_32BIT_API));
646 #else
647         return unlikely(sbi->ll_flags & LL_SBI_32BIT_API);
648 #endif
649 }
650
651 void ll_ras_enter(struct file *f);
652
653 /* llite/lcommon_misc.c */
654 int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp);
655 int cl_ocd_update(struct obd_device *host,
656                   struct obd_device *watched,
657                   enum obd_notify_event ev, void *owner, void *data);
658 int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
659                      struct ll_grouplock *cg);
660 void cl_put_grouplock(struct ll_grouplock *cg);
661
662 /* llite/lproc_llite.c */
663 int ldebugfs_register_mountpoint(struct dentry *parent,
664                                  struct super_block *sb, char *osc, char *mdc);
665 void ldebugfs_unregister_mountpoint(struct ll_sb_info *sbi);
666 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count);
667 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars);
668 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
669                        struct ll_file_data *file, loff_t pos,
670                        size_t count, int rw);
671
672 /* llite/dir.c */
673 extern const struct file_operations ll_dir_operations;
674 extern const struct inode_operations ll_dir_inode_operations;
675 int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
676                 struct dir_context *ctx);
677 int ll_get_mdt_idx(struct inode *inode);
678 int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid);
679 struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
680                              __u64 offset, struct ll_dir_chain *chain);
681 void ll_release_page(struct inode *inode, struct page *page, bool remove);
682
683 /* llite/namei.c */
684 extern const struct inode_operations ll_special_inode_operations;
685
686 int ll_objects_destroy(struct ptlrpc_request *request,
687                        struct inode *dir);
688 struct inode *ll_iget(struct super_block *sb, ino_t hash,
689                       struct lustre_md *lic);
690 int ll_test_inode_by_fid(struct inode *inode, void *opaque);
691 int ll_md_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
692                        void *data, int flag);
693 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de);
694 void ll_update_times(struct ptlrpc_request *request, struct inode *inode);
695
696 /* llite/rw.c */
697 int ll_writepage(struct page *page, struct writeback_control *wbc);
698 int ll_writepages(struct address_space *, struct writeback_control *wbc);
699 int ll_readpage(struct file *file, struct page *page);
700 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras);
701 int ll_readahead(const struct lu_env *env, struct cl_io *io,
702                  struct cl_page_list *queue, struct ll_readahead_state *ras,
703                  bool hit);
704 struct ll_cl_context *ll_cl_find(struct file *file);
705 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io);
706 void ll_cl_remove(struct file *file, const struct lu_env *env);
707
708 extern const struct address_space_operations ll_aops;
709
710 /* llite/file.c */
711 extern struct file_operations ll_file_operations;
712 extern struct file_operations ll_file_operations_flock;
713 extern struct file_operations ll_file_operations_noflock;
714 extern const struct inode_operations ll_file_inode_operations;
715 int ll_have_md_lock(struct inode *inode, __u64 *bits,
716                     enum ldlm_mode l_req_mode);
717 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
718                                struct lustre_handle *lockh, __u64 flags,
719                                enum ldlm_mode mode);
720 int ll_file_open(struct inode *inode, struct file *file);
721 int ll_file_release(struct inode *inode, struct file *file);
722 int ll_glimpse_ioctl(struct ll_sb_info *sbi,
723                      struct lov_stripe_md *lsm, lstat_t *st);
724 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch);
725 int ll_release_openhandle(struct inode *, struct lookup_intent *);
726 int ll_md_real_close(struct inode *inode, fmode_t fmode);
727 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
728                       struct obd_client_handle **och, unsigned long flags);
729 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data);
730 int ll_som_update(struct inode *inode, struct md_op_data *op_data);
731 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
732                      __u64 ioepoch, int sync);
733 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
734                           struct lustre_handle *fh);
735 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat);
736 struct posix_acl *ll_get_acl(struct inode *inode, int type);
737 int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
738                const char *name, int namelen);
739 int ll_get_fid_by_name(struct inode *parent, const char *name,
740                        int namelen, struct lu_fid *fid);
741 int ll_inode_permission(struct inode *inode, int mask);
742
743 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
744                              __u64 flags, struct lov_user_md *lum,
745                              int lum_size);
746 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
747                              struct lov_mds_md **lmm, int *lmm_size,
748                              struct ptlrpc_request **request);
749 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
750                      int set_default);
751 int ll_dir_getstripe(struct inode *inode, void **lmmp, int *lmm_size,
752                      struct ptlrpc_request **request, u64 valid);
753 int ll_fsync(struct file *file, loff_t start, loff_t end, int data);
754 int ll_merge_attr(const struct lu_env *env, struct inode *inode);
755 int ll_fid2path(struct inode *inode, void __user *arg);
756 int ll_data_version(struct inode *inode, __u64 *data_version, int flags);
757 int ll_hsm_release(struct inode *inode);
758
759 /* llite/dcache.c */
760
761 int ll_d_init(struct dentry *de);
762 extern const struct dentry_operations ll_d_ops;
763 void ll_intent_drop_lock(struct lookup_intent *);
764 void ll_intent_release(struct lookup_intent *);
765 void ll_invalidate_aliases(struct inode *);
766 void ll_lookup_finish_locks(struct lookup_intent *it, struct inode *inode);
767 int ll_revalidate_it_finish(struct ptlrpc_request *request,
768                             struct lookup_intent *it, struct inode *inode);
769
770 /* llite/llite_lib.c */
771 extern struct super_operations lustre_super_operations;
772
773 void ll_lli_init(struct ll_inode_info *lli);
774 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt);
775 void ll_put_super(struct super_block *sb);
776 void ll_kill_super(struct super_block *sb);
777 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock);
778 void ll_clear_inode(struct inode *inode);
779 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import);
780 int ll_setattr(struct dentry *de, struct iattr *attr);
781 int ll_statfs(struct dentry *de, struct kstatfs *sfs);
782 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
783                        __u64 max_age, __u32 flags);
784 int ll_update_inode(struct inode *inode, struct lustre_md *md);
785 int ll_read_inode2(struct inode *inode, void *opaque);
786 void ll_delete_inode(struct inode *inode);
787 int ll_iocontrol(struct inode *inode, struct file *file,
788                  unsigned int cmd, unsigned long arg);
789 int ll_flush_ctx(struct inode *inode);
790 void ll_umount_begin(struct super_block *sb);
791 int ll_remount_fs(struct super_block *sb, int *flags, char *data);
792 int ll_show_options(struct seq_file *seq, struct dentry *dentry);
793 void ll_dirty_page_discard_warn(struct page *page, int ioret);
794 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
795                   struct super_block *, struct lookup_intent *);
796 int ll_obd_statfs(struct inode *inode, void __user *arg);
797 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *max_mdsize);
798 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *default_mdsize);
799 int ll_process_config(struct lustre_cfg *lcfg);
800
801 enum {
802         LUSTRE_OPC_MKDIR        = 0,
803         LUSTRE_OPC_SYMLINK      = 1,
804         LUSTRE_OPC_MKNOD        = 2,
805         LUSTRE_OPC_CREATE       = 3,
806         LUSTRE_OPC_ANY          = 5,
807 };
808
809 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
810                                       struct inode *i1, struct inode *i2,
811                                       const char *name, int namelen,
812                                       int mode, __u32 opc, void *data);
813 void ll_finish_md_op_data(struct md_op_data *op_data);
814 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg);
815 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen);
816 void ll_compute_rootsquash_state(struct ll_sb_info *sbi);
817 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req);
818
819 /* llite/llite_nfs.c */
820 extern const struct export_operations lustre_export_operations;
821 __u32 get_uuid2int(const char *name, int len);
822 void get_uuid2fsid(const char *name, int len, __kernel_fsid_t *fsid);
823 struct inode *search_inode_for_lustre(struct super_block *sb,
824                                       const struct lu_fid *fid);
825 int ll_dir_get_parent_fid(struct inode *dir, struct lu_fid *parent_fid);
826
827 /* llite/symlink.c */
828 extern const struct inode_operations ll_fast_symlink_inode_operations;
829
830 /* llite/llite_close.c */
831 struct ll_close_queue {
832         spinlock_t              lcq_lock;
833         struct list_head                lcq_head;
834         wait_queue_head_t               lcq_waitq;
835         struct completion       lcq_comp;
836         atomic_t                lcq_stop;
837 };
838
839 void vvp_write_pending(struct vvp_object *club, struct vvp_page *page);
840 void vvp_write_complete(struct vvp_object *club, struct vvp_page *page);
841
842 /**
843  * IO arguments for various VFS I/O interfaces.
844  */
845 struct vvp_io_args {
846         /** normal/splice */
847         enum vvp_io_subtype via_io_subtype;
848
849         union {
850                 struct {
851                         struct kiocb      *via_iocb;
852                         struct iov_iter   *via_iter;
853                 } normal;
854                 struct {
855                         struct pipe_inode_info  *via_pipe;
856                         unsigned int       via_flags;
857                 } splice;
858         } u;
859 };
860
861 struct ll_cl_context {
862         struct list_head         lcc_list;
863         void       *lcc_cookie;
864         const struct lu_env     *lcc_env;
865         struct cl_io   *lcc_io;
866         struct cl_page *lcc_page;
867 };
868
869 struct ll_thread_info {
870         struct vvp_io_args   lti_args;
871         struct ra_io_arg     lti_ria;
872         struct ll_cl_context lti_io_ctx;
873 };
874
875 extern struct lu_context_key ll_thread_key;
876 static inline struct ll_thread_info *ll_env_info(const struct lu_env *env)
877 {
878         struct ll_thread_info *lti;
879
880         lti = lu_context_key_get(&env->le_ctx, &ll_thread_key);
881         LASSERT(lti);
882         return lti;
883 }
884
885 static inline struct vvp_io_args *ll_env_args(const struct lu_env *env,
886                                               enum vvp_io_subtype type)
887 {
888         struct vvp_io_args *via = &ll_env_info(env)->lti_args;
889
890         via->via_io_subtype = type;
891
892         return via;
893 }
894
895 void ll_queue_done_writing(struct inode *inode, unsigned long flags);
896 void ll_close_thread_shutdown(struct ll_close_queue *lcq);
897 int ll_close_thread_start(struct ll_close_queue **lcq_ret);
898
899 /* llite/llite_mmap.c */
900
901 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last);
902 int ll_file_mmap(struct file *file, struct vm_area_struct *vma);
903 void policy_from_vma(ldlm_policy_data_t *policy, struct vm_area_struct *vma,
904                      unsigned long addr, size_t count);
905 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
906                                size_t count);
907
908 static inline void ll_invalidate_page(struct page *vmpage)
909 {
910         struct address_space *mapping = vmpage->mapping;
911         loff_t offset = vmpage->index << PAGE_SHIFT;
912
913         LASSERT(PageLocked(vmpage));
914         if (!mapping)
915                 return;
916
917         /*
918          * truncate_complete_page() calls
919          * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
920          */
921         ll_teardown_mmaps(mapping, offset, offset + PAGE_SIZE);
922         truncate_complete_page(mapping, vmpage);
923 }
924
925 #define    ll_s2sbi(sb) (s2lsi(sb)->lsi_llsbi)
926
927 /* don't need an addref as the sb_info should be holding one */
928 static inline struct obd_export *ll_s2dtexp(struct super_block *sb)
929 {
930         return ll_s2sbi(sb)->ll_dt_exp;
931 }
932
933 /* don't need an addref as the sb_info should be holding one */
934 static inline struct obd_export *ll_s2mdexp(struct super_block *sb)
935 {
936         return ll_s2sbi(sb)->ll_md_exp;
937 }
938
939 static inline struct client_obd *sbi2mdc(struct ll_sb_info *sbi)
940 {
941         struct obd_device *obd = sbi->ll_md_exp->exp_obd;
942
943         if (!obd)
944                 LBUG();
945         return &obd->u.cli;
946 }
947
948 /* FIXME: replace the name of this with LL_SB to conform to kernel stuff */
949 static inline struct ll_sb_info *ll_i2sbi(struct inode *inode)
950 {
951         return ll_s2sbi(inode->i_sb);
952 }
953
954 static inline struct obd_export *ll_i2dtexp(struct inode *inode)
955 {
956         return ll_s2dtexp(inode->i_sb);
957 }
958
959 static inline struct obd_export *ll_i2mdexp(struct inode *inode)
960 {
961         return ll_s2mdexp(inode->i_sb);
962 }
963
964 static inline struct lu_fid *ll_inode2fid(struct inode *inode)
965 {
966         struct lu_fid *fid;
967
968         LASSERT(inode);
969         fid = &ll_i2info(inode)->lli_fid;
970
971         return fid;
972 }
973
974 static inline __u64 ll_file_maxbytes(struct inode *inode)
975 {
976         return ll_i2info(inode)->lli_maxbytes;
977 }
978
979 /* llite/xattr.c */
980 extern const struct xattr_handler *ll_xattr_handlers[];
981
982 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size);
983
984 /**
985  * Common IO arguments for various VFS I/O interfaces.
986  */
987 int cl_sb_init(struct super_block *sb);
988 int cl_sb_fini(struct super_block *sb);
989 void ll_io_init(struct cl_io *io, const struct file *file, int write);
990
991 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
992                 struct ll_readahead_state *ras, unsigned long index,
993                 unsigned hit);
994 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len);
995 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which);
996
997 /* statahead.c */
998 #define LL_SA_RPC_MIN      2
999 #define LL_SA_RPC_DEF      32
1000 #define LL_SA_RPC_MAX      8192
1001
1002 #define LL_SA_CACHE_BIT  5
1003 #define LL_SA_CACHE_SIZE        (1 << LL_SA_CACHE_BIT)
1004 #define LL_SA_CACHE_MASK        (LL_SA_CACHE_SIZE - 1)
1005
1006 /* per inode struct, for dir only */
1007 struct ll_statahead_info {
1008         struct inode       *sai_inode;
1009         atomic_t            sai_refcount;   /* when access this struct, hold
1010                                              * refcount
1011                                              */
1012         unsigned int        sai_generation; /* generation for statahead */
1013         unsigned int        sai_max;    /* max ahead of lookup */
1014         __u64              sai_sent;       /* stat requests sent count */
1015         __u64              sai_replied;    /* stat requests which received
1016                                             * reply
1017                                             */
1018         __u64              sai_index;      /* index of statahead entry */
1019         __u64              sai_index_wait; /* index of entry which is the
1020                                             * caller is waiting for
1021                                             */
1022         __u64              sai_hit;     /* hit count */
1023         __u64              sai_miss;       /* miss count:
1024                                             * for "ls -al" case, it includes
1025                                             * hidden dentry miss;
1026                                             * for "ls -l" case, it does not
1027                                             * include hidden dentry miss.
1028                                             * "sai_miss_hidden" is used for
1029                                             * the later case.
1030                                             */
1031         unsigned int        sai_consecutive_miss; /* consecutive miss */
1032         unsigned int        sai_miss_hidden;/* "ls -al", but first dentry
1033                                              * is not a hidden one
1034                                              */
1035         unsigned int        sai_skip_hidden;/* skipped hidden dentry count */
1036         unsigned int        sai_ls_all:1,   /* "ls -al", do stat-ahead for
1037                                              * hidden entries
1038                                              */
1039                                 sai_agl_valid:1,/* AGL is valid for the dir */
1040                                 sai_in_readpage:1;/* statahead is in readdir() */
1041         wait_queue_head_t       sai_waitq;      /* stat-ahead wait queue */
1042         struct ptlrpc_thread    sai_thread;     /* stat-ahead thread */
1043         struct ptlrpc_thread    sai_agl_thread; /* AGL thread */
1044         struct list_head        sai_entries;    /* entry list */
1045         struct list_head        sai_entries_received; /* entries returned */
1046         struct list_head        sai_entries_stated;   /* entries stated */
1047         struct list_head        sai_entries_agl; /* AGL entries to be sent */
1048         struct list_head        sai_cache[LL_SA_CACHE_SIZE];
1049         spinlock_t              sai_cache_lock[LL_SA_CACHE_SIZE];
1050         atomic_t                sai_cache_count; /* entry count in cache */
1051 };
1052
1053 int do_statahead_enter(struct inode *dir, struct dentry **dentry,
1054                        int only_unplug);
1055 void ll_authorize_statahead(struct inode *dir, void *key);
1056 void ll_deauthorize_statahead(struct inode *dir, void *key);
1057
1058 blkcnt_t dirty_cnt(struct inode *inode);
1059
1060 int cl_glimpse_size0(struct inode *inode, int agl);
1061 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
1062                     struct inode *inode, struct cl_object *clob, int agl);
1063
1064 static inline int cl_glimpse_size(struct inode *inode)
1065 {
1066         return cl_glimpse_size0(inode, 0);
1067 }
1068
1069 static inline int cl_agl(struct inode *inode)
1070 {
1071         return cl_glimpse_size0(inode, 1);
1072 }
1073
1074 static inline int ll_glimpse_size(struct inode *inode)
1075 {
1076         struct ll_inode_info *lli = ll_i2info(inode);
1077         int rc;
1078
1079         down_read(&lli->lli_glimpse_sem);
1080         rc = cl_glimpse_size(inode);
1081         lli->lli_glimpse_time = cfs_time_current();
1082         up_read(&lli->lli_glimpse_sem);
1083         return rc;
1084 }
1085
1086 static inline void
1087 ll_statahead_mark(struct inode *dir, struct dentry *dentry)
1088 {
1089         struct ll_inode_info     *lli = ll_i2info(dir);
1090         struct ll_statahead_info *sai = lli->lli_sai;
1091         struct ll_dentry_data    *ldd = ll_d2d(dentry);
1092
1093         /* not the same process, don't mark */
1094         if (lli->lli_opendir_pid != current_pid())
1095                 return;
1096
1097         LASSERT(ldd);
1098         if (sai)
1099                 ldd->lld_sa_generation = sai->sai_generation;
1100 }
1101
1102 static inline bool
1103 dentry_need_statahead(struct inode *dir, struct dentry *dentry)
1104 {
1105         struct ll_inode_info  *lli;
1106         struct ll_dentry_data *ldd;
1107
1108         if (ll_i2sbi(dir)->ll_sa_max == 0)
1109                 return false;
1110
1111         lli = ll_i2info(dir);
1112
1113         /*
1114          * statahead is not allowed for this dir, there may be three causes:
1115          * 1. dir is not opened.
1116          * 2. statahead hit ratio is too low.
1117          * 3. previous stat started statahead thread failed.
1118          */
1119         if (!lli->lli_sa_enabled)
1120                 return false;
1121
1122         /* not the same process, don't statahead */
1123         if (lli->lli_opendir_pid != current_pid())
1124                 return false;
1125
1126         ldd = ll_d2d(dentry);
1127         /*
1128          * When stats a dentry, the system trigger more than once "revalidate"
1129          * or "lookup", for "getattr", for "getxattr", and maybe for others.
1130          * Under patchless client mode, the operation intent is not accurate,
1131          * which maybe misguide the statahead thread. For example:
1132          * The "revalidate" call for "getattr" and "getxattr" of a dentry maybe
1133          * have the same operation intent -- "IT_GETATTR".
1134          * In fact, one dentry should has only one chance to interact with the
1135          * statahead thread, otherwise the statahead windows will be confused.
1136          * The solution is as following:
1137          * Assign "lld_sa_generation" with "sai_generation" when a dentry
1138          * "IT_GETATTR" for the first time, and the subsequent "IT_GETATTR"
1139          * will bypass interacting with statahead thread for checking:
1140          * "lld_sa_generation == lli_sai->sai_generation"
1141          */
1142         if (ldd && lli->lli_sai &&
1143             ldd->lld_sa_generation == lli->lli_sai->sai_generation)
1144                 return false;
1145
1146         return true;
1147 }
1148
1149 static inline int
1150 ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int only_unplug)
1151 {
1152         if (!dentry_need_statahead(dir, *dentryp))
1153                 return -EAGAIN;
1154
1155         return do_statahead_enter(dir, dentryp, only_unplug);
1156 }
1157
1158 /* llite ioctl register support routine */
1159 enum llioc_iter {
1160         LLIOC_CONT = 0,
1161         LLIOC_STOP
1162 };
1163
1164 #define LLIOC_MAX_CMD      256
1165
1166 /*
1167  * Rules to write a callback function:
1168  *
1169  * Parameters:
1170  *  @magic: Dynamic ioctl call routine will feed this value with the pointer
1171  *      returned to ll_iocontrol_register.  Callback functions should use this
1172  *      data to check the potential collasion of ioctl cmd. If collasion is
1173  *      found, callback function should return LLIOC_CONT.
1174  *  @rcp: The result of ioctl command.
1175  *
1176  *  Return values:
1177  *      If @magic matches the pointer returned by ll_iocontrol_data, the
1178  *      callback should return LLIOC_STOP; return LLIOC_STOP otherwise.
1179  */
1180 typedef enum llioc_iter (*llioc_callback_t)(struct inode *inode,
1181                 struct file *file, unsigned int cmd, unsigned long arg,
1182                 void *magic, int *rcp);
1183
1184 /* export functions */
1185 /* Register ioctl block dynamatically for a regular file.
1186  *
1187  * @cmd: the array of ioctl command set
1188  * @count: number of commands in the @cmd
1189  * @cb: callback function, it will be called if an ioctl command is found to
1190  *      belong to the command list @cmd.
1191  *
1192  * Return value:
1193  *      A magic pointer will be returned if success;
1194  *      otherwise, NULL will be returned.
1195  * */
1196 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd);
1197 void ll_iocontrol_unregister(void *magic);
1198
1199 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
1200                        enum cl_fsync_mode mode, int ignore_layout);
1201
1202 /** direct write pages */
1203 struct ll_dio_pages {
1204         /** page array to be written. we don't support
1205          * partial pages except the last one.
1206          */
1207         struct page **ldp_pages;
1208         /* offset of each page */
1209         loff_t       *ldp_offsets;
1210         /** if ldp_offsets is NULL, it means a sequential
1211          * pages to be written, then this is the file offset
1212          * of the first page.
1213          */
1214         loff_t  ldp_start_offset;
1215         /** how many bytes are to be written. */
1216         size_t  ldp_size;
1217         /** # of pages in the array. */
1218         int        ldp_nr;
1219 };
1220
1221 static inline void cl_stats_tally(struct cl_device *dev, enum cl_req_type crt,
1222                                   int rc)
1223 {
1224         int opc = (crt == CRT_READ) ? LPROC_LL_OSC_READ :
1225                                       LPROC_LL_OSC_WRITE;
1226
1227         ll_stats_ops_tally(ll_s2sbi(cl2vvp_dev(dev)->vdv_sb), opc, rc);
1228 }
1229
1230 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
1231                            int rw, struct inode *inode,
1232                            struct ll_dio_pages *pv);
1233
1234 static inline int ll_file_nolock(const struct file *file)
1235 {
1236         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1237         struct inode *inode = file_inode(file);
1238
1239         return ((fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
1240                 (ll_i2sbi(inode)->ll_flags & LL_SBI_NOLCK));
1241 }
1242
1243 static inline void ll_set_lock_data(struct obd_export *exp, struct inode *inode,
1244                                     struct lookup_intent *it, __u64 *bits)
1245 {
1246         if (!it->it_lock_set) {
1247                 struct lustre_handle handle;
1248
1249                 /* If this inode is a remote object, it will get two
1250                  * separate locks in different namespaces, Master MDT,
1251                  * where the name entry is, will grant LOOKUP lock,
1252                  * remote MDT, where the object is, will grant
1253                  * UPDATE|PERM lock. The inode will be attached to both
1254                  * LOOKUP and PERM locks, so revoking either locks will
1255                  * case the dcache being cleared
1256                  */
1257                 if (it->it_remote_lock_mode) {
1258                         handle.cookie = it->it_remote_lock_handle;
1259                         CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"%p for remote lock %#llx\n",
1260                                PFID(ll_inode2fid(inode)), inode,
1261                                handle.cookie);
1262                         md_set_lock_data(exp, &handle, inode, NULL);
1263                 }
1264
1265                 handle.cookie = it->it_lock_handle;
1266
1267                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"%p for lock %#llx\n",
1268                        PFID(ll_inode2fid(inode)), inode, handle.cookie);
1269
1270                 md_set_lock_data(exp, &handle, inode, &it->it_lock_bits);
1271                 it->it_lock_set = 1;
1272         }
1273
1274         if (bits)
1275                 *bits = it->it_lock_bits;
1276 }
1277
1278 static inline int d_lustre_invalid(const struct dentry *dentry)
1279 {
1280         struct ll_dentry_data *lld = ll_d2d(dentry);
1281
1282         return !lld || lld->lld_invalid;
1283 }
1284
1285 static inline void __d_lustre_invalidate(struct dentry *dentry)
1286 {
1287         struct ll_dentry_data *lld = ll_d2d(dentry);
1288
1289         if (lld)
1290                 lld->lld_invalid = 1;
1291 }
1292
1293 /*
1294  * Mark dentry INVALID, if dentry refcount is zero (this is normally case for
1295  * ll_md_blocking_ast), unhash this dentry, and let dcache to reclaim it later;
1296  * else dput() of the last refcount will unhash this dentry and kill it.
1297  */
1298 static inline void d_lustre_invalidate(struct dentry *dentry, int nested)
1299 {
1300         CDEBUG(D_DENTRY, "invalidate dentry %pd (%p) parent %p inode %p refc %d\n",
1301                dentry, dentry,
1302                dentry->d_parent, d_inode(dentry), d_count(dentry));
1303
1304         spin_lock_nested(&dentry->d_lock,
1305                          nested ? DENTRY_D_LOCK_NESTED : DENTRY_D_LOCK_NORMAL);
1306         __d_lustre_invalidate(dentry);
1307         /*
1308          * We should be careful about dentries created by d_obtain_alias().
1309          * These dentries are not put in the dentry tree, instead they are
1310          * linked to sb->s_anon through dentry->d_hash.
1311          * shrink_dcache_for_umount() shrinks the tree and sb->s_anon list.
1312          * If we unhashed such a dentry, unmount would not be able to find
1313          * it and busy inodes would be reported.
1314          */
1315         if (d_count(dentry) == 0 && !(dentry->d_flags & DCACHE_DISCONNECTED))
1316                 __d_drop(dentry);
1317         spin_unlock(&dentry->d_lock);
1318 }
1319
1320 static inline void d_lustre_revalidate(struct dentry *dentry)
1321 {
1322         spin_lock(&dentry->d_lock);
1323         LASSERT(ll_d2d(dentry));
1324         ll_d2d(dentry)->lld_invalid = 0;
1325         spin_unlock(&dentry->d_lock);
1326 }
1327
1328 enum {
1329         LL_LAYOUT_GEN_NONE  = ((__u32)-2),      /* layout lock was cancelled */
1330         LL_LAYOUT_GEN_EMPTY = ((__u32)-1)       /* for empty layout */
1331 };
1332
1333 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf);
1334 int ll_layout_refresh(struct inode *inode, __u32 *gen);
1335 int ll_layout_restore(struct inode *inode, loff_t start, __u64 length);
1336
1337 int ll_xattr_init(void);
1338 void ll_xattr_fini(void);
1339
1340 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1341                     struct cl_page *page, enum cl_req_type crt);
1342
1343 /* lcommon_cl.c */
1344 int cl_setattr_ost(struct inode *inode, const struct iattr *attr);
1345
1346 extern struct lu_env *cl_inode_fini_env;
1347 extern int cl_inode_fini_refcheck;
1348
1349 int cl_file_inode_init(struct inode *inode, struct lustre_md *md);
1350 void cl_inode_fini(struct inode *inode);
1351 int cl_local_size(struct inode *inode);
1352
1353 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32);
1354 __u32 cl_fid_build_gen(const struct lu_fid *fid);
1355
1356 #endif /* LLITE_INTERNAL_H */