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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
41 #include <libcfs/libcfs.h>
43 #include <linux/sched.h>
45 #include <linux/quotaops.h>
46 #include <linux/highmem.h>
47 #include <linux/pagemap.h>
48 #include <linux/rbtree.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>
58 #include "llite_internal.h"
59 #include "vvp_internal.h"
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
66 struct lu_env *cl_inode_fini_env;
67 int cl_inode_fini_refcheck;
70 * A mutex serializing calls to slp_inode_fini() under extreme memory
71 * pressure, when environments cannot be allocated.
73 static DEFINE_MUTEX(cl_inode_fini_guard);
75 int cl_setattr_ost(struct inode *inode, const struct iattr *attr,
76 struct obd_capa *capa)
85 env = cl_env_get(&refcheck);
89 io = vvp_env_thread_io(env);
90 io->ci_obj = ll_i2info(inode)->lli_clob;
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_valid = attr->ia_valid;
97 io->u.ci_setattr.sa_parent_fid = ll_inode2fid(inode);
98 io->u.ci_setattr.sa_capa = capa;
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)
105 /* populate the file descriptor for ftruncate to honor
106 * group lock - see LU-787 */
107 vio->vui_fd = LUSTRE_FPRIVATE(attr->ia_file);
109 result = cl_io_loop(env, io);
111 result = io->ci_result;
114 if (unlikely(io->ci_need_restart))
116 /* HSM import case: file is released, cannot be restored
117 * no need to fail except if restore registration failed
119 if (result == -ENODATA && io->ci_restore_needed &&
120 io->ci_result != -ENODATA)
122 cl_env_put(env, &refcheck);
127 * Initialize or update CLIO structures for regular files when new
128 * meta-data arrives from the server.
130 * \param inode regular file inode
131 * \param md new file metadata from MDS
132 * - allocates cl_object if necessary,
133 * - updated layout, if object was already here.
135 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
138 struct ll_inode_info *lli;
139 struct cl_object *clob;
140 struct lu_site *site;
142 struct cl_object_conf conf = {
151 LASSERT(md->body->mbo_valid & OBD_MD_FLID);
152 LASSERT(S_ISREG(inode->i_mode));
154 env = cl_env_get(&refcheck);
158 site = ll_i2sbi(inode)->ll_site;
159 lli = ll_i2info(inode);
161 LASSERT(fid_is_sane(fid));
163 if (lli->lli_clob == NULL) {
164 /* clob is slave of inode, empty lli_clob means for new inode,
165 * there is no clob in cache with the given fid, so it is
166 * unnecessary to perform lookup-alloc-lookup-insert, just
167 * alloc and insert directly. */
168 LASSERT(inode->i_state & I_NEW);
169 conf.coc_lu.loc_flags = LOC_F_NEW;
170 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
174 * No locking is necessary, as new inode is
175 * locked by I_NEW bit.
177 lli->lli_clob = clob;
178 lli->lli_has_smd = lsm_has_objects(md->lsm);
179 lu_object_ref_add(&clob->co_lu, "inode", inode);
181 result = PTR_ERR(clob);
183 result = cl_conf_set(env, lli->lli_clob, &conf);
186 cl_env_put(env, &refcheck);
189 CERROR("Failure to initialize cl object "DFID": %d\n",
195 * Wait for others drop their references of the object at first, then we drop
196 * the last one, which will lead to the object be destroyed immediately.
197 * Must be called after cl_object_kill() against this object.
199 * The reason we want to do this is: destroying top object will wait for sub
200 * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
201 * to initiate top object destroying which may deadlock. See bz22520.
203 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
205 struct lu_object_header *header = obj->co_lu.lo_header;
208 if (unlikely(atomic_read(&header->loh_ref) != 1)) {
209 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
210 struct lu_site_bkt_data *bkt;
212 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
214 init_waitqueue_entry_current(&waiter);
215 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
218 set_current_state(TASK_UNINTERRUPTIBLE);
219 if (atomic_read(&header->loh_ref) == 1)
221 waitq_wait(&waiter, TASK_UNINTERRUPTIBLE);
224 set_current_state(TASK_RUNNING);
225 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
228 cl_object_put(env, obj);
231 void cl_inode_fini(struct inode *inode)
234 struct ll_inode_info *lli = ll_i2info(inode);
235 struct cl_object *clob = lli->lli_clob;
242 cookie = cl_env_reenter();
243 env = cl_env_get(&refcheck);
244 emergency = IS_ERR(env);
246 mutex_lock(&cl_inode_fini_guard);
247 LASSERT(cl_inode_fini_env != NULL);
248 cl_env_implant(cl_inode_fini_env, &refcheck);
249 env = cl_inode_fini_env;
253 * cl_object cache is a slave to inode cache (which, in turn
254 * is a slave to dentry cache), don't keep cl_object in memory
255 * when its master is evicted.
257 cl_object_kill(env, clob);
258 lu_object_ref_del(&clob->co_lu, "inode", inode);
259 cl_object_put_last(env, clob);
260 lli->lli_clob = NULL;
262 cl_env_unplant(cl_inode_fini_env, &refcheck);
263 mutex_unlock(&cl_inode_fini_guard);
265 cl_env_put(env, &refcheck);
268 cl_env_reexit(cookie);
273 * build inode number from passed @fid */
274 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
276 if (BITS_PER_LONG == 32 || api32)
277 RETURN(fid_flatten32(fid));
279 RETURN(fid_flatten(fid));
283 * build inode generation from passed @fid. If our FID overflows the 32-bit
284 * inode number then return a non-zero generation to distinguish them. */
285 __u32 cl_fid_build_gen(const struct lu_fid *fid)
290 if (fid_is_igif(fid)) {
291 gen = lu_igif_gen(fid);
295 gen = (fid_flatten(fid) >> 32);
299 /* lsm is unreliable after hsm implementation as layout can be changed at
300 * any time. This is only to support old, non-clio-ized interfaces. It will
301 * cause deadlock if clio operations are called with this extra layout refcount
302 * because in case the layout changed during the IO, ll_layout_refresh() will
303 * have to wait for the refcount to become zero to destroy the older layout.
305 * Notice that the lsm returned by this function may not be valid unless called
306 * inside layout lock - MDS_INODELOCK_LAYOUT. */
307 struct lov_stripe_md *ccc_inode_lsm_get(struct inode *inode)
309 return lov_lsm_get(ll_i2info(inode)->lli_clob);
312 void inline ccc_inode_lsm_put(struct inode *inode, struct lov_stripe_md *lsm)
314 lov_lsm_put(ll_i2info(inode)->lli_clob, lsm);