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