Whamcloud - gitweb
94467aa13851be508d0e38996cbee0ec396f8c00
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * glimpse code used by vvp (and other Lustre clients in the future).
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Oleg Drokin <oleg.drokin@sun.com>
36  */
37
38 #include <libcfs/libcfs.h>
39 #include <obd_class.h>
40 #include <obd_support.h>
41 #include <obd.h>
42
43 #include <lustre_dlm.h>
44 #include <lustre_mdc.h>
45 #include <linux/pagemap.h>
46 #include <linux/file.h>
47
48 #include "cl_object.h"
49 #include "llite_internal.h"
50 #include "vvp_internal.h"
51
52 static const struct cl_lock_descr whole_file = {
53         .cld_start = 0,
54         .cld_end   = CL_PAGE_EOF,
55         .cld_mode  = CLM_READ
56 };
57
58 /*
59  * Check whether file has possible unwritten pages.
60  *
61  * \retval 1    file is mmap-ed or has dirty pages
62  *         0    otherwise
63  */
64 blkcnt_t dirty_cnt(struct inode *inode)
65 {
66         blkcnt_t cnt = 0;
67         struct vvp_object *vob = cl_inode2vvp(inode);
68         void              *results[1];
69
70         if (inode->i_mapping != NULL)
71                 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
72                                                   results, 0, 1,
73                                                   PAGECACHE_TAG_DIRTY);
74         if (cnt == 0 && atomic_read(&vob->vob_mmap_cnt) > 0)
75                 cnt = 1;
76
77         return (cnt > 0) ? 1 : 0;
78 }
79
80 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
81                     struct inode *inode, struct cl_object *clob, int agl)
82 {
83         const struct lu_fid *fid = lu_object_fid(&clob->co_lu);
84         struct cl_lock *lock = vvp_env_lock(env);
85         struct cl_lock_descr *descr = &lock->cll_descr;
86         int result;
87
88         ENTRY;
89         result = 0;
90
91         CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
92
93         /* NOTE: this looks like DLM lock request, but it may
94          *       not be one. Due to CEF_GLIMPSE flag (translated
95          *       to LDLM_FL_HAS_INTENT by osc), this is
96          *       glimpse request, that won't revoke any
97          *       conflicting DLM locks held. Instead,
98          *       ll_glimpse_callback() will be called on each
99          *       client holding a DLM lock against this file,
100          *       and resulting size will be returned for each
101          *       stripe. DLM lock on [0, EOF] is acquired only
102          *       if there were no conflicting locks. If there
103          *       were conflicting locks, enqueuing or waiting
104          *       fails with -ENAVAIL, but valid inode
105          *       attributes are returned anyway. */
106         *descr = whole_file;
107         descr->cld_obj = clob;
108         descr->cld_mode = CLM_READ;
109         descr->cld_enq_flags = CEF_GLIMPSE | CEF_MUST;
110         if (agl)
111                 descr->cld_enq_flags |= CEF_SPECULATIVE | CEF_NONBLOCK;
112         /*
113          * CEF_MUST protects glimpse lock from conversion into
114          * a lockless mode.
115          */
116         result = cl_lock_request(env, io, lock);
117         if (result < 0)
118                 RETURN(result);
119
120         if (!agl) {
121                 ll_merge_attr(env, inode);
122                 if (i_size_read(inode) > 0 && inode->i_blocks == 0) {
123                         /*
124                          * LU-417: Add dirty pages block count
125                          * lest i_blocks reports 0, some "cp" or
126                          * "tar" may think it's a completely
127                          * sparse file and skip it.
128                          */
129                         inode->i_blocks = dirty_cnt(inode);
130                 }
131         }
132
133         cl_lock_release(env, lock);
134
135         RETURN(result);
136 }
137
138 /**
139  * Get an IO environment for special operations such as glimpse locks and
140  * manually requested locks (ladvise lockahead)
141  *
142  * \param[in]  inode    inode the operation is being performed on
143  * \param[out] envout   thread specific execution environment
144  * \param[out] ioout    client io description
145  * \param[out] refcheck reference check
146  *
147  * \retval 1            on success
148  * \retval 0            not a regular file, cannot get environment
149  * \retval negative     negative errno on error
150  */
151 int cl_io_get(struct inode *inode, struct lu_env **envout,
152                      struct cl_io **ioout, __u16 *refcheck)
153 {
154         struct lu_env           *env;
155         struct cl_io            *io;
156         struct ll_inode_info    *lli = ll_i2info(inode);
157         struct cl_object        *clob = lli->lli_clob;
158         int result;
159
160         if (S_ISREG(inode->i_mode)) {
161                 env = cl_env_get(refcheck);
162                 if (!IS_ERR(env)) {
163                         io = vvp_env_thread_io(env);
164                         io->ci_obj = clob;
165                         *envout = env;
166                         *ioout  = io;
167                         result = +1;
168                 } else
169                         result = PTR_ERR(env);
170         } else
171                 result = 0;
172         return result;
173 }
174
175 int cl_glimpse_size0(struct inode *inode, int agl)
176 {
177         /*
178          * We don't need ast_flags argument to cl_glimpse_size(), because
179          * osc_lock_enqueue() takes care of the possible deadlock that said
180          * argument was introduced to avoid.
181          */
182         /*
183          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
184          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
185          * blocking anyway.
186          */
187         struct lu_env          *env = NULL;
188         struct cl_io           *io  = NULL;
189         __u16                   refcheck;
190         int                     retried = 0;
191         int                     result;
192
193         ENTRY;
194
195         result = cl_io_get(inode, &env, &io, &refcheck);
196         if (result <= 0)
197                 RETURN(result);
198
199         do {
200                 io->ci_ndelay_tried = retried++;
201                 io->ci_ndelay = io->ci_verify_layout = 1;
202                 result = cl_io_init(env, io, CIT_GLIMPSE, io->ci_obj);
203                 if (result > 0) {
204                         /*
205                          * nothing to do for this io. This currently happens
206                          * when stripe sub-object's are not yet created.
207                          */
208                         result = io->ci_result;
209                 } else if (result == 0) {
210                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
211                                                  agl);
212                         if (!agl && result == -EWOULDBLOCK)
213                                 io->ci_need_restart = 1;
214                 }
215
216                 OBD_FAIL_TIMEOUT(OBD_FAIL_GLIMPSE_DELAY, 2);
217                 cl_io_fini(env, io);
218         } while (unlikely(io->ci_need_restart));
219
220         cl_env_put(env, &refcheck);
221         RETURN(result);
222 }