Whamcloud - gitweb
539cf1cf7409e1a29adc5f9d5f5410926e4f1d11
[fs/lustre-release.git] / lustre / llite / 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, 2014, 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 #include <lustre_dlm.h>
49 #include <lustre_mdc.h>
50 #include <linux/pagemap.h>
51 #include <linux/file.h>
52
53 #include "cl_object.h"
54 #include "llite_internal.h"
55
56 static const struct cl_lock_descr whole_file = {
57         .cld_start = 0,
58         .cld_end   = CL_PAGE_EOF,
59         .cld_mode  = CLM_READ
60 };
61
62 /*
63  * Check whether file has possible unwriten pages.
64  *
65  * \retval 1    file is mmap-ed or has dirty pages
66  *         0    otherwise
67  */
68 blkcnt_t dirty_cnt(struct inode *inode)
69 {
70         blkcnt_t cnt = 0;
71         struct vvp_object *vob = cl_inode2vvp(inode);
72         void              *results[1];
73
74         if (inode->i_mapping != NULL)
75                 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
76                                                   results, 0, 1,
77                                                   PAGECACHE_TAG_DIRTY);
78         if (cnt == 0 && atomic_read(&vob->vob_mmap_cnt) > 0)
79                 cnt = 1;
80
81         return (cnt > 0) ? 1 : 0;
82 }
83
84 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
85                     struct inode *inode, struct cl_object *clob, int agl)
86 {
87         struct ll_inode_info *lli   = ll_i2info(inode);
88         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
89         int result;
90
91         ENTRY;
92         result = 0;
93
94         CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
95         if (lli->lli_has_smd) {
96                 struct cl_lock *lock = ccc_env_lock(env);
97                 struct cl_lock_descr *descr = &lock->cll_descr;
98
99                 /* NOTE: this looks like DLM lock request, but it may
100                  *       not be one. Due to CEF_ASYNC flag (translated
101                  *       to LDLM_FL_HAS_INTENT by osc), this is
102                  *       glimpse request, that won't revoke any
103                  *       conflicting DLM locks held. Instead,
104                  *       ll_glimpse_callback() will be called on each
105                  *       client holding a DLM lock against this file,
106                  *       and resulting size will be returned for each
107                  *       stripe. DLM lock on [0, EOF] is acquired only
108                  *       if there were no conflicting locks. If there
109                  *       were conflicting locks, enqueuing or waiting
110                  *       fails with -ENAVAIL, but valid inode
111                  *       attributes are returned anyway. */
112                 *descr = whole_file;
113                 descr->cld_obj = clob;
114                 descr->cld_mode = CLM_READ;
115                 descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
116                 if (agl)
117                         descr->cld_enq_flags |= CEF_AGL;
118                 /*
119                  * CEF_ASYNC is used because glimpse sub-locks cannot
120                  * deadlock (because they never conflict with other
121                  * locks) and, hence, can be enqueued out-of-order.
122                  *
123                  * CEF_MUST protects glimpse lock from conversion into
124                  * a lockless mode.
125                  */
126                 result = cl_lock_request(env, io, lock);
127                 if (result < 0)
128                         RETURN(result);
129
130                 if (!agl) {
131                         ll_merge_attr(env, inode);
132                         if (i_size_read(inode) > 0 && inode->i_blocks == 0) {
133                                 /*
134                                  * LU-417: Add dirty pages block count
135                                  * lest i_blocks reports 0, some "cp" or
136                                  * "tar" may think it's a completely
137                                  * sparse file and skip it.
138                                  */
139                                 inode->i_blocks = dirty_cnt(inode);
140                         }
141                 }
142
143                 cl_lock_release(env, lock);
144         } else {
145                 CDEBUG(D_DLMTRACE, "No objects for inode\n");
146                 ll_merge_attr(env, inode);
147         }
148
149         RETURN(result);
150 }
151
152 static int cl_io_get(struct inode *inode, struct lu_env **envout,
153                      struct cl_io **ioout, int *refcheck)
154 {
155         struct lu_env           *env;
156         struct cl_io            *io;
157         struct ll_inode_info    *lli = ll_i2info(inode);
158         struct cl_object        *clob = lli->lli_clob;
159         int result;
160
161         if (S_ISREG(inode->i_mode)) {
162                 env = cl_env_get(refcheck);
163                 if (!IS_ERR(env)) {
164                         io = ccc_env_thread_io(env);
165                         io->ci_obj = clob;
166                         *envout = env;
167                         *ioout  = io;
168                         result = +1;
169                 } else
170                         result = PTR_ERR(env);
171         } else
172                 result = 0;
173         return result;
174 }
175
176 int cl_glimpse_size0(struct inode *inode, int agl)
177 {
178         /*
179          * We don't need ast_flags argument to cl_glimpse_size(), because
180          * osc_lock_enqueue() takes care of the possible deadlock that said
181          * argument was introduced to avoid.
182          */
183         /*
184          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
185          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
186          * blocking anyway.
187          */
188         struct lu_env          *env = NULL;
189         struct cl_io           *io  = NULL;
190         int                     result;
191         int                     refcheck;
192
193         ENTRY;
194
195         result = cl_io_get(inode, &env, &io, &refcheck);
196         if (result > 0) {
197         again:
198                 io->ci_verify_layout = 1;
199                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
200                 if (result > 0)
201                         /*
202                          * nothing to do for this io. This currently happens
203                          * when stripe sub-object's are not yet created.
204                          */
205                         result = io->ci_result;
206                 else if (result == 0)
207                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
208                                                  agl);
209
210                 OBD_FAIL_TIMEOUT(OBD_FAIL_GLIMPSE_DELAY, 2);
211                 cl_io_fini(env, io);
212                 if (unlikely(io->ci_need_restart))
213                         goto again;
214                 cl_env_put(env, &refcheck);
215         }
216         RETURN(result);
217 }
218
219 int cl_local_size(struct inode *inode)
220 {
221         struct lu_env           *env = NULL;
222         struct cl_io            *io  = NULL;
223         struct cl_object        *clob;
224         int                      result;
225         int                      refcheck;
226
227         ENTRY;
228
229         if (!ll_i2info(inode)->lli_has_smd)
230                 RETURN(0);
231
232         result = cl_io_get(inode, &env, &io, &refcheck);
233         if (result <= 0)
234                 RETURN(result);
235
236         clob = io->ci_obj;
237         result = cl_io_init(env, io, CIT_MISC, clob);
238         if (result > 0)
239                 result = io->ci_result;
240         else if (result == 0) {
241                 struct cl_lock *lock = ccc_env_lock(env);
242
243                 lock->cll_descr = whole_file;
244                 lock->cll_descr.cld_enq_flags = CEF_PEEK;
245                 lock->cll_descr.cld_obj = clob;
246                 result = cl_lock_request(env, io, lock);
247                 if (result == 0) {
248                         ll_merge_attr(env, inode);
249                         cl_lock_release(env, lock);
250                 }
251         }
252         cl_io_fini(env, io);
253         cl_env_put(env, &refcheck);
254         RETURN(result);
255 }
256