4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * cl_object implementation for VVP layer.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
36 #define DEBUG_SUBSYSTEM S_LLITE
38 #include <linux/user_namespace.h>
39 #include <linux/uidgid.h>
41 #include <libcfs/libcfs.h>
44 #include "llite_internal.h"
45 #include "vvp_internal.h"
47 /* Object operations.*/
48 int vvp_object_invariant(const struct cl_object *obj)
50 struct inode *inode = vvp_object_inode(obj);
51 struct ll_inode_info *lli = ll_i2info(inode);
53 return (S_ISREG(inode->i_mode) || inode->i_mode == 0) &&
57 static int vvp_object_print(const struct lu_env *env, void *cookie,
58 lu_printer_t p, const struct lu_object *o)
60 struct vvp_object *obj = lu2vvp(o);
61 struct inode *inode = obj->vob_inode;
62 struct ll_inode_info *lli;
64 (*p)(env, cookie, "(%d) inode: %p ",
65 atomic_read(&obj->vob_mmap_cnt),
68 lli = ll_i2info(inode);
69 (*p)(env, cookie, "%lu/%u %o %u %d %p "DFID,
70 inode->i_ino, inode->i_generation, inode->i_mode,
71 inode->i_nlink, atomic_read(&inode->i_count),
72 lli->lli_clob, PFID(&lli->lli_fid));
77 static int vvp_attr_get(const struct lu_env *env, struct cl_object *obj,
80 struct inode *inode = vvp_object_inode(obj);
83 * lov overwrites most of these fields in
84 * lov_attr_get()->...lov_merge_lvb_kms(), except when inode
85 * attributes are newer.
88 attr->cat_size = i_size_read(inode);
89 attr->cat_mtime = inode_get_mtime_sec(inode);
90 attr->cat_atime = inode_get_atime_sec(inode);
91 attr->cat_ctime = inode_get_ctime_sec(inode);
92 attr->cat_blocks = inode->i_blocks;
93 attr->cat_uid = from_kuid(&init_user_ns, inode->i_uid);
94 attr->cat_gid = from_kgid(&init_user_ns, inode->i_gid);
95 attr->cat_projid = ll_i2info(inode)->lli_projid;
96 /* KMS is not known by this layer */
97 return 0; /* layers below have to fill in the rest */
100 static int vvp_attr_update(const struct lu_env *env, struct cl_object *obj,
101 const struct cl_attr *attr, unsigned int valid)
103 struct inode *inode = vvp_object_inode(obj);
106 inode->i_uid = make_kuid(&init_user_ns, attr->cat_uid);
108 inode->i_gid = make_kgid(&init_user_ns, attr->cat_gid);
109 if (valid & CAT_ATIME)
110 inode_set_atime(inode, attr->cat_atime, 0);
111 if (valid & CAT_MTIME)
112 inode_set_mtime(inode, attr->cat_mtime, 0);
113 if (valid & CAT_CTIME)
114 inode_set_ctime(inode, attr->cat_ctime, 0);
115 if (valid & CAT_PROJID)
116 ll_i2info(inode)->lli_projid = attr->cat_projid;
120 static void vvp_dirty_for_sync(const struct lu_env *env, struct cl_object *obj)
122 struct inode *inode = vvp_object_inode(obj);
124 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
127 static int vvp_conf_set(const struct lu_env *env, struct cl_object *obj,
128 const struct cl_object_conf *conf)
130 struct ll_inode_info *lli = ll_i2info(conf->coc_inode);
132 if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
133 CDEBUG(D_VFSTRACE, DFID ": losing layout lock\n",
134 PFID(&lli->lli_fid));
136 ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
138 /* Clean up page mmap for this inode.
139 * The reason for us to do this is that if the page has
140 * already been installed into memory space, the process
141 * can access it without interacting with lustre, so this
142 * page may be stale due to layout change, and the process
143 * will never be notified.
144 * This operation is expensive but mmap processes have to pay
145 * a price themselves.
147 unmap_mapping_range(conf->coc_inode->i_mapping,
148 0, OBD_OBJECT_EOF, 0);
149 pcc_layout_invalidate(conf->coc_inode);
154 static int vvp_prune(const struct lu_env *env, struct cl_object *obj)
156 struct inode *inode = vvp_object_inode(obj);
160 rc = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, CL_FSYNC_LOCAL, 1);
162 CDEBUG(D_VFSTRACE, DFID ": writeback failed: %d\n",
163 PFID(lu_object_fid(&obj->co_lu)), rc);
167 if (ll_get_inode_lock_owner(inode) != current)
168 /* ask LOV get inode lock then lo_type_guard */
171 LASSERTF(inode_is_locked(inode), DFID ":inode %p lli_flags %#lx\n",
172 PFID(lu_object_fid(&obj->co_lu)), inode,
173 ll_i2info(inode)->lli_flags);
175 ll_truncate_inode_pages_final(inode);
176 mapping_clear_exiting(inode->i_mapping);
181 static int vvp_object_glimpse(const struct lu_env *env,
182 const struct cl_object *obj, struct ost_lvb *lvb)
184 struct inode *inode = vvp_object_inode(obj);
187 lvb->lvb_mtime = inode_get_mtime_sec(inode);
188 lvb->lvb_atime = inode_get_atime_sec(inode);
189 lvb->lvb_ctime = inode_get_ctime_sec(inode);
192 * LU-417: Add dirty pages block count lest i_blocks reports 0, some
193 * "cp" or "tar" on remote node may think it's a completely sparse file
196 if (lvb->lvb_size > 0 && lvb->lvb_blocks == 0)
197 lvb->lvb_blocks = dirty_cnt(inode);
202 static void vvp_req_attr_set(const struct lu_env *env, struct cl_object *obj,
203 struct cl_req_attr *attr)
207 struct ll_inode_info *lli;
208 u64 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLUID | OBD_MD_FLGID |
212 inode = vvp_object_inode(obj);
213 lli = ll_i2info(inode);
215 if (attr->cra_type == CRT_WRITE) {
216 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
217 obdo_set_o_projid(oa, ll_i2info(inode)->lli_projid);
218 } else if (attr->cra_type == CRT_READ) {
219 valid_flags |= OBD_MD_FLATIME;
221 obdo_from_inode(oa, inode, valid_flags & attr->cra_flags);
222 obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
223 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_INVALID_PFID))
226 attr->cra_uid = lli->lli_uid;
227 attr->cra_gid = lli->lli_gid;
229 memcpy(attr->cra_jobid, &lli->lli_jobid, sizeof(attr->cra_jobid));
232 static int vvp_inode_ops(const struct lu_env *env, struct cl_object *obj,
233 enum coo_inode_opc opc, void *data)
235 struct inode *inode = vvp_object_inode(obj);
236 struct ll_inode_info *lli = ll_i2info(inode);
241 case COIO_INODE_LOCK:
242 if (ll_get_inode_lock_owner(inode) != current)
243 ll_inode_lock(inode);
247 case COIO_INODE_UNLOCK:
248 if (ll_get_inode_lock_owner(inode) == current)
249 ll_inode_unlock(inode);
254 if (lli->lli_size_lock_owner != current)
255 ll_inode_size_lock(inode);
259 case COIO_SIZE_UNLOCK:
260 if (lli->lli_size_lock_owner == current)
261 ll_inode_size_unlock(inode);
273 static const struct cl_object_operations vvp_ops = {
274 .coo_page_init = vvp_page_init,
275 .coo_io_init = vvp_io_init,
276 .coo_attr_get = vvp_attr_get,
277 .coo_attr_update = vvp_attr_update,
278 .coo_dirty_for_sync = vvp_dirty_for_sync,
279 .coo_conf_set = vvp_conf_set,
280 .coo_prune = vvp_prune,
281 .coo_glimpse = vvp_object_glimpse,
282 .coo_req_attr_set = vvp_req_attr_set,
283 .coo_inode_ops = vvp_inode_ops,
286 static int vvp_object_init0(const struct lu_env *env,
287 struct vvp_object *vob,
288 const struct cl_object_conf *conf)
290 vob->vob_inode = conf->coc_inode;
291 cl_object_page_init(&vob->vob_cl, sizeof(struct cl_page_slice));
295 static int vvp_object_init(const struct lu_env *env, struct lu_object *obj,
296 const struct lu_object_conf *conf)
298 struct vvp_device *dev = lu2vvp_dev(obj->lo_dev);
299 struct vvp_object *vob = lu2vvp(obj);
300 struct lu_object *below;
301 struct lu_device *under;
304 under = &dev->vdv_next->cd_lu_dev;
305 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header, under);
307 const struct cl_object_conf *cconf;
309 cconf = lu2cl_conf(conf);
310 lu_object_add(obj, below);
311 result = vvp_object_init0(env, vob, cconf);
318 static void vvp_object_free_rcu(struct rcu_head *head)
320 struct vvp_object *vob = container_of(head, struct vvp_object,
321 vob_header.coh_lu.loh_rcu);
323 kmem_cache_free(vvp_object_kmem, vob);
326 static void vvp_object_free(const struct lu_env *env, struct lu_object *obj)
328 struct vvp_object *vob = lu2vvp(obj);
331 lu_object_header_fini(obj->lo_header);
332 OBD_FREE_PRE(vob, sizeof(*vob), "slab-freed");
333 call_rcu(&vob->vob_header.coh_lu.loh_rcu, vvp_object_free_rcu);
336 static const struct lu_object_operations vvp_lu_obj_ops = {
337 .loo_object_init = vvp_object_init,
338 .loo_object_free = vvp_object_free,
339 .loo_object_print = vvp_object_print,
342 struct vvp_object *cl_inode2vvp(struct inode *inode)
344 struct ll_inode_info *lli = ll_i2info(inode);
345 struct cl_object *obj = lli->lli_clob;
346 struct lu_object *lu;
348 LASSERT(obj != NULL);
349 lu = lu_object_locate(obj->co_lu.lo_header, &vvp_device_type);
355 struct lu_object *vvp_object_alloc(const struct lu_env *env,
356 const struct lu_object_header *unused,
357 struct lu_device *dev)
359 struct vvp_object *vob;
360 struct lu_object *obj;
362 OBD_SLAB_ALLOC_PTR_GFP(vob, vvp_object_kmem, GFP_NOFS);
364 struct cl_object_header *hdr;
366 obj = &vob->vob_cl.co_lu;
367 hdr = &vob->vob_header;
368 cl_object_header_init(hdr);
369 hdr->coh_page_bufsize = round_up(sizeof(struct cl_page), 8);
371 lu_object_init(obj, &hdr->coh_lu, dev);
372 lu_object_add_top(&hdr->coh_lu, obj);
374 vob->vob_cl.co_ops = &vvp_ops;
375 obj->lo_ops = &vvp_lu_obj_ops;