staging/lustre: Replace sun.com GPLv2 URL with gnu.org one.
[linux-2.6-block.git] / drivers / staging / lustre / 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, 2012, 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 "../../include/linux/libcfs/libcfs.h"
40 #include "../include/obd_class.h"
41 #include "../include/obd_support.h"
42 #include "../include/obd.h"
43
44 #include "../include/lustre_dlm.h"
45 #include "../include/lustre_lite.h"
46 #include "../include/lustre_mdc.h"
47 #include <linux/pagemap.h>
48 #include <linux/file.h>
49
50 #include "../include/cl_object.h"
51 #include "../llite/llite_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 unwriten 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)
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         struct ll_inode_info *lli   = ll_i2info(inode);
85         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
86         int result;
87
88         result = 0;
89         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
90                 CDEBUG(D_DLMTRACE, "Glimpsing inode " DFID "\n", PFID(fid));
91                 if (lli->lli_has_smd) {
92                         struct cl_lock *lock = vvp_env_lock(env);
93                         struct cl_lock_descr *descr = &lock->cll_descr;
94
95                         /* NOTE: this looks like DLM lock request, but it may
96                          *       not be one. Due to CEF_ASYNC flag (translated
97                          *       to LDLM_FL_HAS_INTENT by osc), this is
98                          *       glimpse request, that won't revoke any
99                          *       conflicting DLM locks held. Instead,
100                          *       ll_glimpse_callback() will be called on each
101                          *       client holding a DLM lock against this file,
102                          *       and resulting size will be returned for each
103                          *       stripe. DLM lock on [0, EOF] is acquired only
104                          *       if there were no conflicting locks. If there
105                          *       were conflicting locks, enqueuing or waiting
106                          *       fails with -ENAVAIL, but valid inode
107                          *       attributes are returned anyway.
108                          */
109                         *descr = whole_file;
110                         descr->cld_obj   = clob;
111                         descr->cld_mode  = CLM_READ;
112                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
113                         if (agl)
114                                 descr->cld_enq_flags |= CEF_AGL;
115                         /*
116                          * CEF_ASYNC is used because glimpse sub-locks cannot
117                          * deadlock (because they never conflict with other
118                          * locks) and, hence, can be enqueued out-of-order.
119                          *
120                          * CEF_MUST protects glimpse lock from conversion into
121                          * a lockless mode.
122                          */
123                         result = cl_lock_request(env, io, lock);
124                         if (result < 0)
125                                 return result;
126
127                         if (!agl) {
128                                 ll_merge_attr(env, inode);
129                                 if (i_size_read(inode) > 0 &&
130                                     inode->i_blocks == 0) {
131                                         /*
132                                          * LU-417: Add dirty pages block count
133                                          * lest i_blocks reports 0, some "cp" or
134                                          * "tar" may think it's a completely
135                                          * sparse file and skip it.
136                                          */
137                                         inode->i_blocks = dirty_cnt(inode);
138                                 }
139                         }
140                         cl_lock_release(env, lock);
141                 } else {
142                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
143                         ll_merge_attr(env, inode);
144                 }
145         }
146
147         return result;
148 }
149
150 static int cl_io_get(struct inode *inode, struct lu_env **envout,
151                      struct cl_io **ioout, int *refcheck)
152 {
153         struct lu_env     *env;
154         struct cl_io       *io;
155         struct ll_inode_info    *lli = ll_i2info(inode);
156         struct cl_object       *clob = lli->lli_clob;
157         int result;
158
159         if (S_ISREG(inode->i_mode)) {
160                 env = cl_env_get(refcheck);
161                 if (!IS_ERR(env)) {
162                         io = vvp_env_thread_io(env);
163                         io->ci_obj = clob;
164                         *envout = env;
165                         *ioout  = io;
166                         result = 1;
167                 } else {
168                         result = PTR_ERR(env);
169                 }
170         } else {
171                 result = 0;
172         }
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         result = cl_io_get(inode, &env, &io, &refcheck);
194         if (result > 0) {
195 again:
196                 io->ci_verify_layout = 1;
197                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
198                 if (result > 0)
199                         /*
200                          * nothing to do for this io. This currently happens
201                          * when stripe sub-object's are not yet created.
202                          */
203                         result = io->ci_result;
204                 else if (result == 0)
205                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
206                                                  agl);
207
208                 OBD_FAIL_TIMEOUT(OBD_FAIL_GLIMPSE_DELAY, 2);
209                 cl_io_fini(env, io);
210                 if (unlikely(io->ci_need_restart))
211                         goto again;
212                 cl_env_put(env, &refcheck);
213         }
214         return result;
215 }
216
217 int cl_local_size(struct inode *inode)
218 {
219         struct lu_env      *env = NULL;
220         struct cl_io        *io  = NULL;
221         struct cl_object        *clob;
222         int                   result;
223         int                   refcheck;
224
225         if (!ll_i2info(inode)->lli_has_smd)
226                 return 0;
227
228         result = cl_io_get(inode, &env, &io, &refcheck);
229         if (result <= 0)
230                 return result;
231
232         clob = io->ci_obj;
233         result = cl_io_init(env, io, CIT_MISC, clob);
234         if (result > 0) {
235                 result = io->ci_result;
236         } else if (result == 0) {
237                 struct cl_lock *lock = vvp_env_lock(env);
238
239                 lock->cll_descr = whole_file;
240                 lock->cll_descr.cld_enq_flags = CEF_PEEK;
241                 lock->cll_descr.cld_obj = clob;
242                 result = cl_lock_request(env, io, lock);
243                 if (result == 0) {
244                         ll_merge_attr(env, inode);
245                         cl_lock_release(env, lock);
246                 }
247         }
248         cl_io_fini(env, io);
249         cl_env_put(env, &refcheck);
250         return result;
251 }