Whamcloud - gitweb
- fixes in seq and fld after Mike's CODEINSP.
[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 struct seq_store_capsule {
52         struct lu_range ssc_space;
53         struct lu_range ssc_super;
54 };
55
56 enum {
57         SEQ_TXN_STORE_CREDITS = 20
58 };
59
60 /* this function implies that caller takes care about locking */
61 int
62 seq_store_write(struct lu_server_seq *seq,
63                 const struct lu_context *ctx)
64 {
65         struct dt_object *dt_obj = seq->seq_obj;
66         struct dt_device *dt_dev = seq->seq_dev;
67         struct seq_store_capsule capsule;
68         loff_t pos = 0;
69         struct txn_param txn;
70         struct thandle *th;
71         int rc;
72         ENTRY;
73
74         /* stub here, will fix it later */
75         txn.tp_credits = SEQ_TXN_STORE_CREDITS;
76
77         th = dt_dev->dd_ops->dt_trans_start(ctx, dt_dev, &txn);
78         if (!IS_ERR(th)) {
79                 rc = dt_obj->do_body_ops->dbo_write(ctx, dt_obj,
80                                                     (char *)&capsule,
81                                                     sizeof(capsule),
82                                                     &pos, th);
83                 if (rc == sizeof(capsule)) {
84                         rc = 0;
85                 } else if (rc >= 0) {
86                         rc = -EIO;
87                 }
88                 
89                 dt_dev->dd_ops->dt_trans_stop(ctx, th);
90         } else {
91                 rc = PTR_ERR(th);
92         }
93         
94         RETURN(rc);
95 }
96
97 /* this function implies that caller takes care about locking or locking is not
98  * needed (init time). */
99 int
100 seq_store_read(struct lu_server_seq *seq,
101                const struct lu_context *ctx)
102 {
103         struct dt_object *dt_obj = seq->seq_obj;
104         struct seq_store_capsule capsule;
105         loff_t pos = 0;
106         int rc;
107         ENTRY;
108
109         rc = dt_obj->do_body_ops->dbo_read(ctx, dt_obj,
110                                            (char *)&capsule,
111                                            sizeof(capsule), &pos);
112         if (rc == sizeof(capsule)) {
113                 seq->seq_space = capsule.ssc_space;
114                 seq->seq_super = capsule.ssc_super;
115                 rc = 0;
116         } else if (rc == 0) {
117                 rc = -ENODATA;
118         } else if (rc >= 0) {
119                 CERROR("read only %d bytes of %d\n",
120                        rc, sizeof(capsule));
121                 rc = -EIO;
122         }
123         
124         RETURN(rc);
125 }
126
127 int
128 seq_store_init(struct lu_server_seq *seq,
129                const struct lu_context *ctx)
130 {
131         struct dt_device *dt = seq->seq_dev;
132         struct dt_object *dt_obj;
133         int rc;
134         ENTRY;
135
136         LASSERT(seq->seq_service == NULL);
137
138         dt_obj = dt_store_open(ctx, dt, "seq", &seq->seq_fid);
139         if (!IS_ERR(dt_obj)) {
140                 seq->seq_obj = dt_obj;
141                 rc = 0;
142         } else {
143                 CERROR("cannot find \"seq\" obj %d\n",
144                        (int)PTR_ERR(dt_obj));
145                 rc = PTR_ERR(dt_obj);
146         }
147
148         RETURN(rc);
149 }
150
151 void
152 seq_store_fini(struct lu_server_seq *seq,
153                const struct lu_context *ctx)
154 {
155         ENTRY;
156         if (seq->seq_obj != NULL) {
157                 lu_object_put(ctx, &seq->seq_obj->do_lu);
158                 seq->seq_obj = NULL;
159         }
160         EXIT;
161 }
162 #endif