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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * cl code shared between vvp and liblustre (and other Lustre clients in the
35 * Author: Nikita Danilov <nikita.danilov@sun.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <linux/libcfs/libcfs.h>
42 #include <linux/sched.h>
44 #include <linux/quotaops.h>
45 #include <linux/highmem.h>
46 #include <linux/pagemap.h>
47 #include <linux/rbtree.h>
50 #include <obd_support.h>
51 #include <lustre_fid.h>
52 #include <lustre_dlm.h>
53 #include <lustre_mdc.h>
54 #include <cl_object.h>
56 #include "llite_internal.h"
59 * ccc_ prefix stands for "Common Client Code".
62 /*****************************************************************************
64 * Vvp device and device type functions.
69 * An `emergency' environment used by cl_inode_fini() when cl_env_get()
70 * fails. Access to this environment is serialized by cl_inode_fini_guard
73 struct lu_env *cl_inode_fini_env;
74 u16 cl_inode_fini_refcheck;
77 * A mutex serializing calls to slp_inode_fini() under extreme memory
78 * pressure, when environments cannot be allocated.
80 static DEFINE_MUTEX(cl_inode_fini_guard);
82 int cl_setattr_ost(struct cl_object *obj, const struct iattr *attr,
83 unsigned int attr_flags)
90 env = cl_env_get(&refcheck);
94 io = vvp_env_thread_io(env);
96 io->ci_verify_layout = 1;
98 io->u.ci_setattr.sa_attr.lvb_atime = LTIME_S(attr->ia_atime);
99 io->u.ci_setattr.sa_attr.lvb_mtime = LTIME_S(attr->ia_mtime);
100 io->u.ci_setattr.sa_attr.lvb_ctime = LTIME_S(attr->ia_ctime);
101 io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
102 io->u.ci_setattr.sa_attr_flags = attr_flags;
103 io->u.ci_setattr.sa_valid = attr->ia_valid;
104 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&obj->co_lu);
107 if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0) {
108 struct vvp_io *vio = vvp_env_io(env);
110 if (attr->ia_valid & ATTR_FILE)
111 /* populate the file descriptor for ftruncate to honor
112 * group lock - see LU-787
114 vio->vui_fd = LUSTRE_FPRIVATE(attr->ia_file);
116 result = cl_io_loop(env, io);
118 result = io->ci_result;
121 if (unlikely(io->ci_need_restart))
124 cl_env_put(env, &refcheck);
129 * Initialize or update CLIO structures for regular files when new
130 * meta-data arrives from the server.
132 * \param inode regular file inode
133 * \param md new file metadata from MDS
134 * - allocates cl_object if necessary,
135 * - updated layout, if object was already here.
137 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
140 struct ll_inode_info *lli;
141 struct cl_object *clob;
142 struct lu_site *site;
144 struct cl_object_conf conf = {
147 .coc_layout = md->layout,
153 LASSERT(md->body->mbo_valid & OBD_MD_FLID);
154 LASSERT(S_ISREG(inode->i_mode));
156 env = cl_env_get(&refcheck);
160 site = ll_i2sbi(inode)->ll_site;
161 lli = ll_i2info(inode);
163 LASSERT(fid_is_sane(fid));
165 if (!lli->lli_clob) {
166 /* clob is slave of inode, empty lli_clob means for new inode,
167 * there is no clob in cache with the given fid, so it is
168 * unnecessary to perform lookup-alloc-lookup-insert, just
169 * alloc and insert directly.
171 LASSERT(inode->i_state & I_NEW);
172 conf.coc_lu.loc_flags = LOC_F_NEW;
173 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
177 * No locking is necessary, as new inode is
178 * locked by I_NEW bit.
180 lli->lli_clob = clob;
181 lu_object_ref_add(&clob->co_lu, "inode", inode);
183 result = PTR_ERR(clob);
186 result = cl_conf_set(env, lli->lli_clob, &conf);
189 cl_env_put(env, &refcheck);
192 CERROR("Failure to initialize cl object " DFID ": %d\n",
198 * Wait for others drop their references of the object at first, then we drop
199 * the last one, which will lead to the object be destroyed immediately.
200 * Must be called after cl_object_kill() against this object.
202 * The reason we want to do this is: destroying top object will wait for sub
203 * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
204 * to initiate top object destroying which may deadlock. See bz22520.
206 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
208 struct lu_object_header *header = obj->co_lu.lo_header;
209 wait_queue_entry_t waiter;
211 if (unlikely(atomic_read(&header->loh_ref) != 1)) {
212 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
213 struct lu_site_bkt_data *bkt;
215 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
217 init_waitqueue_entry(&waiter, current);
218 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
221 set_current_state(TASK_UNINTERRUPTIBLE);
222 if (atomic_read(&header->loh_ref) == 1)
227 set_current_state(TASK_RUNNING);
228 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
231 cl_object_put(env, obj);
234 void cl_inode_fini(struct inode *inode)
237 struct ll_inode_info *lli = ll_i2info(inode);
238 struct cl_object *clob = lli->lli_clob;
243 env = cl_env_get(&refcheck);
244 emergency = IS_ERR(env);
246 mutex_lock(&cl_inode_fini_guard);
247 LASSERT(cl_inode_fini_env);
248 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 lu_object_ref_del(&clob->co_lu, "inode", inode);
257 cl_object_put_last(env, clob);
258 lli->lli_clob = NULL;
260 mutex_unlock(&cl_inode_fini_guard);
262 cl_env_put(env, &refcheck);
267 * build inode number from passed @fid
269 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
271 if (BITS_PER_LONG == 32 || api32)
272 return fid_flatten32(fid);
274 return fid_flatten(fid);
278 * build inode generation from passed @fid. If our FID overflows the 32-bit
279 * inode number then return a non-zero generation to distinguish them.
281 __u32 cl_fid_build_gen(const struct lu_fid *fid)
285 if (fid_is_igif(fid)) {
286 gen = lu_igif_gen(fid);
290 gen = fid_flatten(fid) >> 32;