Whamcloud - gitweb
6df7eb1807d042c6503f516939d85d87bf680d74
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * cl_object implementation for VVP layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #ifndef __KERNEL__
44 # error This file is kernel only.
45 #endif
46
47 #include <libcfs/libcfs.h>
48
49 #include <obd.h>
50 #include <lustre_lite.h>
51
52 #include "vvp_internal.h"
53
54 /*****************************************************************************
55  *
56  * Object operations.
57  *
58  */
59
60 static int vvp_object_print(const struct lu_env *env, void *cookie,
61                             lu_printer_t p, const struct lu_object *o)
62 {
63         struct ccc_object    *obj   = lu2ccc(o);
64         struct inode         *inode = obj->cob_inode;
65         struct ll_inode_info *lli;
66
67         (*p)(env, cookie, "(%s %d %d) inode: %p ",
68              cfs_list_empty(&obj->cob_pending_list) ? "-" : "+",
69              obj->cob_transient_pages, cfs_atomic_read(&obj->cob_mmap_cnt),
70              inode);
71         if (inode) {
72                 lli = ll_i2info(inode);
73                 (*p)(env, cookie, "%lu/%u %o %u %d %p "DFID,
74                      inode->i_ino, inode->i_generation, inode->i_mode,
75                      inode->i_nlink, atomic_read(&inode->i_count),
76                      lli->lli_clob, PFID(&lli->lli_fid));
77         }
78         return 0;
79 }
80
81 static int vvp_attr_get(const struct lu_env *env, struct cl_object *obj,
82                         struct cl_attr *attr)
83 {
84         struct inode *inode = ccc_object_inode(obj);
85
86         /*
87          * lov overwrites most of these fields in
88          * lov_attr_get()->...lov_merge_lvb_kms(), except when inode
89          * attributes are newer.
90          */
91
92         attr->cat_size = i_size_read(inode);
93         attr->cat_mtime = LTIME_S(inode->i_mtime);
94         attr->cat_atime = LTIME_S(inode->i_atime);
95         attr->cat_ctime = LTIME_S(inode->i_ctime);
96         attr->cat_blocks = inode->i_blocks;
97         attr->cat_uid = inode->i_uid;
98         attr->cat_gid = inode->i_gid;
99         /* KMS is not known by this layer */
100         return 0; /* layers below have to fill in the rest */
101 }
102
103 static int vvp_attr_set(const struct lu_env *env, struct cl_object *obj,
104                         const struct cl_attr *attr, unsigned valid)
105 {
106         struct inode *inode = ccc_object_inode(obj);
107
108         if (valid & CAT_UID)
109                 inode->i_uid = attr->cat_uid;
110         if (valid & CAT_GID)
111                 inode->i_gid = attr->cat_gid;
112         if (valid & CAT_ATIME)
113                 LTIME_S(inode->i_atime) = attr->cat_atime;
114         if (valid & CAT_MTIME)
115                 LTIME_S(inode->i_mtime) = attr->cat_mtime;
116         if (valid & CAT_CTIME)
117                 LTIME_S(inode->i_ctime) = attr->cat_ctime;
118         if (0 && valid & CAT_SIZE)
119                 cl_isize_write_nolock(inode, attr->cat_size);
120         /* not currently necessary */
121         if (0 && valid & (CAT_UID|CAT_GID|CAT_SIZE))
122                 mark_inode_dirty(inode);
123         return 0;
124 }
125
126 int vvp_conf_set(const struct lu_env *env, struct cl_object *obj,
127                 const struct cl_object_conf *conf)
128 {
129         struct ll_inode_info *lli = ll_i2info(conf->coc_inode);
130
131         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
132                 CDEBUG(D_VFSTRACE, DFID ": losing layout lock\n",
133                        PFID(&lli->lli_fid));
134
135                 ll_layout_version_set(lli, LL_LAYOUT_GEN_NONE);
136
137                 /* Clean up page mmap for this inode.
138                  * The reason for us to do this is that if the page has
139                  * already been installed into memory space, the process
140                  * can access it without interacting with lustre, so this
141                  * page may be stale due to layout change, and the process
142                  * will never be notified.
143                  * This operation is expensive but mmap processes have to pay
144                  * a price themselves. */
145                 unmap_mapping_range(conf->coc_inode->i_mapping,
146                                     0, OBD_OBJECT_EOF, 0);
147
148                 return 0;
149         }
150
151         if (conf->coc_opc != OBJECT_CONF_SET)
152                 return 0;
153
154         if (conf->u.coc_md != NULL && conf->u.coc_md->lsm != NULL) {
155                 CDEBUG(D_VFSTRACE, DFID ": layout version change: %u -> %u\n",
156                        PFID(&lli->lli_fid), lli->lli_layout_gen,
157                        conf->u.coc_md->lsm->lsm_layout_gen);
158
159                 lli->lli_has_smd = lsm_has_objects(conf->u.coc_md->lsm);
160                 ll_layout_version_set(lli, conf->u.coc_md->lsm->lsm_layout_gen);
161         } else {
162                 CDEBUG(D_VFSTRACE, DFID ": layout nuked: %u.\n",
163                        PFID(&lli->lli_fid), lli->lli_layout_gen);
164
165                 lli->lli_has_smd = false;
166                 ll_layout_version_set(lli, LL_LAYOUT_GEN_EMPTY);
167         }
168         return 0;
169 }
170
171 static int vvp_prune(const struct lu_env *env, struct cl_object *obj)
172 {
173         struct inode *inode = ccc_object_inode(obj);
174         int rc;
175         ENTRY;
176
177         rc = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, CL_FSYNC_ALL, 1);
178         if (rc == 0)
179                 truncate_inode_pages(inode->i_mapping, 0);
180
181         RETURN(rc);
182 }
183
184 static const struct cl_object_operations vvp_ops = {
185         .coo_page_init = vvp_page_init,
186         .coo_lock_init = vvp_lock_init,
187         .coo_io_init   = vvp_io_init,
188         .coo_attr_get  = vvp_attr_get,
189         .coo_attr_set  = vvp_attr_set,
190         .coo_conf_set  = vvp_conf_set,
191         .coo_prune     = vvp_prune,
192         .coo_glimpse   = ccc_object_glimpse
193 };
194
195 static const struct lu_object_operations vvp_lu_obj_ops = {
196         .loo_object_init  = ccc_object_init,
197         .loo_object_free  = ccc_object_free,
198         .loo_object_print = vvp_object_print
199 };
200
201 struct ccc_object *cl_inode2ccc(struct inode *inode)
202 {
203         struct cl_inode_info *lli = cl_i2info(inode);
204         struct cl_object     *obj = lli->lli_clob;
205         struct lu_object     *lu;
206
207         LASSERT(obj != NULL);
208         lu = lu_object_locate(obj->co_lu.lo_header, &vvp_device_type);
209         LASSERT(lu != NULL);
210         return lu2ccc(lu);
211 }
212
213 struct lu_object *vvp_object_alloc(const struct lu_env *env,
214                                    const struct lu_object_header *hdr,
215                                    struct lu_device *dev)
216 {
217         return ccc_object_alloc(env, hdr, dev, &vvp_ops, &vvp_lu_obj_ops);
218 }
219