Whamcloud - gitweb
cd2a05be452866ed54bc344e707a5facb7ae2ffc
[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
44 #include <libcfs/libcfs.h>
45
46 #include <obd.h>
47 #include "llite_internal.h"
48 #include "vvp_internal.h"
49
50 /*****************************************************************************
51  *
52  * Object operations.
53  *
54  */
55
56 static int vvp_object_print(const struct lu_env *env, void *cookie,
57                             lu_printer_t p, const struct lu_object *o)
58 {
59         struct ccc_object    *obj   = lu2ccc(o);
60         struct inode         *inode = obj->cob_inode;
61         struct ll_inode_info *lli;
62
63         (*p)(env, cookie, "(%s %d %d) inode: %p ",
64              list_empty(&obj->cob_pending_list) ? "-" : "+",
65              atomic_read(&obj->cob_transient_pages),
66              atomic_read(&obj->cob_mmap_cnt),
67              inode);
68         if (inode) {
69                 lli = ll_i2info(inode);
70                 (*p)(env, cookie, "%lu/%u %o %u %d %p "DFID,
71                      inode->i_ino, inode->i_generation, inode->i_mode,
72                      inode->i_nlink, atomic_read(&inode->i_count),
73                      lli->lli_clob, PFID(&lli->lli_fid));
74         }
75         return 0;
76 }
77
78 static int vvp_attr_get(const struct lu_env *env, struct cl_object *obj,
79                         struct cl_attr *attr)
80 {
81         struct inode *inode = ccc_object_inode(obj);
82
83         /*
84          * lov overwrites most of these fields in
85          * lov_attr_get()->...lov_merge_lvb_kms(), except when inode
86          * attributes are newer.
87          */
88
89         attr->cat_size = i_size_read(inode);
90         attr->cat_mtime = LTIME_S(inode->i_mtime);
91         attr->cat_atime = LTIME_S(inode->i_atime);
92         attr->cat_ctime = LTIME_S(inode->i_ctime);
93         attr->cat_blocks = inode->i_blocks;
94         attr->cat_uid = from_kuid(&init_user_ns, inode->i_uid);
95         attr->cat_gid = from_kgid(&init_user_ns, inode->i_gid);
96         /* KMS is not known by this layer */
97         return 0; /* layers below have to fill in the rest */
98 }
99
100 static int vvp_attr_set(const struct lu_env *env, struct cl_object *obj,
101                         const struct cl_attr *attr, unsigned valid)
102 {
103         struct inode *inode = ccc_object_inode(obj);
104
105         if (valid & CAT_UID)
106                 inode->i_uid = make_kuid(&init_user_ns, attr->cat_uid);
107         if (valid & CAT_GID)
108                 inode->i_gid = make_kgid(&init_user_ns, attr->cat_gid);
109         if (valid & CAT_ATIME)
110                 LTIME_S(inode->i_atime) = attr->cat_atime;
111         if (valid & CAT_MTIME)
112                 LTIME_S(inode->i_mtime) = attr->cat_mtime;
113         if (valid & CAT_CTIME)
114                 LTIME_S(inode->i_ctime) = attr->cat_ctime;
115         if (0 && valid & CAT_SIZE)
116                 i_size_write(inode, attr->cat_size);
117         /* not currently necessary */
118         if (0 && valid & (CAT_UID|CAT_GID|CAT_SIZE))
119                 mark_inode_dirty(inode);
120         return 0;
121 }
122
123 static int vvp_conf_set(const struct lu_env *env, struct cl_object *obj,
124                         const struct cl_object_conf *conf)
125 {
126         struct ll_inode_info *lli = ll_i2info(conf->coc_inode);
127
128         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
129                 CDEBUG(D_VFSTRACE, DFID ": losing layout lock\n",
130                        PFID(&lli->lli_fid));
131
132                 ll_layout_version_set(lli, LL_LAYOUT_GEN_NONE);
133
134                 /* Clean up page mmap for this inode.
135                  * The reason for us to do this is that if the page has
136                  * already been installed into memory space, the process
137                  * can access it without interacting with lustre, so this
138                  * page may be stale due to layout change, and the process
139                  * will never be notified.
140                  * This operation is expensive but mmap processes have to pay
141                  * a price themselves. */
142                 unmap_mapping_range(conf->coc_inode->i_mapping,
143                                     0, OBD_OBJECT_EOF, 0);
144         }
145         return 0;
146 }
147
148 static int vvp_prune(const struct lu_env *env, struct cl_object *obj)
149 {
150         struct inode *inode = ccc_object_inode(obj);
151         int rc;
152         ENTRY;
153
154         rc = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, CL_FSYNC_LOCAL, 1);
155         if (rc < 0) {
156                 CDEBUG(D_VFSTRACE, DFID ": writeback failed: %d\n",
157                        PFID(lu_object_fid(&obj->co_lu)), rc);
158                 RETURN(rc);
159         }
160
161         truncate_inode_pages(inode->i_mapping, 0);
162         RETURN(0);
163 }
164
165 static const struct cl_object_operations vvp_ops = {
166         .coo_page_init = vvp_page_init,
167         .coo_lock_init = vvp_lock_init,
168         .coo_io_init   = vvp_io_init,
169         .coo_attr_get  = vvp_attr_get,
170         .coo_attr_set  = vvp_attr_set,
171         .coo_conf_set  = vvp_conf_set,
172         .coo_prune     = vvp_prune,
173         .coo_glimpse   = ccc_object_glimpse
174 };
175
176 static const struct lu_object_operations vvp_lu_obj_ops = {
177         .loo_object_init  = ccc_object_init,
178         .loo_object_free  = ccc_object_free,
179         .loo_object_print = vvp_object_print
180 };
181
182 struct ccc_object *cl_inode2ccc(struct inode *inode)
183 {
184         struct ll_inode_info *lli = ll_i2info(inode);
185         struct cl_object     *obj = lli->lli_clob;
186         struct lu_object     *lu;
187
188         LASSERT(obj != NULL);
189         lu = lu_object_locate(obj->co_lu.lo_header, &vvp_device_type);
190         LASSERT(lu != NULL);
191         return lu2ccc(lu);
192 }
193
194 struct lu_object *vvp_object_alloc(const struct lu_env *env,
195                                    const struct lu_object_header *hdr,
196                                    struct lu_device *dev)
197 {
198         return ccc_object_alloc(env, hdr, dev, &vvp_ops, &vvp_lu_obj_ops);
199 }
200