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