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 * glimpse code shared between vvp and liblustre (and other Lustre clients in
39 * Author: Nikita Danilov <nikita.danilov@sun.com>
40 * Author: Oleg Drokin <oleg.drokin@sun.com>
43 #include <libcfs/libcfs.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
48 #include <lustre_dlm.h>
49 #include <lustre_mdc.h>
50 #include <linux/pagemap.h>
51 #include <linux/file.h>
53 #include "cl_object.h"
54 #include "llite_internal.h"
55 #include "vvp_internal.h"
57 static const struct cl_lock_descr whole_file = {
59 .cld_end = CL_PAGE_EOF,
64 * Check whether file has possible unwritten pages.
66 * \retval 1 file is mmap-ed or has dirty pages
69 blkcnt_t dirty_cnt(struct inode *inode)
72 struct vvp_object *vob = cl_inode2vvp(inode);
75 if (inode->i_mapping != NULL)
76 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
79 if (cnt == 0 && atomic_read(&vob->vob_mmap_cnt) > 0)
82 return (cnt > 0) ? 1 : 0;
85 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
86 struct inode *inode, struct cl_object *clob, int agl)
88 struct ll_inode_info *lli = ll_i2info(inode);
89 const struct lu_fid *fid = lu_object_fid(&clob->co_lu);
95 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
96 if (lli->lli_has_smd) {
97 struct cl_lock *lock = vvp_env_lock(env);
98 struct cl_lock_descr *descr = &lock->cll_descr;
100 /* NOTE: this looks like DLM lock request, but it may
101 * not be one. Due to CEF_ASYNC flag (translated
102 * to LDLM_FL_HAS_INTENT by osc), this is
103 * glimpse request, that won't revoke any
104 * conflicting DLM locks held. Instead,
105 * ll_glimpse_callback() will be called on each
106 * client holding a DLM lock against this file,
107 * and resulting size will be returned for each
108 * stripe. DLM lock on [0, EOF] is acquired only
109 * if there were no conflicting locks. If there
110 * were conflicting locks, enqueuing or waiting
111 * fails with -ENAVAIL, but valid inode
112 * attributes are returned anyway. */
114 descr->cld_obj = clob;
115 descr->cld_mode = CLM_READ;
116 descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
118 descr->cld_enq_flags |= CEF_AGL;
120 * CEF_ASYNC is used because glimpse sub-locks cannot
121 * deadlock (because they never conflict with other
122 * locks) and, hence, can be enqueued out-of-order.
124 * CEF_MUST protects glimpse lock from conversion into
127 result = cl_lock_request(env, io, lock);
132 ll_merge_attr(env, inode);
133 if (i_size_read(inode) > 0 && inode->i_blocks == 0) {
135 * LU-417: Add dirty pages block count
136 * lest i_blocks reports 0, some "cp" or
137 * "tar" may think it's a completely
138 * sparse file and skip it.
140 inode->i_blocks = dirty_cnt(inode);
144 cl_lock_release(env, lock);
146 CDEBUG(D_DLMTRACE, "No objects for inode\n");
147 ll_merge_attr(env, inode);
153 static int cl_io_get(struct inode *inode, struct lu_env **envout,
154 struct cl_io **ioout, int *refcheck)
158 struct ll_inode_info *lli = ll_i2info(inode);
159 struct cl_object *clob = lli->lli_clob;
162 if (S_ISREG(inode->i_mode)) {
163 env = cl_env_get(refcheck);
165 io = vvp_env_thread_io(env);
171 result = PTR_ERR(env);
177 int cl_glimpse_size0(struct inode *inode, int agl)
180 * We don't need ast_flags argument to cl_glimpse_size(), because
181 * osc_lock_enqueue() takes care of the possible deadlock that said
182 * argument was introduced to avoid.
185 * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
186 * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
189 struct lu_env *env = NULL;
190 struct cl_io *io = NULL;
196 result = cl_io_get(inode, &env, &io, &refcheck);
199 io->ci_verify_layout = 1;
200 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
203 * nothing to do for this io. This currently happens
204 * when stripe sub-object's are not yet created.
206 result = io->ci_result;
207 else if (result == 0)
208 result = cl_glimpse_lock(env, io, inode, io->ci_obj,
211 OBD_FAIL_TIMEOUT(OBD_FAIL_GLIMPSE_DELAY, 2);
213 if (unlikely(io->ci_need_restart))
215 cl_env_put(env, &refcheck);
220 int cl_local_size(struct inode *inode)
222 struct lu_env *env = NULL;
223 struct cl_io *io = NULL;
224 struct cl_object *clob;
230 if (!ll_i2info(inode)->lli_has_smd)
233 result = cl_io_get(inode, &env, &io, &refcheck);
238 result = cl_io_init(env, io, CIT_MISC, clob);
240 result = io->ci_result;
241 else if (result == 0) {
242 struct cl_lock *lock = vvp_env_lock(env);
244 lock->cll_descr = whole_file;
245 lock->cll_descr.cld_enq_flags = CEF_PEEK;
246 lock->cll_descr.cld_obj = clob;
247 result = cl_lock_request(env, io, lock);
249 ll_merge_attr(env, inode);
250 cl_lock_release(env, lock);
254 cl_env_put(env, &refcheck);