Whamcloud - gitweb
LU-1222 ldlm: Fix the race in AST sender vs multiple arriving RPCs
[fs/lustre-release.git] / lustre / lclient / glimpse.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * glimpse code shared between vvp and liblustre (and other Lustre clients in
39  * the future).
40  *
41  *   Author: Nikita Danilov <nikita.danilov@sun.com>
42  *   Author: Oleg Drokin <oleg.drokin@sun.com>
43  */
44
45 #include <libcfs/libcfs.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <obd.h>
49
50 #ifdef __KERNEL__
51 # include <lustre_dlm.h>
52 # include <lustre_lite.h>
53 # include <lustre_mdc.h>
54 # include <linux/pagemap.h>
55 # include <linux/file.h>
56 #else
57 #include <stdlib.h>
58 #include <string.h>
59 #include <assert.h>
60 #include <time.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <sys/queue.h>
64 #include <fcntl.h>
65 #include <liblustre.h>
66 #endif
67
68 #include "cl_object.h"
69 #include "lclient.h"
70 #ifdef __KERNEL__
71 # include "../llite/llite_internal.h"
72 #else
73 # include "../liblustre/llite_lib.h"
74 #endif
75
76 static const struct cl_lock_descr whole_file = {
77         .cld_start = 0,
78         .cld_end   = CL_PAGE_EOF,
79         .cld_mode  = CLM_READ
80 };
81
82 /*
83  * Check whether file has possible unwriten pages.
84  *
85  * \retval 1    file is mmap-ed or has dirty pages
86  *         0    otherwise
87  */
88 blkcnt_t dirty_cnt(struct inode *inode)
89 {
90         blkcnt_t cnt = 0;
91 #ifdef __KERNEL__
92         struct ccc_object *vob = cl_inode2ccc(inode);
93         void              *results[1];
94
95         if (inode->i_mapping != NULL)
96                 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
97                                                   results, 0, 1,
98                                                   PAGECACHE_TAG_DIRTY);
99         if (cnt == 0 && cfs_atomic_read(&vob->cob_mmap_cnt) > 0)
100                 cnt = 1;
101
102 #endif
103         return (cnt > 0) ? 1 : 0;
104 }
105
106 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
107                     struct inode *inode, struct cl_object *clob, int agl)
108 {
109         struct cl_lock_descr *descr = &ccc_env_info(env)->cti_descr;
110         struct cl_inode_info *lli   = cl_i2info(inode);
111         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
112         struct ccc_io        *cio   = ccc_env_io(env);
113         struct cl_lock       *lock;
114         int result;
115
116         ENTRY;
117         result = 0;
118         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
119                 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
120                 if (lli->lli_smd) {
121                         /* NOTE: this looks like DLM lock request, but it may
122                          *       not be one. Due to CEF_ASYNC flag (translated
123                          *       to LDLM_FL_HAS_INTENT by osc), this is
124                          *       glimpse request, that won't revoke any
125                          *       conflicting DLM locks held. Instead,
126                          *       ll_glimpse_callback() will be called on each
127                          *       client holding a DLM lock against this file,
128                          *       and resulting size will be returned for each
129                          *       stripe. DLM lock on [0, EOF] is acquired only
130                          *       if there were no conflicting locks. If there
131                          *       were conflicting locks, enqueuing or waiting
132                          *       fails with -ENAVAIL, but valid inode
133                          *       attributes are returned anyway. */
134                         *descr = whole_file;
135                         descr->cld_obj   = clob;
136                         descr->cld_mode  = CLM_PHANTOM;
137                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
138                         if (agl)
139                                 descr->cld_enq_flags |= CEF_AGL;
140                         cio->cui_glimpse = 1;
141                         /*
142                          * CEF_ASYNC is used because glimpse sub-locks cannot
143                          * deadlock (because they never conflict with other
144                          * locks) and, hence, can be enqueued out-of-order.
145                          *
146                          * CEF_MUST protects glimpse lock from conversion into
147                          * a lockless mode.
148                          */
149                         lock = cl_lock_request(env, io, descr, "glimpse",
150                                                cfs_current());
151                         cio->cui_glimpse = 0;
152
153                         if (lock == NULL)
154                                 RETURN(0);
155
156                         if (IS_ERR(lock))
157                                 RETURN(PTR_ERR(lock));
158
159                         LASSERT(agl == 0);
160                         result = cl_wait(env, lock);
161                         if (result == 0) {
162                                 cl_merge_lvb(inode);
163                                 if (cl_isize_read(inode) > 0 &&
164                                     inode->i_blocks == 0) {
165                                         /*
166                                          * LU-417: Add dirty pages block count
167                                          * lest i_blocks reports 0, some "cp" or
168                                          * "tar" may think it's a completely
169                                          * sparse file and skip it.
170                                          */
171                                         inode->i_blocks = dirty_cnt(inode);
172                                 }
173                                 cl_unuse(env, lock);
174                         }
175                         cl_lock_release(env, lock, "glimpse", cfs_current());
176                 } else {
177                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
178                 }
179         }
180
181         RETURN(result);
182 }
183
184 static int cl_io_get(struct inode *inode, struct lu_env **envout,
185                      struct cl_io **ioout, int *refcheck)
186 {
187         struct lu_env          *env;
188         struct cl_io           *io;
189         struct cl_inode_info   *lli = cl_i2info(inode);
190         struct cl_object       *clob = lli->lli_clob;
191         int result;
192
193         if (S_ISREG(cl_inode_mode(inode))) {
194                 env = cl_env_get(refcheck);
195                 if (!IS_ERR(env)) {
196                         io = ccc_env_thread_io(env);
197                         io->ci_obj = clob;
198                         *envout = env;
199                         *ioout  = io;
200                         result = +1;
201                 } else
202                         result = PTR_ERR(env);
203         } else
204                 result = 0;
205         return result;
206 }
207
208 int cl_glimpse_size0(struct inode *inode, int agl)
209 {
210         /*
211          * We don't need ast_flags argument to cl_glimpse_size(), because
212          * osc_lock_enqueue() takes care of the possible deadlock that said
213          * argument was introduced to avoid.
214          */
215         /*
216          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
217          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
218          * blocking anyway.
219          */
220         struct lu_env          *env = NULL;
221         struct cl_io           *io  = NULL;
222         int                     result;
223         int                     refcheck;
224
225         ENTRY;
226
227         result = cl_io_get(inode, &env, &io, &refcheck);
228         if (result > 0) {
229                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
230                 if (result > 0)
231                         /*
232                          * nothing to do for this io. This currently happens
233                          * when stripe sub-object's are not yet created.
234                          */
235                         result = io->ci_result;
236                 else if (result == 0)
237                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
238                                                  agl);
239                 cl_io_fini(env, io);
240                 cl_env_put(env, &refcheck);
241         }
242         RETURN(result);
243 }
244
245 int cl_local_size(struct inode *inode)
246 {
247         struct lu_env           *env = NULL;
248         struct cl_io            *io  = NULL;
249         struct ccc_thread_info  *cti;
250         struct cl_object        *clob;
251         struct cl_lock_descr    *descr;
252         struct cl_lock          *lock;
253         int                      result;
254         int                      refcheck;
255
256         ENTRY;
257
258         if (!cl_i2info(inode)->lli_smd)
259                 RETURN(0);
260
261         result = cl_io_get(inode, &env, &io, &refcheck);
262         if (result <= 0)
263                 RETURN(result);
264
265         clob = io->ci_obj;
266         result = cl_io_init(env, io, CIT_MISC, clob);
267         if (result > 0)
268                 result = io->ci_result;
269         else if (result == 0) {
270                 cti = ccc_env_info(env);
271                 descr = &cti->cti_descr;
272
273                 *descr = whole_file;
274                 descr->cld_obj = clob;
275                 lock = cl_lock_peek(env, io, descr, "localsize", cfs_current());
276                 if (lock != NULL) {
277                         cl_merge_lvb(inode);
278                         cl_unuse(env, lock);
279                         cl_lock_release(env, lock, "localsize", cfs_current());
280                         result = 0;
281                 } else
282                         result = -ENODATA;
283         }
284         cl_io_fini(env, io);
285         cl_env_put(env, &refcheck);
286         RETURN(result);
287 }
288