Whamcloud - gitweb
New release 2.15.64
[fs/lustre-release.git] / lustre / llite / pcc.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) 2017, DDN Storage Corporation.
24  */
25 /*
26  *
27  * Persistent Client Cache
28  *
29  * Author: Li Xi <lixi@ddn.com>
30  */
31
32 #ifndef LLITE_PCC_H
33 #define LLITE_PCC_H
34
35 #include <linux/types.h>
36 #include <linux/fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/mm.h>
39 #include <uapi/linux/lustre/lustre_user.h>
40
41 extern struct kmem_cache *pcc_inode_slab;
42
43 #define LPROCFS_WR_PCC_MAX_CMD 4096
44
45 /* User/Group/Project ID */
46 struct pcc_match_id {
47         __u32                   pmi_id;
48         struct list_head        pmi_linkage;
49 };
50
51 /* Lazy file size */
52 struct pcc_match_size {
53         __u64                   pms_size;
54         struct list_head        pms_linkage;
55 };
56
57 /* wildcard file name */
58 struct pcc_match_fname {
59         char                    *pmf_name;
60         struct list_head         pmf_linkage;
61 };
62
63 enum pcc_field {
64         PCC_FIELD_UID,
65         PCC_FIELD_GID,
66         PCC_FIELD_PROJID,
67         PCC_FIELD_FNAME,
68         PCC_FIELD_SIZE,
69         PCC_FIELD_MTIME,
70         PCC_FIELD_MAX
71 };
72
73 enum pcc_field_op {
74         PCC_FIELD_OP_EQ         = 0,
75         PCC_FIELD_OP_LT         = 1,
76         PCC_FIELD_OP_GT         = 2,
77         PCC_FIELD_OP_MAX        = 3,
78         PCC_FIELD_OP_INV        = PCC_FIELD_MAX,
79 };
80
81 struct pcc_expression {
82         struct list_head        pe_linkage;
83         enum pcc_field          pe_field;
84         enum pcc_field_op       pe_opc;
85         union {
86                 struct list_head        pe_cond;
87                 __u64                   pe_size;  /* file size in bytes */
88                 __u64                   pe_mtime; /* relative age in seconds */
89                 __u32                   pe_id;    /* UID/GID/PROJID */
90         };
91 };
92
93 struct pcc_conjunction {
94         /* link to disjunction */
95         struct list_head        pc_linkage;
96         /* list of logical conjunction */
97         struct list_head        pc_expressions;
98 };
99
100 /**
101  * Match rule for auto PCC-cached files.
102  */
103 struct pcc_match_rule {
104         char                    *pmr_conds_str;
105         struct list_head         pmr_conds;
106 };
107
108 struct pcc_matcher {
109         __u32            pm_uid;
110         __u32            pm_gid;
111         __u32            pm_projid;
112         __u64            pm_size;
113         __u64            pm_mtime;
114         struct qstr     *pm_name;
115 };
116
117 enum pcc_dataset_flags {
118         PCC_DATASET_INVALID     = 0x0,
119         /* Indicate that known the file is not in PCC. */
120         PCC_DATASET_NONE        = 0x01,
121         /* Try auto attach at open, enabled by default */
122         PCC_DATASET_OPEN_ATTACH = 0x02,
123         /* Try auto attach during IO when layout refresh, enabled by default */
124         PCC_DATASET_IO_ATTACH   = 0x04,
125         /* Try auto attach at stat */
126         PCC_DATASET_STAT_ATTACH = 0x08,
127         PCC_DATASET_AUTO_ATTACH = PCC_DATASET_OPEN_ATTACH |
128                                   PCC_DATASET_IO_ATTACH |
129                                   PCC_DATASET_STAT_ATTACH,
130         /* PCC backend is only used for PCC-RW */
131         PCC_DATASET_PCCRW       = 0x10,
132         /* PCC backend is only used for PCC-RO */
133         PCC_DATASET_PCCRO       = 0x20,
134         /* PCC backend provides caching services for both PCC-RW and PCC-RO */
135         PCC_DATASET_PCC_ALL     = PCC_DATASET_PCCRW | PCC_DATASET_PCCRO,
136         /* Default PCC caching mode: PCC-RO mode */
137         PCC_DATASET_PCC_DEFAULT = PCC_DATASET_PCCRO,
138 };
139
140 struct pcc_dataset {
141         __u32                   pccd_rwid;       /* Archive ID */
142         __u32                   pccd_roid;       /* Readonly ID */
143         struct pcc_match_rule   pccd_rule;       /* Match rule */
144         enum pcc_dataset_flags  pccd_flags;      /* Flags of PCC backend */
145         char                    pccd_pathname[PATH_MAX]; /* full path */
146         struct path             pccd_path;       /* Root path */
147         struct list_head        pccd_linkage;  /* Linked to pccs_datasets */
148         atomic_t                pccd_refcount; /* Reference count */
149         enum hsmtool_type       pccd_hsmtool_type; /*HSM copytool type */
150 };
151
152 struct pcc_super {
153         /* Protect pccs_datasets */
154         struct rw_semaphore      pccs_rw_sem;
155         /* List of datasets */
156         struct list_head         pccs_datasets;
157         /* creds of process who forced instantiation of super block */
158         const struct cred       *pccs_cred;
159         /*
160          * Gobal PCC Generation: it will be increased once the configuration
161          * for PCC is changed, i.e. add or delete a PCC backend, modify the
162          * parameters for PCC.
163          */
164         __u64                    pccs_generation;
165 };
166
167 struct pcc_inode {
168         struct ll_inode_info    *pcci_lli;
169         /* Cache path on local file system */
170         struct path              pcci_path;
171         /*
172          * If reference count is 0, then the cache is not inited, if 1, then
173          * no one is using it.
174          */
175         atomic_t                 pcci_refcount;
176         /* Whether readonly or readwrite PCC */
177         enum lu_pcc_type         pcci_type:8;
178         /* Whether the inode attr is cached locally */
179         bool                     pcci_attr_valid;
180         /* Whether the PCC inode is unlinked at detach */
181         bool                     pcci_unlinked;
182         /* Layout generation */
183         __u32                    pcci_layout_gen;
184         /*
185          * How many IOs are on going on this cached object. Layout can be
186          * changed only if there is no active IO.
187          */
188         atomic_t                 pcci_active_ios;
189         /* Waitq - wait for PCC I/O completion. */
190         wait_queue_head_t        pcci_waitq;
191 };
192
193 struct pcc_file {
194         /* Opened cache file */
195         struct file             *pccf_file;
196         /* Whether readonly or readwrite PCC */
197         enum lu_pcc_type         pccf_type;
198 };
199
200 enum pcc_io_type {
201         /* read system call */
202         PIT_READ = 1,
203         /* write system call */
204         PIT_WRITE,
205         /* truncate, utime system calls */
206         PIT_SETATTR,
207         /* stat system call */
208         PIT_GETATTR,
209         /* mmap write handling */
210         PIT_PAGE_MKWRITE,
211         /* page fault handling */
212         PIT_FAULT,
213         /* fsync system call handling */
214         PIT_FSYNC,
215         /* splice_read system call */
216         PIT_SPLICE_READ,
217         /* open system call */
218         PIT_OPEN
219 };
220
221 enum pcc_cmd_type {
222         PCC_ADD_DATASET = 0,
223         PCC_DEL_DATASET,
224         PCC_CLEAR_ALL,
225 };
226
227 struct pcc_cmd {
228         enum pcc_cmd_type                        pccc_cmd;
229         char                                    *pccc_pathname;
230         union {
231                 struct pcc_cmd_add {
232                         __u32                    pccc_rwid;
233                         __u32                    pccc_roid;
234                         struct list_head         pccc_conds;
235                         char                    *pccc_conds_str;
236                         enum pcc_dataset_flags   pccc_flags;
237                         enum hsmtool_type        pccc_hsmtool_type;
238                 } pccc_add;
239                 struct pcc_cmd_del {
240                         __u32                    pccc_pad;
241                 } pccc_del;
242         } u;
243 };
244
245 struct pcc_create_attach {
246         struct pcc_dataset *pca_dataset;
247         struct dentry *pca_dentry;
248 };
249
250 int pcc_super_init(struct pcc_super *super);
251 void pcc_super_fini(struct pcc_super *super);
252 int pcc_cmd_handle(char *buffer, unsigned long count,
253                    struct pcc_super *super);
254 int pcc_super_dump(struct pcc_super *super, struct seq_file *m);
255 int pcc_readwrite_attach(struct file *file, struct inode *inode,
256                          __u32 arch_id);
257 int pcc_readwrite_attach_fini(struct file *file, struct inode *inode,
258                               __u32 gen, bool lease_broken, int rc,
259                               bool attached);
260 int pcc_ioctl_attach(struct file *file, struct inode *inode,
261                      struct lu_pcc_attach *attach);
262 int pcc_ioctl_detach(struct inode *inode, __u32 *flags);
263 int pcc_ioctl_state(struct file *file, struct inode *inode,
264                     struct lu_pcc_state *state);
265 void pcc_file_init(struct pcc_file *pccf);
266 int pcc_file_open(struct inode *inode, struct file *file);
267 void pcc_file_release(struct inode *inode, struct file *file);
268 ssize_t pcc_file_read_iter(struct kiocb *iocb, struct iov_iter *iter,
269                            bool *cached);
270 ssize_t pcc_file_write_iter(struct kiocb *iocb, struct iov_iter *iter,
271                             bool *cached);
272 int pcc_inode_getattr(struct inode *inode, u32 request_mask,
273                       unsigned int flags, bool *cached);
274 int pcc_inode_setattr(struct inode *inode, struct iattr *attr, bool *cached);
275 #ifdef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
276 ssize_t pcc_file_splice_read(struct file *in_file, loff_t *ppos,
277                              struct pipe_inode_info *pipe, size_t count,
278                              unsigned int flags);
279 #endif
280 int pcc_fsync(struct file *file, loff_t start, loff_t end,
281               int datasync, bool *cached);
282 int pcc_file_mmap(struct file *file, struct vm_area_struct *vma, bool *cached);
283 void pcc_vm_open(struct vm_area_struct *vma);
284 void pcc_vm_close(struct vm_area_struct *vma);
285 int pcc_fault(struct vm_area_struct *mva, struct vm_fault *vmf, bool *cached);
286 int pcc_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
287                      bool *cached);
288 int pcc_inode_create(struct super_block *sb, struct pcc_dataset *dataset,
289                      struct lu_fid *fid, struct dentry **pcc_dentry);
290 int pcc_inode_create_fini(struct inode *inode, struct pcc_create_attach *pca);
291 void pcc_create_attach_cleanup(struct super_block *sb,
292                                struct pcc_create_attach *pca);
293 struct pcc_dataset *pcc_dataset_match_get(struct pcc_super *super,
294                                           enum lu_pcc_type type,
295                                           struct pcc_matcher *matcher);
296 void pcc_dataset_put(struct pcc_dataset *dataset);
297 void pcc_inode_free(struct inode *inode);
298 void pcc_layout_invalidate(struct inode *inode);
299 #endif /* LLITE_PCC_H */