Whamcloud - gitweb
LU-488 ptlrpc_connection_put() LASSERT(!cfs_hlist_unhashed(&conn->c_hash))
[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  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * glimpse code shared between vvp and liblustre (and other Lustre clients in
40  * the future).
41  *
42  *   Author: Nikita Danilov <nikita.danilov@sun.com>
43  *   Author: Oleg Drokin <oleg.drokin@sun.com>
44  */
45
46 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <obd.h>
50
51 #ifdef __KERNEL__
52 # include <lustre_dlm.h>
53 # include <lustre_lite.h>
54 # include <lustre_mdc.h>
55 # include <linux/pagemap.h>
56 # include <linux/file.h>
57 #else
58 #include <stdlib.h>
59 #include <string.h>
60 #include <assert.h>
61 #include <time.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/queue.h>
65 #include <fcntl.h>
66 #include <liblustre.h>
67 #endif
68
69 #include "cl_object.h"
70 #include "lclient.h"
71 #ifdef __KERNEL__
72 # include "../llite/llite_internal.h"
73 #else
74 # include "../liblustre/llite_lib.h"
75 #endif
76
77 static const struct cl_lock_descr whole_file = {
78         .cld_start = 0,
79         .cld_end   = CL_PAGE_EOF,
80         .cld_mode  = CLM_READ
81 };
82
83 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
84                     struct inode *inode, struct cl_object *clob)
85 {
86         struct cl_lock_descr *descr = &ccc_env_info(env)->cti_descr;
87         struct cl_inode_info *lli   = cl_i2info(inode);
88         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
89         struct ccc_io        *cio   = ccc_env_io(env);
90         struct cl_lock       *lock;
91         int result;
92
93         ENTRY;
94         result = 0;
95         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
96                 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
97                 if (lli->lli_smd) {
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_PHANTOM;
114                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
115                         cio->cui_glimpse = 1;
116                         /*
117                          * CEF_ASYNC is used because glimpse sub-locks cannot
118                          * deadlock (because they never conflict with other
119                          * locks) and, hence, can be enqueued out-of-order.
120                          *
121                          * CEF_MUST protects glimpse lock from conversion into
122                          * a lockless mode.
123                          */
124                         lock = cl_lock_request(env, io, descr, "glimpse",
125                                                cfs_current());
126                         cio->cui_glimpse = 0;
127                         if (!IS_ERR(lock)) {
128                                 result = cl_wait(env, lock);
129                                 if (result == 0) {
130                                         cl_merge_lvb(inode);
131                                         cl_unuse(env, lock);
132                                 }
133                                 cl_lock_release(env, lock,
134                                                 "glimpse", cfs_current());
135                         } else
136                                 result = PTR_ERR(lock);
137                 } else
138                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
139         }
140
141         RETURN(result);
142 }
143
144 static int cl_io_get(struct inode *inode, struct lu_env **envout,
145                      struct cl_io **ioout, int *refcheck)
146 {
147         struct lu_env          *env;
148         struct cl_io           *io;
149         struct cl_inode_info   *lli = cl_i2info(inode);
150         struct cl_object       *clob = lli->lli_clob;
151         int result;
152
153         if (S_ISREG(cl_inode_mode(inode))) {
154                 env = cl_env_get(refcheck);
155                 if (!IS_ERR(env)) {
156                         io = ccc_env_thread_io(env);
157                         io->ci_obj = clob;
158                         *envout = env;
159                         *ioout  = io;
160                         result = +1;
161                 } else
162                         result = PTR_ERR(env);
163         } else
164                 result = 0;
165         return result;
166 }
167
168 int cl_glimpse_size(struct inode *inode)
169 {
170         /*
171          * We don't need ast_flags argument to cl_glimpse_size(), because
172          * osc_lock_enqueue() takes care of the possible deadlock that said
173          * argument was introduced to avoid.
174          */
175         /*
176          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
177          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
178          * blocking anyway.
179          */
180         struct lu_env          *env = NULL;
181         struct cl_io           *io  = NULL;
182         int                     result;
183         int                     refcheck;
184
185         ENTRY;
186
187         result = cl_io_get(inode, &env, &io, &refcheck);
188         if (result > 0) {
189                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
190                 if (result > 0)
191                         /*
192                          * nothing to do for this io. This currently happens
193                          * when stripe sub-object's are not yet created.
194                          */
195                         result = io->ci_result;
196                 else if (result == 0)
197                         result = cl_glimpse_lock(env, io, inode, io->ci_obj);
198                 cl_io_fini(env, io);
199                 cl_env_put(env, &refcheck);
200         }
201         RETURN(result);
202 }
203
204 int cl_local_size(struct inode *inode)
205 {
206         struct lu_env           *env = NULL;
207         struct cl_io            *io  = NULL;
208         struct ccc_thread_info  *cti;
209         struct cl_object        *clob;
210         struct cl_lock_descr    *descr;
211         struct cl_lock          *lock;
212         int                      result;
213         int                      refcheck;
214
215         ENTRY;
216
217         if (!cl_i2info(inode)->lli_smd)
218                 RETURN(0);
219
220         result = cl_io_get(inode, &env, &io, &refcheck);
221         if (result <= 0)
222                 RETURN(result);
223
224         clob = io->ci_obj;
225         result = cl_io_init(env, io, CIT_MISC, clob);
226         if (result > 0)
227                 result = io->ci_result;
228         else if (result == 0) {
229                 cti = ccc_env_info(env);
230                 descr = &cti->cti_descr;
231
232                 *descr = whole_file;
233                 descr->cld_obj = clob;
234                 lock = cl_lock_peek(env, io, descr, "localsize", cfs_current());
235                 if (lock != NULL) {
236                         cl_merge_lvb(inode);
237                         cl_unuse(env, lock);
238                         cl_lock_release(env, lock, "localsize", cfs_current());
239                         result = 0;
240                 } else
241                         result = -ENODATA;
242         }
243         cl_io_fini(env, io);
244         cl_env_put(env, &refcheck);
245         RETURN(result);
246 }
247