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