Whamcloud - gitweb
1527635ab270ca5ae3103ebc7bf47e6c126775e7
[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 2008 Sun Microsystems, Inc.  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 <sysio.h>
64 # ifdef HAVE_XTIO_H
65 #  include <xtio.h>
66 # endif
67 # include <fs.h>
68 # include <mount.h>
69 # include <inode.h>
70 # ifdef HAVE_FILE_H
71 #  include <file.h>
72 # endif
73 # include <liblustre.h>
74 #endif
75
76 #include "cl_object.h"
77 #include "lclient.h"
78 #ifdef __KERNEL__
79 # include "../llite/llite_internal.h"
80 #else
81 # include "../liblustre/llite_lib.h"
82 #endif
83
84 static const struct cl_lock_descr whole_file = {
85         .cld_start = 0,
86         .cld_end   = CL_PAGE_EOF,
87         .cld_mode  = CLM_READ
88 };
89
90 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
91                     struct inode *inode, struct cl_object *clob)
92 {
93         struct cl_lock_descr *descr = &ccc_env_info(env)->cti_descr;
94         struct cl_inode_info *lli   = cl_i2info(inode);
95         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
96         struct ccc_io        *cio   = ccc_env_io(env);
97         struct cl_lock       *lock;
98         int result;
99
100         ENTRY;
101         result = 0;
102         if (!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)) {
103                 CDEBUG(D_DLMTRACE, "Glimpsing inode "DFID"\n", PFID(fid));
104                 if (lli->lli_smd) {
105                         /* NOTE: this looks like DLM lock request, but it may
106                          *       not be one. Due to CEF_ASYNC flag (translated
107                          *       to LDLM_FL_HAS_INTENT by osc), this is
108                          *       glimpse request, that won't revoke any
109                          *       conflicting DLM locks held. Instead,
110                          *       ll_glimpse_callback() will be called on each
111                          *       client holding a DLM lock against this file,
112                          *       and resulting size will be returned for each
113                          *       stripe. DLM lock on [0, EOF] is acquired only
114                          *       if there were no conflicting locks. If there
115                          *       were conflicting locks, enqueuing or waiting
116                          *       fails with -ENAVAIL, but valid inode
117                          *       attributes are returned anyway. */
118                         *descr = whole_file;
119                         descr->cld_obj   = clob;
120                         descr->cld_mode  = CLM_PHANTOM;
121                         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
122                         cio->cui_glimpse = 1;
123                         /*
124                          * CEF_ASYNC is used because glimpse sub-locks cannot
125                          * deadlock (because they never conflict with other
126                          * locks) and, hence, can be enqueued out-of-order.
127                          *
128                          * CEF_MUST protects glimpse lock from conversion into
129                          * a lockless mode.
130                          */
131                         lock = cl_lock_request(env, io, descr, "glimpse",
132                                                cfs_current());
133                         cio->cui_glimpse = 0;
134                         if (!IS_ERR(lock)) {
135                                 result = cl_wait(env, lock);
136                                 if (result == 0) {
137                                         cl_merge_lvb(inode);
138                                         cl_unuse(env, lock);
139                                 }
140                                 cl_lock_release(env, lock,
141                                                 "glimpse", cfs_current());
142                         } else
143                                 result = PTR_ERR(lock);
144                 } else
145                         CDEBUG(D_DLMTRACE, "No objects for inode\n");
146         }
147
148         RETURN(result);
149 }
150
151 static int cl_io_get(struct inode *inode, struct lu_env **envout,
152                      struct cl_io **ioout, int *refcheck)
153 {
154         struct ccc_thread_info *info;
155         struct lu_env          *env;
156         struct cl_io           *io;
157         struct cl_inode_info   *lli = cl_i2info(inode);
158         struct cl_object       *clob = lli->lli_clob;
159         int result;
160
161         if (S_ISREG(cl_inode_mode(inode))) {
162                 env = cl_env_get(refcheck);
163                 if (!IS_ERR(env)) {
164                         info = ccc_env_info(env);
165                         io = &info->cti_io;
166                         io->ci_obj = clob;
167                         *envout = env;
168                         *ioout  = io;
169                         result = +1;
170                 } else
171                         result = PTR_ERR(env);
172         } else
173                 result = 0;
174         return result;
175 }
176
177 int cl_glimpse_size(struct inode *inode)
178 {
179         /*
180          * We don't need ast_flags argument to cl_glimpse_size(), because
181          * osc_lock_enqueue() takes care of the possible deadlock that said
182          * argument was introduced to avoid.
183          */
184         /*
185          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
186          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
187          * blocking anyway.
188          */
189         struct lu_env          *env;
190         struct cl_io           *io;
191         int                     result;
192         int                     refcheck;
193
194         ENTRY;
195
196         result = cl_io_get(inode, &env, &io, &refcheck);
197         if (result > 0) {
198                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
199                 if (result > 0)
200                         /*
201                          * nothing to do for this io. This currently happens
202                          * when stripe sub-object's are not yet created.
203                          */
204                         result = io->ci_result;
205                 else if (result == 0)
206                         result = cl_glimpse_lock(env, io, inode, io->ci_obj);
207                 cl_io_fini(env, io);
208                 cl_env_put(env, &refcheck);
209         }
210         RETURN(result);
211 }
212
213 int cl_local_size(struct inode *inode)
214 {
215         struct lu_env           *env;
216         struct cl_io            *io;
217         struct ccc_thread_info  *cti;
218         struct cl_object        *clob;
219         struct cl_lock_descr    *descr;
220         struct cl_lock          *lock;
221         int                      result;
222         int                      refcheck;
223
224         ENTRY;
225
226         /*
227          * XXX layering violation.
228          */
229         if (cl_i2info(inode)->lli_smd->lsm_stripe_count == 0)
230                 RETURN(0);
231
232         result = cl_io_get(inode, &env, &io, &refcheck);
233         if (result <= 0)
234                 RETURN(result);
235
236         clob = io->ci_obj;
237         result = cl_io_init(env, io, CIT_MISC, clob);
238         if (result > 0)
239                 result = io->ci_result;
240         else if (result == 0) {
241                 cti = ccc_env_info(env);
242                 descr = &cti->cti_descr;
243
244                 *descr = whole_file;
245                 descr->cld_obj = clob;
246                 lock = cl_lock_peek(env, io, descr, "localsize", cfs_current());
247                 if (lock != NULL) {
248                         cl_merge_lvb(inode);
249                         cl_unuse(env, lock);
250                         cl_lock_release(env, lock, "localsize", cfs_current());
251                         result = 0;
252                 } else
253                         result = -ENODATA;
254         }
255         cl_io_fini(env, io);
256         cl_env_put(env, &refcheck);
257         RETURN(result);
258 }
259