Whamcloud - gitweb
LU-228 kernel update [RHEL5 U6 2.6.18-238.9.1.el5]
[fs/lustre-release.git] / lustre / lclient / glimpse.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifdef __KERNEL__
49 # include <lustre_dlm.h>
50 # include <lustre_lite.h>
51 # include <lustre_mdc.h>
52 # include <linux/pagemap.h>
53 # include <linux/file.h>
54 #else
55 #include <stdlib.h>
56 #include <string.h>
57 #include <assert.h>
58 #include <time.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/queue.h>
62 #include <fcntl.h>
63 #include <liblustre.h>
64 #endif
65
66 #include "cl_object.h"
67 #include "lclient.h"
68 #ifdef __KERNEL__
69 # include "../llite/llite_internal.h"
70 #else
71 # include "../liblustre/llite_lib.h"
72 #endif
73
74 static const struct cl_lock_descr whole_file = {
75         .cld_start = 0,
76         .cld_end   = CL_PAGE_EOF,
77         .cld_mode  = CLM_READ
78 };
79
80 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
81                     struct inode *inode, struct cl_object *clob)
82 {
83         struct cl_lock_descr *descr = &ccc_env_info(env)->cti_descr;
84         struct cl_inode_info *lli   = cl_i2info(inode);
85         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
86         struct ccc_io        *cio   = ccc_env_io(env);
87         struct cl_lock       *lock;
88         int result;
89
90         ENTRY;
91         result = 0;
92         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
93                 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
94                 if (lli->lli_smd) {
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                         *descr = whole_file;
109                         descr->cld_obj   = clob;
110                         descr->cld_mode  = CLM_PHANTOM;
111                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
112                         cio->cui_glimpse = 1;
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                         lock = cl_lock_request(env, io, descr, "glimpse",
122                                                cfs_current());
123                         cio->cui_glimpse = 0;
124                         if (!IS_ERR(lock)) {
125                                 result = cl_wait(env, lock);
126                                 if (result == 0) {
127                                         cl_merge_lvb(inode);
128                                         cl_unuse(env, lock);
129                                 }
130                                 cl_lock_release(env, lock,
131                                                 "glimpse", cfs_current());
132                         } else
133                                 result = PTR_ERR(lock);
134                 } else
135                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
136         }
137
138         RETURN(result);
139 }
140
141 static int cl_io_get(struct inode *inode, struct lu_env **envout,
142                      struct cl_io **ioout, int *refcheck)
143 {
144         struct lu_env          *env;
145         struct cl_io           *io;
146         struct cl_inode_info   *lli = cl_i2info(inode);
147         struct cl_object       *clob = lli->lli_clob;
148         int result;
149
150         if (S_ISREG(cl_inode_mode(inode))) {
151                 env = cl_env_get(refcheck);
152                 if (!IS_ERR(env)) {
153                         io = ccc_env_thread_io(env);
154                         io->ci_obj = clob;
155                         *envout = env;
156                         *ioout  = io;
157                         result = +1;
158                 } else
159                         result = PTR_ERR(env);
160         } else
161                 result = 0;
162         return result;
163 }
164
165 int cl_glimpse_size(struct inode *inode)
166 {
167         /*
168          * We don't need ast_flags argument to cl_glimpse_size(), because
169          * osc_lock_enqueue() takes care of the possible deadlock that said
170          * argument was introduced to avoid.
171          */
172         /*
173          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
174          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
175          * blocking anyway.
176          */
177         struct lu_env          *env = NULL;
178         struct cl_io           *io  = NULL;
179         int                     result;
180         int                     refcheck;
181
182         ENTRY;
183
184         result = cl_io_get(inode, &env, &io, &refcheck);
185         if (result > 0) {
186                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
187                 if (result > 0)
188                         /*
189                          * nothing to do for this io. This currently happens
190                          * when stripe sub-object's are not yet created.
191                          */
192                         result = io->ci_result;
193                 else if (result == 0)
194                         result = cl_glimpse_lock(env, io, inode, io->ci_obj);
195                 cl_io_fini(env, io);
196                 cl_env_put(env, &refcheck);
197         }
198         RETURN(result);
199 }
200
201 int cl_local_size(struct inode *inode)
202 {
203         struct lu_env           *env = NULL;
204         struct cl_io            *io  = NULL;
205         struct ccc_thread_info  *cti;
206         struct cl_object        *clob;
207         struct cl_lock_descr    *descr;
208         struct cl_lock          *lock;
209         int                      result;
210         int                      refcheck;
211
212         ENTRY;
213
214         if (!cl_i2info(inode)->lli_smd)
215                 RETURN(0);
216
217         result = cl_io_get(inode, &env, &io, &refcheck);
218         if (result <= 0)
219                 RETURN(result);
220
221         clob = io->ci_obj;
222         result = cl_io_init(env, io, CIT_MISC, clob);
223         if (result > 0)
224                 result = io->ci_result;
225         else if (result == 0) {
226                 cti = ccc_env_info(env);
227                 descr = &cti->cti_descr;
228
229                 *descr = whole_file;
230                 descr->cld_obj = clob;
231                 lock = cl_lock_peek(env, io, descr, "localsize", cfs_current());
232                 if (lock != NULL) {
233                         cl_merge_lvb(inode);
234                         cl_unuse(env, lock);
235                         cl_lock_release(env, lock, "localsize", cfs_current());
236                         result = 0;
237                 } else
238                         result = -ENODATA;
239         }
240         cl_io_fini(env, io);
241         cl_env_put(env, &refcheck);
242         RETURN(result);
243 }
244