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