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