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