Whamcloud - gitweb
7fe5d7e882287c3a09083684c2b130ff09a2a4b5
[fs/lustre-release.git] / lustre / fld / fld_handler.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fld/fld_handler.c
37  *
38  * FLD (Fids Location Database)
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  * Author: WangDi <wangdi@clusterfs.com>
42  * Author: Pravin Shelar <pravin.shelar@sun.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_FLD
46
47 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 # include <linux/module.h>
50 # include <linux/jbd.h>
51 # include <asm/div64.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 # include <libcfs/list.h>
55 #endif
56
57 #include <obd.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
62
63 #include <md_object.h>
64 #include <lustre_fid.h>
65 #include <lustre_req_layout.h>
66 #include "fld_internal.h"
67 #include <lustre_fid.h>
68
69 #ifdef __KERNEL__
70
71 /* context key constructor/destructor: fld_key_init, fld_key_fini */
72 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
73
74 /* context key: fld_thread_key */
75 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
76
77 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
78
79 static int __init fld_mod_init(void)
80 {
81         fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
82                                              proc_lustre_root,
83                                              NULL, NULL);
84         if (IS_ERR(fld_type_proc_dir))
85                 return PTR_ERR(fld_type_proc_dir);
86
87         LU_CONTEXT_KEY_INIT(&fld_thread_key);
88         lu_context_key_register(&fld_thread_key);
89         return 0;
90 }
91
92 static void __exit fld_mod_exit(void)
93 {
94         lu_context_key_degister(&fld_thread_key);
95         if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
96                 lprocfs_remove(&fld_type_proc_dir);
97                 fld_type_proc_dir = NULL;
98         }
99 }
100
101 int fld_declare_server_create(const struct lu_env *env,
102                               struct lu_server_fld *fld,
103                               struct lu_seq_range *range,
104                               struct thandle *th)
105 {
106         int rc;
107
108         rc = fld_declare_index_create(env, fld, range, th);
109         RETURN(rc);
110 }
111 EXPORT_SYMBOL(fld_declare_server_create);
112
113 /**
114  * Insert FLD index entry and update FLD cache.
115  *
116  * This function is called from the sequence allocator when a super-sequence
117  * is granted to a server.
118  */
119 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
120                       struct lu_seq_range *range, struct thandle *th)
121 {
122         int rc;
123
124         mutex_lock(&fld->lsf_lock);
125         rc = fld_index_create(env, fld, range, th);
126         mutex_unlock(&fld->lsf_lock);
127
128         RETURN(rc);
129 }
130 EXPORT_SYMBOL(fld_server_create);
131
132 /**
133  *  Lookup mds by seq, returns a range for given seq.
134  *
135  *  If that entry is not cached in fld cache, request is sent to super
136  *  sequence controller node (MDT0). All other MDT[1...N] and client
137  *  cache fld entries, but this cache is not persistent.
138  */
139
140 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
141                       seqno_t seq, struct lu_seq_range *range)
142 {
143         struct lu_seq_range *erange;
144         struct fld_thread_info *info;
145         int rc;
146         ENTRY;
147
148         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
149         LASSERT(info != NULL);
150         erange = &info->fti_lrange;
151
152         /* Lookup it in the cache. */
153         rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
154         if (rc == 0) {
155                 if (unlikely(erange->lsr_flags != range->lsr_flags) &&
156                     range->lsr_flags != -1) {
157                         CERROR("FLD cache found a range "DRANGE" doesn't "
158                                "match the requested flag %x\n",
159                                PRANGE(erange), range->lsr_flags);
160                         RETURN(-EIO);
161                 }
162                 *range = *erange;
163                 RETURN(0);
164         }
165
166         if (fld->lsf_obj) {
167                 /* On server side, all entries should be in cache.
168                  * If we can not find it in cache, just return error */
169                 CERROR("%s: Can not found the seq "LPX64"\n",
170                         fld->lsf_name, seq);
171                 RETURN(-EIO);
172         } else {
173                 LASSERT(fld->lsf_control_exp);
174                 /* send request to mdt0 i.e. super seq. controller.
175                  * This is temporary solution, long term solution is fld
176                  * replication on all mdt servers.
177                  */
178                 range->lsr_start = seq;
179                 rc = fld_client_rpc(fld->lsf_control_exp,
180                                     range, FLD_LOOKUP);
181                 if (rc == 0)
182                         fld_cache_insert(fld->lsf_cache, range);
183         }
184         RETURN(rc);
185 }
186 EXPORT_SYMBOL(fld_server_lookup);
187
188 /**
189  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
190  * if entry is not found in cache we need to forward lookup request to MDT0
191  */
192
193 static int fld_server_handle(struct lu_server_fld *fld,
194                              const struct lu_env *env,
195                              __u32 opc, struct lu_seq_range *range,
196                              struct fld_thread_info *info)
197 {
198         int rc;
199         ENTRY;
200
201         switch (opc) {
202         case FLD_LOOKUP:
203                 rc = fld_server_lookup(env, fld, range->lsr_start, range);
204                 break;
205         default:
206                 rc = -EINVAL;
207                 break;
208         }
209
210         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
211                DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
212
213         RETURN(rc);
214
215 }
216
217 static int fld_req_handle(struct ptlrpc_request *req,
218                           struct fld_thread_info *info)
219 {
220         struct obd_export *exp = req->rq_export;
221         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
222         struct lu_seq_range *in;
223         struct lu_seq_range *out;
224         int rc;
225         __u32 *opc;
226         ENTRY;
227
228         rc = req_capsule_server_pack(info->fti_pill);
229         if (rc)
230                 RETURN(err_serious(rc));
231
232         opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
233         if (opc != NULL) {
234                 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
235                 if (in == NULL)
236                         RETURN(err_serious(-EPROTO));
237                 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
238                 if (out == NULL)
239                         RETURN(err_serious(-EPROTO));
240                 *out = *in;
241
242                 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
243                  * Set it as 'LU_SEQ_RANGE_MDT' by default. */
244                 if (!(exp_connect_flags(exp) & OBD_CONNECT_64BITHASH) &&
245                     !(exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
246                     !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT) &&
247                     !exp->exp_libclient)
248                         out->lsr_flags = LU_SEQ_RANGE_MDT;
249
250                 rc = fld_server_handle(lu_site2seq(site)->ss_server_fld,
251                                        req->rq_svc_thread->t_env,
252                                        *opc, out, info);
253         } else
254                 rc = err_serious(-EPROTO);
255
256         RETURN(rc);
257 }
258
259 static void fld_thread_info_init(struct ptlrpc_request *req,
260                                  struct fld_thread_info *info)
261 {
262         info->fti_pill = &req->rq_pill;
263         /* Init request capsule. */
264         req_capsule_init(info->fti_pill, req, RCL_SERVER);
265         req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
266 }
267
268 static void fld_thread_info_fini(struct fld_thread_info *info)
269 {
270         req_capsule_fini(info->fti_pill);
271 }
272
273 static int fld_handle(struct ptlrpc_request *req)
274 {
275         struct fld_thread_info *info;
276         const struct lu_env *env;
277         int rc;
278
279         env = req->rq_svc_thread->t_env;
280         LASSERT(env != NULL);
281
282         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
283         LASSERT(info != NULL);
284
285         fld_thread_info_init(req, info);
286         rc = fld_req_handle(req, info);
287         fld_thread_info_fini(info);
288
289         return rc;
290 }
291
292 /*
293  * Entry point for handling FLD RPCs called from MDT.
294  */
295 int fld_query(struct com_thread_info *info)
296 {
297         return fld_handle(info->cti_pill->rc_req);
298 }
299 EXPORT_SYMBOL(fld_query);
300
301 /*
302  * Returns true, if fid is local to this server node.
303  *
304  * WARNING: this function is *not* guaranteed to return false if fid is
305  * remote: it makes an educated conservative guess only.
306  *
307  * fid_is_local() is supposed to be used in assertion checks only.
308  */
309 int fid_is_local(const struct lu_env *env,
310                  struct lu_site *site, const struct lu_fid *fid)
311 {
312         int result;
313         struct seq_server_site *ss_site;
314         struct lu_seq_range *range;
315         struct fld_thread_info *info;
316         ENTRY;
317
318         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
319         range = &info->fti_lrange;
320
321         result = 1; /* conservatively assume fid is local */
322         ss_site = lu_site2seq(site);
323         if (ss_site->ss_client_fld != NULL) {
324                 int rc;
325
326                 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
327                                       fid_seq(fid), range);
328                 if (rc == 0)
329                         result = (range->lsr_index == ss_site->ss_node_id);
330         }
331         return result;
332 }
333 EXPORT_SYMBOL(fid_is_local);
334
335 static void fld_server_proc_fini(struct lu_server_fld *fld);
336
337 #ifdef LPROCFS
338 static int fld_server_proc_init(struct lu_server_fld *fld)
339 {
340         int rc = 0;
341         ENTRY;
342
343         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
344                                              fld_type_proc_dir,
345                                              fld_server_proc_list, fld);
346         if (IS_ERR(fld->lsf_proc_dir)) {
347                 rc = PTR_ERR(fld->lsf_proc_dir);
348                 RETURN(rc);
349         }
350
351         rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
352                                 &fld_proc_seq_fops, fld);
353         if (rc) {
354                 lprocfs_remove(&fld->lsf_proc_dir);
355                 fld->lsf_proc_dir = NULL;
356         }
357
358         RETURN(rc);
359 }
360
361 static void fld_server_proc_fini(struct lu_server_fld *fld)
362 {
363         ENTRY;
364         if (fld->lsf_proc_dir != NULL) {
365                 if (!IS_ERR(fld->lsf_proc_dir))
366                         lprocfs_remove(&fld->lsf_proc_dir);
367                 fld->lsf_proc_dir = NULL;
368         }
369         EXIT;
370 }
371 #else
372 static int fld_server_proc_init(struct lu_server_fld *fld)
373 {
374         return 0;
375 }
376
377 static void fld_server_proc_fini(struct lu_server_fld *fld)
378 {
379         return;
380 }
381 #endif
382
383 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
384                     struct dt_device *dt, const char *prefix, int mds_node_id,
385                     __u32 lsr_flags)
386 {
387         int cache_size, cache_threshold;
388         struct lu_seq_range range;
389         int rc;
390         ENTRY;
391
392         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
393                  "srv-%s", prefix);
394
395         cache_size = FLD_SERVER_CACHE_SIZE /
396                 sizeof(struct fld_cache_entry);
397
398         cache_threshold = cache_size *
399                 FLD_SERVER_CACHE_THRESHOLD / 100;
400
401         mutex_init(&fld->lsf_lock);
402         fld->lsf_cache = fld_cache_init(fld->lsf_name,
403                                         cache_size, cache_threshold);
404         if (IS_ERR(fld->lsf_cache)) {
405                 rc = PTR_ERR(fld->lsf_cache);
406                 fld->lsf_cache = NULL;
407                 GOTO(out, rc);
408         }
409
410         if (!mds_node_id && lsr_flags == LU_SEQ_RANGE_MDT) {
411                 rc = fld_index_init(env, fld, dt);
412                 if (rc)
413                         GOTO(out, rc);
414         } else
415                 fld->lsf_obj = NULL;
416
417         rc = fld_server_proc_init(fld);
418         if (rc)
419                 GOTO(out, rc);
420
421         fld->lsf_control_exp = NULL;
422
423         if (lsr_flags == LU_SEQ_RANGE_MDT) {
424                 /* Insert reserved sequence of "ROOT" and ".lustre"
425                  * into fld cache. */
426                 range.lsr_start = FID_SEQ_LOCAL_FILE;
427                 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
428                 range.lsr_index = 0;
429                 range.lsr_flags = lsr_flags;
430                 fld_cache_insert(fld->lsf_cache, &range);
431         }
432         EXIT;
433 out:
434         if (rc)
435                 fld_server_fini(env, fld);
436         return rc;
437 }
438 EXPORT_SYMBOL(fld_server_init);
439
440 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
441 {
442         ENTRY;
443
444         fld_server_proc_fini(fld);
445         fld_index_fini(env, fld);
446
447         if (fld->lsf_cache != NULL) {
448                 if (!IS_ERR(fld->lsf_cache))
449                         fld_cache_fini(fld->lsf_cache);
450                 fld->lsf_cache = NULL;
451         }
452
453         EXIT;
454 }
455 EXPORT_SYMBOL(fld_server_fini);
456
457 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
458 MODULE_DESCRIPTION("Lustre FLD");
459 MODULE_LICENSE("GPL");
460
461 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);
462 #endif