Whamcloud - gitweb
LU-925 agl: async glimpse lock process in CLIO stack
[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 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * glimpse code shared between vvp and liblustre (and other Lustre clients in
40  * the future).
41  *
42  *   Author: Nikita Danilov <nikita.danilov@sun.com>
43  *   Author: Oleg Drokin <oleg.drokin@sun.com>
44  */
45
46 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <obd.h>
50
51 #ifdef __KERNEL__
52 # include <lustre_dlm.h>
53 # include <lustre_lite.h>
54 # include <lustre_mdc.h>
55 # include <linux/pagemap.h>
56 # include <linux/file.h>
57 #else
58 #include <stdlib.h>
59 #include <string.h>
60 #include <assert.h>
61 #include <time.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/queue.h>
65 #include <fcntl.h>
66 #include <liblustre.h>
67 #endif
68
69 #include "cl_object.h"
70 #include "lclient.h"
71 #ifdef __KERNEL__
72 # include "../llite/llite_internal.h"
73 #else
74 # include "../liblustre/llite_lib.h"
75 #endif
76
77 static const struct cl_lock_descr whole_file = {
78         .cld_start = 0,
79         .cld_end   = CL_PAGE_EOF,
80         .cld_mode  = CLM_READ
81 };
82
83 /*
84  * Check whether file has possible unwriten pages.
85  *
86  * \retval 1    file is mmap-ed or has dirty pages
87  *         0    otherwise
88  */
89 blkcnt_t dirty_cnt(struct inode *inode)
90 {
91         blkcnt_t cnt = 0;
92 #ifdef __KERNEL__
93         struct ccc_object *vob = cl_inode2ccc(inode);
94         void              *results[1];
95
96         if (inode->i_mapping != NULL)
97                 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
98                                                   results, 0, 1,
99                                                   PAGECACHE_TAG_DIRTY);
100         if (cnt == 0 && cfs_atomic_read(&vob->cob_mmap_cnt) > 0)
101                 cnt = 1;
102
103 #endif
104         return (cnt > 0) ? 1 : 0;
105 }
106
107 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
108                     struct inode *inode, struct cl_object *clob, int agl)
109 {
110         struct cl_lock_descr *descr = &ccc_env_info(env)->cti_descr;
111         struct cl_inode_info *lli   = cl_i2info(inode);
112         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
113         struct ccc_io        *cio   = ccc_env_io(env);
114         struct cl_lock       *lock;
115         int result;
116
117         ENTRY;
118         result = 0;
119         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
120                 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
121                 if (lli->lli_smd) {
122                         /* NOTE: this looks like DLM lock request, but it may
123                          *       not be one. Due to CEF_ASYNC flag (translated
124                          *       to LDLM_FL_HAS_INTENT by osc), this is
125                          *       glimpse request, that won't revoke any
126                          *       conflicting DLM locks held. Instead,
127                          *       ll_glimpse_callback() will be called on each
128                          *       client holding a DLM lock against this file,
129                          *       and resulting size will be returned for each
130                          *       stripe. DLM lock on [0, EOF] is acquired only
131                          *       if there were no conflicting locks. If there
132                          *       were conflicting locks, enqueuing or waiting
133                          *       fails with -ENAVAIL, but valid inode
134                          *       attributes are returned anyway. */
135                         *descr = whole_file;
136                         descr->cld_obj   = clob;
137                         descr->cld_mode  = CLM_PHANTOM;
138                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
139                         if (agl)
140                                 descr->cld_enq_flags |= CEF_AGL;
141                         cio->cui_glimpse = 1;
142                         /*
143                          * CEF_ASYNC is used because glimpse sub-locks cannot
144                          * deadlock (because they never conflict with other
145                          * locks) and, hence, can be enqueued out-of-order.
146                          *
147                          * CEF_MUST protects glimpse lock from conversion into
148                          * a lockless mode.
149                          */
150                         lock = cl_lock_request(env, io, descr, "glimpse",
151                                                cfs_current());
152                         cio->cui_glimpse = 0;
153
154                         if (lock == NULL)
155                                 RETURN(0);
156
157                         if (IS_ERR(lock))
158                                 RETURN(PTR_ERR(lock));
159
160                         LASSERT(agl == 0);
161                         result = cl_wait(env, lock);
162                         if (result == 0) {
163                                 cl_merge_lvb(inode);
164                                 if (cl_isize_read(inode) > 0 &&
165                                     inode->i_blocks == 0) {
166                                         /*
167                                          * LU-417: Add dirty pages block count
168                                          * lest i_blocks reports 0, some "cp" or
169                                          * "tar" may think it's a completely
170                                          * sparse file and skip it.
171                                          */
172                                         inode->i_blocks = dirty_cnt(inode);
173                                 }
174                                 cl_unuse(env, lock);
175                         }
176                         cl_lock_release(env, lock, "glimpse", cfs_current());
177                 } else {
178                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
179                 }
180         }
181
182         RETURN(result);
183 }
184
185 static int cl_io_get(struct inode *inode, struct lu_env **envout,
186                      struct cl_io **ioout, int *refcheck)
187 {
188         struct lu_env          *env;
189         struct cl_io           *io;
190         struct cl_inode_info   *lli = cl_i2info(inode);
191         struct cl_object       *clob = lli->lli_clob;
192         int result;
193
194         if (S_ISREG(cl_inode_mode(inode))) {
195                 env = cl_env_get(refcheck);
196                 if (!IS_ERR(env)) {
197                         io = ccc_env_thread_io(env);
198                         io->ci_obj = clob;
199                         *envout = env;
200                         *ioout  = io;
201                         result = +1;
202                 } else
203                         result = PTR_ERR(env);
204         } else
205                 result = 0;
206         return result;
207 }
208
209 int cl_glimpse_size0(struct inode *inode, int agl)
210 {
211         /*
212          * We don't need ast_flags argument to cl_glimpse_size(), because
213          * osc_lock_enqueue() takes care of the possible deadlock that said
214          * argument was introduced to avoid.
215          */
216         /*
217          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
218          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
219          * blocking anyway.
220          */
221         struct lu_env          *env = NULL;
222         struct cl_io           *io  = NULL;
223         int                     result;
224         int                     refcheck;
225
226         ENTRY;
227
228         result = cl_io_get(inode, &env, &io, &refcheck);
229         if (result > 0) {
230                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
231                 if (result > 0)
232                         /*
233                          * nothing to do for this io. This currently happens
234                          * when stripe sub-object's are not yet created.
235                          */
236                         result = io->ci_result;
237                 else if (result == 0)
238                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
239                                                  agl);
240                 cl_io_fini(env, io);
241                 cl_env_put(env, &refcheck);
242         }
243         RETURN(result);
244 }
245
246 int cl_local_size(struct inode *inode)
247 {
248         struct lu_env           *env = NULL;
249         struct cl_io            *io  = NULL;
250         struct ccc_thread_info  *cti;
251         struct cl_object        *clob;
252         struct cl_lock_descr    *descr;
253         struct cl_lock          *lock;
254         int                      result;
255         int                      refcheck;
256
257         ENTRY;
258
259         if (!cl_i2info(inode)->lli_smd)
260                 RETURN(0);
261
262         result = cl_io_get(inode, &env, &io, &refcheck);
263         if (result <= 0)
264                 RETURN(result);
265
266         clob = io->ci_obj;
267         result = cl_io_init(env, io, CIT_MISC, clob);
268         if (result > 0)
269                 result = io->ci_result;
270         else if (result == 0) {
271                 cti = ccc_env_info(env);
272                 descr = &cti->cti_descr;
273
274                 *descr = whole_file;
275                 descr->cld_obj = clob;
276                 lock = cl_lock_peek(env, io, descr, "localsize", cfs_current());
277                 if (lock != NULL) {
278                         cl_merge_lvb(inode);
279                         cl_unuse(env, lock);
280                         cl_lock_release(env, lock, "localsize", cfs_current());
281                         result = 0;
282                 } else
283                         result = -ENODATA;
284         }
285         cl_io_fini(env, io);
286         cl_env_put(env, &refcheck);
287         RETURN(result);
288 }
289