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