Whamcloud - gitweb
LU-8726 osd-ldiskfs: bypass read for benchmarking
[fs/lustre-release.git] / lustre / llite / lcommon_cl.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) 2011, 2016, 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  *   Author: Nikita Danilov <nikita.danilov@sun.com>
33  */
34
35 #define DEBUG_SUBSYSTEM S_LLITE
36
37 #include <libcfs/libcfs.h>
38 #include <linux/fs.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/quotaops.h>
42 #include <linux/highmem.h>
43 #include <linux/pagemap.h>
44 #include <linux/rbtree.h>
45
46 #include <obd.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_dlm.h>
50 #include <lustre_ver.h>
51 #include <lustre_mdc.h>
52 #include <cl_object.h>
53
54 #include "llite_internal.h"
55 #include "vvp_internal.h"
56
57 /**
58  * An `emergency' environment used by cl_inode_fini() when cl_env_get()
59  * fails. Access to this environment is serialized by cl_inode_fini_guard
60  * mutex.
61  */
62 struct lu_env *cl_inode_fini_env;
63 __u16 cl_inode_fini_refcheck;
64
65 /**
66  * A mutex serializing calls to slp_inode_fini() under extreme memory
67  * pressure, when environments cannot be allocated.
68  */
69 static DEFINE_MUTEX(cl_inode_fini_guard);
70
71 int cl_setattr_ost(struct cl_object *obj, const struct iattr *attr,
72                    unsigned int attr_flags)
73 {
74         struct lu_env *env;
75         struct cl_io  *io;
76         int            result;
77         __u16          refcheck;
78
79         ENTRY;
80
81         env = cl_env_get(&refcheck);
82         if (IS_ERR(env))
83                 RETURN(PTR_ERR(env));
84
85         io = vvp_env_thread_io(env);
86         io->ci_obj = obj;
87         io->ci_verify_layout = 1;
88
89         io->u.ci_setattr.sa_attr.lvb_atime = LTIME_S(attr->ia_atime);
90         io->u.ci_setattr.sa_attr.lvb_mtime = LTIME_S(attr->ia_mtime);
91         io->u.ci_setattr.sa_attr.lvb_ctime = LTIME_S(attr->ia_ctime);
92         io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
93         io->u.ci_setattr.sa_attr_flags = attr_flags;
94         io->u.ci_setattr.sa_valid = attr->ia_valid;
95         io->u.ci_setattr.sa_parent_fid = lu_object_fid(&obj->co_lu);
96
97 again:
98         if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0) {
99                 struct vvp_io *vio = vvp_env_io(env);
100
101                 if (attr->ia_valid & ATTR_FILE)
102                         /* populate the file descriptor for ftruncate to honor
103                          * group lock - see LU-787 */
104                         vio->vui_fd = LUSTRE_FPRIVATE(attr->ia_file);
105
106                 result = cl_io_loop(env, io);
107         } else {
108                 result = io->ci_result;
109         }
110         cl_io_fini(env, io);
111         if (unlikely(io->ci_need_restart))
112                 goto again;
113
114         cl_env_put(env, &refcheck);
115         RETURN(result);
116 }
117
118 /**
119  * Initialize or update CLIO structures for regular files when new
120  * meta-data arrives from the server.
121  *
122  * \param inode regular file inode
123  * \param md    new file metadata from MDS
124  * - allocates cl_object if necessary,
125  * - updated layout, if object was already here.
126  */
127 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
128 {
129         struct lu_env        *env;
130         struct ll_inode_info *lli;
131         struct cl_object     *clob;
132         struct lu_site       *site;
133         struct lu_fid        *fid;
134         struct cl_object_conf conf = {
135                 .coc_inode = inode,
136                 .u = {
137                         .coc_layout = md->layout,
138                 }
139         };
140         int result = 0;
141         __u16 refcheck;
142
143         LASSERT(md->body->mbo_valid & OBD_MD_FLID);
144         LASSERT(S_ISREG(inode->i_mode));
145
146         env = cl_env_get(&refcheck);
147         if (IS_ERR(env))
148                 return PTR_ERR(env);
149
150         site = ll_i2sbi(inode)->ll_site;
151         lli  = ll_i2info(inode);
152         fid  = &lli->lli_fid;
153         LASSERT(fid_is_sane(fid));
154
155         if (lli->lli_clob == NULL) {
156                 /* clob is slave of inode, empty lli_clob means for new inode,
157                  * there is no clob in cache with the given fid, so it is
158                  * unnecessary to perform lookup-alloc-lookup-insert, just
159                  * alloc and insert directly. */
160                 LASSERT(inode->i_state & I_NEW);
161                 conf.coc_lu.loc_flags = LOC_F_NEW;
162                 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
163                                       fid, &conf);
164                 if (!IS_ERR(clob)) {
165                         /*
166                          * No locking is necessary, as new inode is
167                          * locked by I_NEW bit.
168                          */
169                         lli->lli_clob = clob;
170                         lu_object_ref_add(&clob->co_lu, "inode", inode);
171                 } else
172                         result = PTR_ERR(clob);
173         } else {
174                 result = cl_conf_set(env, lli->lli_clob, &conf);
175         }
176
177         cl_env_put(env, &refcheck);
178
179         if (result != 0)
180                 CERROR("Failure to initialize cl object "DFID": %d\n",
181                        PFID(fid), result);
182         return result;
183 }
184
185 /**
186  * Wait for others drop their references of the object at first, then we drop
187  * the last one, which will lead to the object be destroyed immediately.
188  * Must be called after cl_object_kill() against this object.
189  *
190  * The reason we want to do this is: destroying top object will wait for sub
191  * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
192  * to initiate top object destroying which may deadlock. See bz22520.
193  */
194 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
195 {
196         struct lu_object_header *header = obj->co_lu.lo_header;
197         wait_queue_t           waiter;
198
199         if (unlikely(atomic_read(&header->loh_ref) != 1)) {
200                 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
201                 struct lu_site_bkt_data *bkt;
202
203                 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
204
205                 init_waitqueue_entry(&waiter, current);
206                 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
207
208                 while (1) {
209                         set_current_state(TASK_UNINTERRUPTIBLE);
210                         if (atomic_read(&header->loh_ref) == 1)
211                                 break;
212                         schedule();
213                 }
214
215                 set_current_state(TASK_RUNNING);
216                 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
217         }
218
219         cl_object_put(env, obj);
220 }
221
222 void cl_inode_fini(struct inode *inode)
223 {
224         struct lu_env           *env;
225         struct ll_inode_info    *lli  = ll_i2info(inode);
226         struct cl_object        *clob = lli->lli_clob;
227         __u16  refcheck;
228         int emergency;
229
230         if (clob != NULL) {
231                 env = cl_env_get(&refcheck);
232                 emergency = IS_ERR(env);
233                 if (emergency) {
234                         mutex_lock(&cl_inode_fini_guard);
235                         LASSERT(cl_inode_fini_env != NULL);
236                         env = cl_inode_fini_env;
237                 }
238
239                 /*
240                  * cl_object cache is a slave to inode cache (which, in turn
241                  * is a slave to dentry cache), don't keep cl_object in memory
242                  * when its master is evicted.
243                  */
244                 cl_object_kill(env, clob);
245                 lu_object_ref_del(&clob->co_lu, "inode", inode);
246                 cl_object_put_last(env, clob);
247                 lli->lli_clob = NULL;
248                 if (emergency)
249                         mutex_unlock(&cl_inode_fini_guard);
250                 else
251                         cl_env_put(env, &refcheck);
252         }
253 }
254
255 /**
256  * build inode number from passed @fid */
257 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
258 {
259         if (BITS_PER_LONG == 32 || api32)
260                 RETURN(fid_flatten32(fid));
261         else
262                 RETURN(fid_flatten(fid));
263 }
264
265 /**
266  * build inode generation from passed @fid.  If our FID overflows the 32-bit
267  * inode number then return a non-zero generation to distinguish them. */
268 __u32 cl_fid_build_gen(const struct lu_fid *fid)
269 {
270         __u32 gen;
271         ENTRY;
272
273         if (fid_is_igif(fid)) {
274                 gen = lu_igif_gen(fid);
275                 RETURN(gen);
276         }
277
278         gen = (fid_flatten(fid) >> 32);
279         RETURN(gen);
280 }