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