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