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