4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 #define DEBUG_SUBSYSTEM S_LLITE
36 #include <libcfs/libcfs.h>
38 #include <linux/sched.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/rbtree.h>
46 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <lustre_dlm.h>
49 #include <lustre_mdc.h>
50 #include <cl_object.h>
52 #include "llite_internal.h"
53 #include "vvp_internal.h"
56 * An `emergency' environment used by cl_inode_fini() when cl_env_get()
57 * fails. Access to this environment is serialized by cl_inode_fini_guard
60 struct lu_env *cl_inode_fini_env;
61 __u16 cl_inode_fini_refcheck;
64 * A mutex serializing calls to slp_inode_fini() under extreme memory
65 * pressure, when environments cannot be allocated.
67 static DEFINE_MUTEX(cl_inode_fini_guard);
69 int cl_setattr_ost(struct cl_object *obj, const struct iattr *attr,
70 enum op_xvalid xvalid, unsigned int attr_flags)
79 env = cl_env_get(&refcheck);
83 io = vvp_env_thread_io(env);
85 io->ci_verify_layout = 1;
87 io->u.ci_setattr.sa_attr.lvb_atime = attr->ia_atime.tv_sec;
88 io->u.ci_setattr.sa_attr.lvb_mtime = attr->ia_mtime.tv_sec;
89 io->u.ci_setattr.sa_attr.lvb_ctime = attr->ia_ctime.tv_sec;
90 io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
91 io->u.ci_setattr.sa_attr_flags = attr_flags;
92 io->u.ci_setattr.sa_avalid = attr->ia_valid;
93 io->u.ci_setattr.sa_xvalid = xvalid;
94 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&obj->co_lu);
95 if (attr->ia_valid & ATTR_SIZE)
96 io->u.ci_setattr.sa_subtype = CL_SETATTR_TRUNC;
98 if (attr->ia_valid & ATTR_FILE)
99 ll_io_set_mirror(io, attr->ia_file);
101 if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0) {
102 struct vvp_io *vio = vvp_env_io(env);
104 if (attr->ia_valid & ATTR_FILE)
106 * populate the file descriptor for ftruncate to honor
107 * group lock - see LU-787
109 vio->vui_fd = attr->ia_file->private_data;
111 result = cl_io_loop(env, io);
113 result = io->ci_result;
116 if (unlikely(io->ci_need_restart))
119 cl_env_put(env, &refcheck);
124 * Initialize or update CLIO structures for regular files when new
125 * meta-data arrives from the server.
127 * \param inode regular file inode
128 * \param md new file metadata from MDS
129 * - allocates cl_object if necessary,
130 * - updated layout, if object was already here.
132 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
135 struct ll_inode_info *lli;
136 struct cl_object *clob;
137 struct lu_site *site;
139 struct cl_object_conf conf = {
142 .coc_layout = md->layout,
148 LASSERT(md->body->mbo_valid & OBD_MD_FLID);
149 LASSERT(S_ISREG(inode->i_mode));
151 env = cl_env_get(&refcheck);
155 site = ll_i2sbi(inode)->ll_site;
156 lli = ll_i2info(inode);
158 LASSERT(fid_is_sane(fid));
160 if (lli->lli_clob == NULL) {
161 /* clob is slave of inode, empty lli_clob means for new inode,
162 * there is no clob in cache with the given fid, so it is
163 * unnecessary to perform lookup-alloc-lookup-insert, just
164 * alloc and insert directly.
166 if (!(inode->i_state & I_NEW)) {
168 CERROR("%s: unexpected not-NEW inode "DFID": rc = %d\n",
169 ll_i2sbi(inode)->ll_fsname, PFID(fid), result);
173 conf.coc_lu.loc_flags = LOC_F_NEW;
174 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
178 * No locking is necessary, as new inode is
179 * locked by I_NEW bit.
181 lli->lli_clob = clob;
183 result = PTR_ERR(clob);
186 result = cl_conf_set(env, lli->lli_clob, &conf);
187 if (result == -EBUSY) {
188 /* ignore the error since I/O will handle it later */
194 CERROR("%s: failed to initialize cl_object "DFID": rc = %d\n",
195 ll_i2sbi(inode)->ll_fsname, PFID(fid), result);
198 cl_env_put(env, &refcheck);
204 * Wait for others drop their references of the object at first, then we drop
205 * the last one, which will lead to the object be destroyed immediately.
206 * Must be called after cl_object_kill() against this object.
208 * The reason we want to do this is: destroying top object will wait for sub
209 * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
210 * to initiate top object destroying which may deadlock. See bz22520.
212 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
214 struct lu_object_header *header = obj->co_lu.lo_header;
216 if (unlikely(atomic_read(&header->loh_ref) != 1)) {
217 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
218 wait_queue_head_t *wq;
220 wq = lu_site_wq_from_fid(site, &header->loh_fid);
222 /* LU_OBJECT_HEARD_BANSHEE is set in cl_object_kill(), in case
223 * someone is waiting on this, wake up and then wait for object
224 * refcount becomes one.
227 wait_event(*wq, atomic_read(&header->loh_ref) == 1);
230 cl_object_put(env, obj);
233 void cl_inode_fini(struct inode *inode)
236 struct ll_inode_info *lli = ll_i2info(inode);
237 struct cl_object *clob = lli->lli_clob;
242 env = cl_env_get(&refcheck);
243 emergency = IS_ERR(env);
245 mutex_lock(&cl_inode_fini_guard);
246 LASSERT(cl_inode_fini_env != NULL);
247 env = cl_inode_fini_env;
251 * cl_object cache is a slave to inode cache (which, in turn
252 * is a slave to dentry cache), don't keep cl_object in memory
253 * when its master is evicted.
255 cl_object_kill(env, clob);
256 cl_object_put_last(env, clob);
257 lli->lli_clob = NULL;
259 mutex_unlock(&cl_inode_fini_guard);
261 cl_env_put(env, &refcheck);
266 * build inode number from passed @fid.
268 * For 32-bit systems or syscalls limit the inode number to a 32-bit value
269 * to avoid EOVERFLOW errors. This will inevitably result in inode number
270 * collisions, but fid_flatten32() tries hard to avoid this if possible.
272 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
274 if (BITS_PER_LONG == 32 || api32)
275 RETURN(fid_flatten32(fid));
277 RETURN(fid_flatten64(fid));
281 * build inode generation from passed @fid. If our FID overflows the 32-bit
282 * inode number then return a non-zero generation to distinguish them.
284 __u32 cl_fid_build_gen(const struct lu_fid *fid)
286 if (fid_is_igif(fid))
287 RETURN(lu_igif_gen(fid));
289 RETURN(fid_flatten64(fid) >> 32);