Whamcloud - gitweb
00ec2e8221dd26e8270595af92d9a8d9b1600039
[fs/lustre-release.git] / lustre / fid / fid_store.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/fid/fid_store.c
5  *  Lustre Sequence Manager
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Yury Umanets <umka@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_FID
33
34 #ifdef __KERNEL__
35 # include <libcfs/libcfs.h>
36 # include <linux/module.h>
37 #else /* __KERNEL__ */
38 # include <liblustre.h>
39 #endif
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <dt_object.h>
44 #include <md_object.h>
45 #include <obd_support.h>
46 #include <lustre_req_layout.h>
47 #include <lustre_fid.h>
48 #include "fid_internal.h"
49
50 #ifdef __KERNEL__
51 enum {
52         SEQ_TXN_STORE_CREDITS = 20
53 };
54
55 /* this function implies that caller takes care about locking */
56 int seq_store_write(struct lu_server_seq *seq,
57                     const struct lu_context *ctx)
58 {
59         struct dt_object *dt_obj = seq->lss_obj;
60         struct seq_thread_info *info;
61         struct dt_device *dt_dev;
62         struct thandle *th;
63         loff_t pos = 0;
64         int rc;
65         ENTRY;
66
67         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
68         info = lu_context_key_get(ctx, &seq_thread_key);
69         LASSERT(info != NULL);
70
71         /* stub here, will fix it later */
72         info->sti_txn.tp_credits = SEQ_TXN_STORE_CREDITS;
73
74         th = dt_dev->dd_ops->dt_trans_start(ctx, dt_dev, &info->sti_txn);
75         if (!IS_ERR(th)) {
76                 /* store ranges in le format */
77                 range_to_le(&info->sti_record.ssr_space, &seq->lss_space);
78                 range_to_le(&info->sti_record.ssr_super, &seq->lss_super);
79
80                 rc = dt_obj->do_body_ops->dbo_write(ctx, dt_obj,
81                                                     (char *)&info->sti_record,
82                                                     sizeof(info->sti_record),
83                                                     &pos, th);
84                 if (rc == sizeof(info->sti_record)) {
85                         rc = 0;
86                 } else if (rc >= 0) {
87                         rc = -EIO;
88                 }
89                 
90                 dt_dev->dd_ops->dt_trans_stop(ctx, th);
91         } else {
92                 rc = PTR_ERR(th);
93         }
94         
95         RETURN(rc);
96 }
97
98 /* this function implies that caller takes care about locking or locking is not
99  * needed (init time). */
100 int seq_store_read(struct lu_server_seq *seq,
101                    const struct lu_context *ctx)
102 {
103         struct dt_object *dt_obj = seq->lss_obj;
104         struct seq_thread_info *info;
105         loff_t pos = 0;
106         int rc;
107         ENTRY;
108
109         info = lu_context_key_get(ctx, &seq_thread_key);
110         LASSERT(info != NULL);
111
112         rc = dt_obj->do_body_ops->dbo_read(ctx, dt_obj,
113                                            (char *)&info->sti_record,
114                                            sizeof(info->sti_record), &pos);
115         if (rc == sizeof(info->sti_record)) {
116                 seq->lss_space = info->sti_record.ssr_space;
117                 lustre_swab_lu_range(&seq->lss_space);
118                 
119                 seq->lss_super = info->sti_record.ssr_super;
120                 lustre_swab_lu_range(&seq->lss_super);
121                 rc = 0;
122         } else if (rc == 0) {
123                 rc = -ENODATA;
124         } else if (rc >= 0) {
125                 CERROR("read only %d bytes of %d\n",
126                        rc, sizeof(info->sti_record));
127                 rc = -EIO;
128         }
129         
130         RETURN(rc);
131 }
132
133 int seq_store_init(struct lu_server_seq *seq,
134                    const struct lu_context *ctx,
135                    struct dt_device *dt)
136 {
137         struct dt_object *dt_obj;
138         struct lu_fid fid;
139         int rc;
140         ENTRY;
141
142         LASSERT(seq->lss_md_service == NULL);
143         LASSERT(seq->lss_dt_service == NULL);
144
145         dt_obj = dt_store_open(ctx, dt, "seq", &fid);
146         if (!IS_ERR(dt_obj)) {
147                 seq->lss_obj = dt_obj;
148                 rc = 0;
149         } else {
150                 CERROR("cannot find \"seq\" obj %d\n",
151                        (int)PTR_ERR(dt_obj));
152                 rc = PTR_ERR(dt_obj);
153         }
154
155         RETURN(rc);
156 }
157
158 void seq_store_fini(struct lu_server_seq *seq,
159                     const struct lu_context *ctx)
160 {
161         ENTRY;
162
163         if (seq->lss_obj != NULL) {
164                 if (!IS_ERR(seq->lss_obj))
165                         lu_object_put(ctx, &seq->lss_obj->do_lu);
166                 seq->lss_obj = NULL;
167         }
168         
169         EXIT;
170 }
171 #endif