Whamcloud - gitweb
LU-12275 sec: ioctls to handle encryption policies
[fs/lustre-release.git] / lustre / llite / vvp_object.c
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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, 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  * cl_object implementation for VVP layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <linux/user_namespace.h>
40 #include <linux/uidgid.h>
41
42 #include <libcfs/libcfs.h>
43
44 #include <obd.h>
45 #include "llite_internal.h"
46 #include "vvp_internal.h"
47
48 /*****************************************************************************
49  *
50  * Object operations.
51  *
52  */
53
54 int vvp_object_invariant(const struct cl_object *obj)
55 {
56         struct inode            *inode  = vvp_object_inode(obj);
57         struct ll_inode_info    *lli    = ll_i2info(inode);
58
59         return (S_ISREG(inode->i_mode) || inode->i_mode == 0) &&
60                lli->lli_clob == obj;
61 }
62
63 static int vvp_object_print(const struct lu_env *env, void *cookie,
64                             lu_printer_t p, const struct lu_object *o)
65 {
66         struct vvp_object    *obj   = lu2vvp(o);
67         struct inode         *inode = obj->vob_inode;
68         struct ll_inode_info *lli;
69
70         (*p)(env, cookie, "(%d %d) inode: %p ",
71              atomic_read(&obj->vob_transient_pages),
72              atomic_read(&obj->vob_mmap_cnt),
73              inode);
74         if (inode) {
75                 lli = ll_i2info(inode);
76                 (*p)(env, cookie, "%lu/%u %o %u %d %p "DFID,
77                      inode->i_ino, inode->i_generation, inode->i_mode,
78                      inode->i_nlink, atomic_read(&inode->i_count),
79                      lli->lli_clob, PFID(&lli->lli_fid));
80         }
81         return 0;
82 }
83
84 static int vvp_attr_get(const struct lu_env *env, struct cl_object *obj,
85                         struct cl_attr *attr)
86 {
87         struct inode *inode = vvp_object_inode(obj);
88
89         /*
90          * lov overwrites most of these fields in
91          * lov_attr_get()->...lov_merge_lvb_kms(), except when inode
92          * attributes are newer.
93          */
94
95         attr->cat_size = i_size_read(inode);
96         attr->cat_mtime = inode->i_mtime.tv_sec;
97         attr->cat_atime = inode->i_atime.tv_sec;
98         attr->cat_ctime = inode->i_ctime.tv_sec;
99         attr->cat_blocks = inode->i_blocks;
100         attr->cat_uid = from_kuid(&init_user_ns, inode->i_uid);
101         attr->cat_gid = from_kgid(&init_user_ns, inode->i_gid);
102         attr->cat_projid = ll_i2info(inode)->lli_projid;
103         /* KMS is not known by this layer */
104         return 0; /* layers below have to fill in the rest */
105 }
106
107 static int vvp_attr_update(const struct lu_env *env, struct cl_object *obj,
108                            const struct cl_attr *attr, unsigned valid)
109 {
110         struct inode *inode = vvp_object_inode(obj);
111
112         if (valid & CAT_UID)
113                 inode->i_uid = make_kuid(&init_user_ns, attr->cat_uid);
114         if (valid & CAT_GID)
115                 inode->i_gid = make_kgid(&init_user_ns, attr->cat_gid);
116         if (valid & CAT_ATIME)
117                 inode->i_atime.tv_sec = attr->cat_atime;
118         if (valid & CAT_MTIME)
119                 inode->i_mtime.tv_sec = attr->cat_mtime;
120         if (valid & CAT_CTIME)
121                 inode->i_ctime.tv_sec = attr->cat_ctime;
122         if (0 && valid & CAT_SIZE)
123                 i_size_write(inode, attr->cat_size);
124         if (valid & CAT_PROJID)
125                 ll_i2info(inode)->lli_projid = attr->cat_projid;
126         /* not currently necessary */
127         if (0 && valid & (CAT_UID|CAT_GID|CAT_SIZE|CAT_PROJID))
128                 mark_inode_dirty(inode);
129         return 0;
130 }
131
132 static int vvp_conf_set(const struct lu_env *env, struct cl_object *obj,
133                         const struct cl_object_conf *conf)
134 {
135         struct ll_inode_info *lli = ll_i2info(conf->coc_inode);
136
137         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
138                 CDEBUG(D_VFSTRACE, DFID ": losing layout lock\n",
139                        PFID(&lli->lli_fid));
140
141                 ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
142
143                 /* Clean up page mmap for this inode.
144                  * The reason for us to do this is that if the page has
145                  * already been installed into memory space, the process
146                  * can access it without interacting with lustre, so this
147                  * page may be stale due to layout change, and the process
148                  * will never be notified.
149                  * This operation is expensive but mmap processes have to pay
150                  * a price themselves. */
151                 unmap_mapping_range(conf->coc_inode->i_mapping,
152                                     0, OBD_OBJECT_EOF, 0);
153                 pcc_layout_invalidate(conf->coc_inode);
154         }
155         return 0;
156 }
157
158 static int vvp_prune(const struct lu_env *env, struct cl_object *obj)
159 {
160         struct inode *inode = vvp_object_inode(obj);
161         int rc;
162         ENTRY;
163
164         rc = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, CL_FSYNC_LOCAL, 1);
165         if (rc < 0) {
166                 CDEBUG(D_VFSTRACE, DFID ": writeback failed: %d\n",
167                        PFID(lu_object_fid(&obj->co_lu)), rc);
168                 RETURN(rc);
169         }
170
171         truncate_inode_pages(inode->i_mapping, 0);
172         if (inode->i_mapping->nrpages) {
173                 CDEBUG(D_VFSTRACE, DFID ": still has %lu pages remaining\n",
174                        PFID(lu_object_fid(&obj->co_lu)),
175                        inode->i_mapping->nrpages);
176                 RETURN(-EIO);
177         }
178
179         RETURN(0);
180 }
181
182 static int vvp_object_glimpse(const struct lu_env *env,
183                               const struct cl_object *obj, struct ost_lvb *lvb)
184 {
185         struct inode *inode = vvp_object_inode(obj);
186
187         ENTRY;
188         lvb->lvb_mtime = inode->i_mtime.tv_sec;
189         lvb->lvb_atime = inode->i_atime.tv_sec;
190         lvb->lvb_ctime = inode->i_ctime.tv_sec;
191
192         /*
193          * LU-417: Add dirty pages block count lest i_blocks reports 0, some
194          * "cp" or "tar" on remote node may think it's a completely sparse file
195          * and skip it.
196          */
197         if (lvb->lvb_size > 0 && lvb->lvb_blocks == 0)
198                 lvb->lvb_blocks = dirty_cnt(inode);
199
200         RETURN(0);
201 }
202
203 static void vvp_req_attr_set(const struct lu_env *env, struct cl_object *obj,
204                              struct cl_req_attr *attr)
205 {
206         struct inode *inode;
207         struct obdo  *oa;
208         u64 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLUID | OBD_MD_FLGID;
209
210         oa = attr->cra_oa;
211         inode = vvp_object_inode(obj);
212
213         if (attr->cra_type == CRT_WRITE) {
214                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
215                 obdo_set_o_projid(oa, ll_i2info(inode)->lli_projid);
216         } else if (attr->cra_type == CRT_READ) {
217                 valid_flags |= OBD_MD_FLATIME;
218         }
219         obdo_from_inode(oa, inode, valid_flags & attr->cra_flags);
220         obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
221         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_INVALID_PFID))
222                 oa->o_parent_oid++;
223         memcpy(attr->cra_jobid, ll_i2info(inode)->lli_jobid,
224                sizeof(attr->cra_jobid));
225 }
226
227 static const struct cl_object_operations vvp_ops = {
228         .coo_page_init    = vvp_page_init,
229         .coo_io_init      = vvp_io_init,
230         .coo_attr_get     = vvp_attr_get,
231         .coo_attr_update  = vvp_attr_update,
232         .coo_conf_set     = vvp_conf_set,
233         .coo_prune        = vvp_prune,
234         .coo_glimpse      = vvp_object_glimpse,
235         .coo_req_attr_set = vvp_req_attr_set
236 };
237
238 static int vvp_object_init0(const struct lu_env *env,
239                             struct vvp_object *vob,
240                             const struct cl_object_conf *conf)
241 {
242         vob->vob_inode = conf->coc_inode;
243         atomic_set(&vob->vob_transient_pages, 0);
244         cl_object_page_init(&vob->vob_cl, sizeof(struct vvp_page));
245         return 0;
246 }
247
248 static int vvp_object_init(const struct lu_env *env, struct lu_object *obj,
249                            const struct lu_object_conf *conf)
250 {
251         struct vvp_device *dev = lu2vvp_dev(obj->lo_dev);
252         struct vvp_object *vob = lu2vvp(obj);
253         struct lu_object  *below;
254         struct lu_device  *under;
255         int result;
256
257         under = &dev->vdv_next->cd_lu_dev;
258         below = under->ld_ops->ldo_object_alloc(env, obj->lo_header, under);
259         if (below != NULL) {
260                 const struct cl_object_conf *cconf;
261
262                 cconf = lu2cl_conf(conf);
263                 lu_object_add(obj, below);
264                 result = vvp_object_init0(env, vob, cconf);
265         } else
266                 result = -ENOMEM;
267
268         return result;
269 }
270
271 static void vvp_object_free_rcu(struct rcu_head *head)
272 {
273         struct vvp_object *vob = container_of(head, struct vvp_object,
274                                               vob_header.coh_lu.loh_rcu);
275
276         kmem_cache_free(vvp_object_kmem, vob);
277 }
278
279 static void vvp_object_free(const struct lu_env *env, struct lu_object *obj)
280 {
281         struct vvp_object *vob = lu2vvp(obj);
282
283         lu_object_fini(obj);
284         lu_object_header_fini(obj->lo_header);
285         OBD_FREE_PRE(vob, sizeof(*vob), "slab-freed");
286         call_rcu(&vob->vob_header.coh_lu.loh_rcu, vvp_object_free_rcu);
287 }
288
289 static const struct lu_object_operations vvp_lu_obj_ops = {
290         .loo_object_init        = vvp_object_init,
291         .loo_object_free        = vvp_object_free,
292         .loo_object_print       = vvp_object_print,
293 };
294
295 struct vvp_object *cl_inode2vvp(struct inode *inode)
296 {
297         struct ll_inode_info *lli = ll_i2info(inode);
298         struct cl_object     *obj = lli->lli_clob;
299         struct lu_object     *lu;
300
301         LASSERT(obj != NULL);
302         lu = lu_object_locate(obj->co_lu.lo_header, &vvp_device_type);
303         LASSERT(lu != NULL);
304
305         return lu2vvp(lu);
306 }
307
308 struct lu_object *vvp_object_alloc(const struct lu_env *env,
309                                    const struct lu_object_header *unused,
310                                    struct lu_device *dev)
311 {
312         struct vvp_object *vob;
313         struct lu_object  *obj;
314
315         OBD_SLAB_ALLOC_PTR_GFP(vob, vvp_object_kmem, GFP_NOFS);
316         if (vob != NULL) {
317                 struct cl_object_header *hdr;
318
319                 obj = &vob->vob_cl.co_lu;
320                 hdr = &vob->vob_header;
321                 cl_object_header_init(hdr);
322                 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
323
324                 lu_object_init(obj, &hdr->coh_lu, dev);
325                 lu_object_add_top(&hdr->coh_lu, obj);
326
327                 vob->vob_cl.co_ops = &vvp_ops;
328                 obj->lo_ops = &vvp_lu_obj_ops;
329         } else
330                 obj = NULL;
331         return obj;
332 }