Whamcloud - gitweb
e693fa02ec923893209652b8da07077a85566d7c
[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, 2014, 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, struct obd_capa *capa)
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
92         io->u.ci_setattr.sa_attr.lvb_atime = LTIME_S(attr->ia_atime);
93         io->u.ci_setattr.sa_attr.lvb_mtime = LTIME_S(attr->ia_mtime);
94         io->u.ci_setattr.sa_attr.lvb_ctime = LTIME_S(attr->ia_ctime);
95         io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
96         io->u.ci_setattr.sa_attr_flags = attr_flags;
97         io->u.ci_setattr.sa_valid = attr->ia_valid;
98         io->u.ci_setattr.sa_parent_fid = lu_object_fid(&obj->co_lu);
99         io->u.ci_setattr.sa_capa = capa;
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         /* HSM import case: file is released, cannot be restored
118          * no need to fail except if restore registration failed
119          * with -ENODATA */
120         if (result == -ENODATA && io->ci_restore_needed &&
121             io->ci_result != -ENODATA)
122                 result = 0;
123         cl_env_put(env, &refcheck);
124         RETURN(result);
125 }
126
127 /**
128  * Initialize or update CLIO structures for regular files when new
129  * meta-data arrives from the server.
130  *
131  * \param inode regular file inode
132  * \param md    new file metadata from MDS
133  * - allocates cl_object if necessary,
134  * - updated layout, if object was already here.
135  */
136 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
137 {
138         struct lu_env        *env;
139         struct ll_inode_info *lli;
140         struct cl_object     *clob;
141         struct lu_site       *site;
142         struct lu_fid        *fid;
143         struct cl_object_conf conf = {
144                 .coc_inode = inode,
145                 .u = {
146                         .coc_md    = md
147                 }
148         };
149         int result = 0;
150         int refcheck;
151
152         LASSERT(md->body->mbo_valid & OBD_MD_FLID);
153         LASSERT(S_ISREG(inode->i_mode));
154
155         env = cl_env_get(&refcheck);
156         if (IS_ERR(env))
157                 return PTR_ERR(env);
158
159         site = ll_i2sbi(inode)->ll_site;
160         lli  = ll_i2info(inode);
161         fid  = &lli->lli_fid;
162         LASSERT(fid_is_sane(fid));
163
164         if (lli->lli_clob == NULL) {
165                 /* clob is slave of inode, empty lli_clob means for new inode,
166                  * there is no clob in cache with the given fid, so it is
167                  * unnecessary to perform lookup-alloc-lookup-insert, just
168                  * alloc and insert directly. */
169                 LASSERT(inode->i_state & I_NEW);
170                 conf.coc_lu.loc_flags = LOC_F_NEW;
171                 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
172                                       fid, &conf);
173                 if (!IS_ERR(clob)) {
174                         /*
175                          * No locking is necessary, as new inode is
176                          * locked by I_NEW bit.
177                          */
178                         lli->lli_clob = clob;
179                         lli->lli_has_smd = lsm_has_objects(md->lsm);
180                         lu_object_ref_add(&clob->co_lu, "inode", inode);
181                 } else
182                         result = PTR_ERR(clob);
183         } else {
184                 result = cl_conf_set(env, lli->lli_clob, &conf);
185         }
186
187         cl_env_put(env, &refcheck);
188
189         if (result != 0)
190                 CERROR("Failure to initialize cl object "DFID": %d\n",
191                        PFID(fid), result);
192         return result;
193 }
194
195 /**
196  * Wait for others drop their references of the object at first, then we drop
197  * the last one, which will lead to the object be destroyed immediately.
198  * Must be called after cl_object_kill() against this object.
199  *
200  * The reason we want to do this is: destroying top object will wait for sub
201  * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
202  * to initiate top object destroying which may deadlock. See bz22520.
203  */
204 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
205 {
206         struct lu_object_header *header = obj->co_lu.lo_header;
207         wait_queue_t           waiter;
208
209         if (unlikely(atomic_read(&header->loh_ref) != 1)) {
210                 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
211                 struct lu_site_bkt_data *bkt;
212
213                 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
214
215                 init_waitqueue_entry_current(&waiter);
216                 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
217
218                 while (1) {
219                         set_current_state(TASK_UNINTERRUPTIBLE);
220                         if (atomic_read(&header->loh_ref) == 1)
221                                 break;
222                         waitq_wait(&waiter, TASK_UNINTERRUPTIBLE);
223                 }
224
225                 set_current_state(TASK_RUNNING);
226                 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
227         }
228
229         cl_object_put(env, obj);
230 }
231
232 void cl_inode_fini(struct inode *inode)
233 {
234         struct lu_env           *env;
235         struct ll_inode_info    *lli  = ll_i2info(inode);
236         struct cl_object        *clob = lli->lli_clob;
237         int refcheck;
238         int emergency;
239
240         if (clob != NULL) {
241                 void                    *cookie;
242
243                 cookie = cl_env_reenter();
244                 env = cl_env_get(&refcheck);
245                 emergency = IS_ERR(env);
246                 if (emergency) {
247                         mutex_lock(&cl_inode_fini_guard);
248                         LASSERT(cl_inode_fini_env != NULL);
249                         cl_env_implant(cl_inode_fini_env, &refcheck);
250                         env = cl_inode_fini_env;
251                 }
252
253                 /*
254                  * cl_object cache is a slave to inode cache (which, in turn
255                  * is a slave to dentry cache), don't keep cl_object in memory
256                  * when its master is evicted.
257                  */
258                 cl_object_kill(env, clob);
259                 lu_object_ref_del(&clob->co_lu, "inode", inode);
260                 cl_object_put_last(env, clob);
261                 lli->lli_clob = NULL;
262                 if (emergency) {
263                         cl_env_unplant(cl_inode_fini_env, &refcheck);
264                         mutex_unlock(&cl_inode_fini_guard);
265                 } else {
266                         cl_env_put(env, &refcheck);
267                 }
268
269                 cl_env_reexit(cookie);
270         }
271 }
272
273 /**
274  * build inode number from passed @fid */
275 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
276 {
277         if (BITS_PER_LONG == 32 || api32)
278                 RETURN(fid_flatten32(fid));
279         else
280                 RETURN(fid_flatten(fid));
281 }
282
283 /**
284  * build inode generation from passed @fid.  If our FID overflows the 32-bit
285  * inode number then return a non-zero generation to distinguish them. */
286 __u32 cl_fid_build_gen(const struct lu_fid *fid)
287 {
288         __u32 gen;
289         ENTRY;
290
291         if (fid_is_igif(fid)) {
292                 gen = lu_igif_gen(fid);
293                 RETURN(gen);
294         }
295
296         gen = (fid_flatten(fid) >> 32);
297         RETURN(gen);
298 }
299
300 /* lsm is unreliable after hsm implementation as layout can be changed at
301  * any time. This is only to support old, non-clio-ized interfaces. It will
302  * cause deadlock if clio operations are called with this extra layout refcount
303  * because in case the layout changed during the IO, ll_layout_refresh() will
304  * have to wait for the refcount to become zero to destroy the older layout.
305  *
306  * Notice that the lsm returned by this function may not be valid unless called
307  * inside layout lock - MDS_INODELOCK_LAYOUT. */
308 struct lov_stripe_md *ccc_inode_lsm_get(struct inode *inode)
309 {
310         return lov_lsm_get(ll_i2info(inode)->lli_clob);
311 }
312
313 void inline ccc_inode_lsm_put(struct inode *inode, struct lov_stripe_md *lsm)
314 {
315         lov_lsm_put(ll_i2info(inode)->lli_clob, lsm);
316 }