Whamcloud - gitweb
- documentation update for MDS recovery
[fs/lustre-release.git] / lustre / llite / super.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
10  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11  * Copryright (C) 1999 Seagate Technology Inc.
12  * Copryright (C) 2001 Mountain View Data, Inc.
13  * Copryright (C) 2002 Cluster File Systems, Inc.
14  *
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19
20 #define DEBUG_SUBSYSTEM S_LLITE
21
22 #include <linux/lustre_lite.h>
23 #include <linux/lustre_ha.h>
24
25 kmem_cache_t *ll_file_data_slab;
26 extern struct address_space_operations ll_aops;
27 extern struct address_space_operations ll_dir_aops;
28 struct super_operations ll_super_operations;
29
30 extern void ll_recover(struct ptlrpc_client *);
31 extern int ll_commitcbd_setup(struct ll_sb_info *);
32 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
33
34 static char *ll_read_opt(const char *opt, char *data)
35 {
36         char *value;
37         char *retval;
38         ENTRY;
39
40         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
41         if ( strncmp(opt, data, strlen(opt)) )
42                 RETURN(NULL);
43         if ( (value = strchr(data, '=')) == NULL )
44                 RETURN(NULL);
45
46         value++;
47         OBD_ALLOC(retval, strlen(value) + 1);
48         if ( !retval ) {
49                 CERROR("out of memory!\n");
50                 RETURN(NULL);
51         }
52         
53         memcpy(retval, value, strlen(value)+1);
54         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
55         RETURN(retval);
56 }
57
58 static void ll_options(char *options, char **dev, char **vers)
59 {
60         char *this_char;
61         ENTRY;
62
63         if (!options) {
64                 EXIT;
65                 return;
66         }
67
68         for (this_char = strtok (options, ",");
69              this_char != NULL;
70              this_char = strtok (NULL, ",")) {
71                 CDEBUG(D_INFO, "this_char %s\n", this_char);
72                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
73                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
74                         continue;
75         }
76         EXIT;
77 }
78
79 static struct super_block * ll_read_super(struct super_block *sb,
80                                           void *data, int silent)
81 {
82         struct inode *root = 0;
83         struct ll_sb_info *sbi;
84         char *device = NULL;
85         char *version = NULL;
86         int devno;
87         int err;
88         struct ll_fid rootfid;
89         __u64 last_committed, last_rcvd;
90         __u32 last_xid;
91         struct ptlrpc_request *request = NULL;
92
93         ENTRY;
94         MOD_INC_USE_COUNT;
95
96         OBD_ALLOC(sbi, sizeof(*sbi));
97         if (!sbi) {
98                 MOD_DEC_USE_COUNT;
99                 RETURN(NULL);
100         }
101
102         sb->u.generic_sbp = sbi;
103
104         ll_options(data, &device, &version);
105
106         if (!device) {
107                 CERROR("no device\n");
108                 GOTO(out_free, sb = NULL);
109         }
110
111         devno = simple_strtoul(device, NULL, 0);
112         if (devno >= MAX_OBD_DEVICES) {
113                 CERROR("device of %s too high\n", device);
114                 GOTO(out_free, sb = NULL);
115         }
116
117         sbi->ll_conn.oc_dev = &obd_dev[devno];
118         err = obd_connect(&sbi->ll_conn);
119         if (err) {
120                 CERROR("cannot connect to %s: rc = %d\n", device, err);
121                 GOTO(out_free, sb = NULL);
122         }
123
124         ptlrpc_init_client(ptlrpc_connmgr, ll_recover,
125                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
126                            &sbi->ll_mds_client);
127
128         sbi->ll_mds_conn = ptlrpc_uuid_to_connection("mds");
129         if (!sbi->ll_mds_conn) {
130                 CERROR("cannot find MDS\n");
131                 GOTO(out_disc, sb = NULL);
132         }
133
134         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
135         if (err) {
136                 CERROR("cannot connect to MDS: rc = %d\n", err);
137                 ptlrpc_put_connection(sbi->ll_mds_conn);
138                 GOTO(out_disc, sb = NULL);
139         }
140
141         sbi->ll_mds_conn->c_level = LUSTRE_CONN_FULL;
142
143         /* XXX: need to store the last_* values somewhere */
144         err = mdc_connect(&sbi->ll_mds_client, sbi->ll_mds_conn,
145                           &rootfid, &last_committed, &last_rcvd, &last_xid,
146                           &request);
147         if (err) {
148                 CERROR("cannot mds_connect: rc = %d\n", err);
149                 GOTO(out_disc, sb = NULL);
150         }
151         CERROR("rootfid %ld\n", (unsigned long)rootfid.id);
152         sbi->ll_rootino = rootfid.id;
153
154         sb->s_maxbytes = 1ULL << 36;
155         sb->s_blocksize = PAGE_SIZE;
156         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
157         sb->s_magic = LL_SUPER_MAGIC;
158         sb->s_op = &ll_super_operations;
159
160         /* make root inode */
161         err = mdc_getattr(&sbi->ll_mds_client, sbi->ll_mds_conn,
162                           sbi->ll_rootino, S_IFDIR,
163                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, &request);
164         if (err) {
165                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
166                 GOTO(out_req, sb = NULL);
167         }
168
169         /* initialize committed transaction callback daemon */
170         spin_lock_init(&sbi->ll_commitcbd_lock); 
171         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
172         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
173         sbi->ll_commitcbd_flags = 0;
174         err = ll_commitcbd_setup(sbi);
175         if (err) {
176                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
177                 GOTO(out_req, sb = NULL);
178         }
179
180         root = iget4(sb, sbi->ll_rootino, NULL,
181                      lustre_msg_buf(request->rq_repmsg, 0));
182         if (root) {
183                 sb->s_root = d_alloc_root(root);
184         } else {
185                 CERROR("lustre_lite: bad iget4 for root\n");
186                 GOTO(out_req, sb = NULL);
187         }
188
189 out_req:
190         ptlrpc_free_req(request);
191         if (!sb) {
192 out_disc:
193                 obd_disconnect(&sbi->ll_conn);
194 out_free:
195                 MOD_DEC_USE_COUNT;
196                 OBD_FREE(sbi, sizeof(*sbi));
197         }
198         if (device)
199                 OBD_FREE(device, strlen(device) + 1);
200         if (version)
201                 OBD_FREE(version, strlen(version) + 1);
202
203         RETURN(sb);
204 } /* ll_read_super */
205
206 static void ll_put_super(struct super_block *sb)
207 {
208         struct ll_sb_info *sbi = sb->u.generic_sbp;
209         ENTRY;
210         ll_commitcbd_cleanup(sbi);
211         obd_disconnect(&sbi->ll_conn);
212         ptlrpc_put_connection(sbi->ll_mds_conn);
213         ptlrpc_cleanup_client(&sbi->ll_mds_client);
214         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
215         MOD_DEC_USE_COUNT;
216         EXIT;
217 } /* ll_put_super */
218
219
220 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
221 static void ll_delete_inode(struct inode *inode)
222 {
223         if (S_ISREG(inode->i_mode)) { 
224                 int err; 
225                 struct obdo *oa; 
226                 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
227                 if (!oa) { 
228                         CERROR("no memory\n"); 
229                 }
230
231                 err = obd_destroy(ll_i2obdconn(inode), oa); 
232                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
233                        (unsigned long long)oa->o_id, err);
234                 obdo_free(oa);
235         }
236
237         clear_inode(inode); 
238 }
239
240 /* like inode_setattr, but doesn't mark the inode dirty */ 
241 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
242 {
243         unsigned int ia_valid = attr->ia_valid;
244         int error = 0;
245
246         if ((ia_valid & ATTR_SIZE) && trunc ) {
247                 error = vmtruncate(inode, attr->ia_size);
248                 if (error)
249                         goto out;
250         } else if (ia_valid & ATTR_SIZE) { 
251                 inode->i_size = attr->ia_size;
252         }               
253
254         if (ia_valid & ATTR_UID)
255                 inode->i_uid = attr->ia_uid;
256         if (ia_valid & ATTR_GID)
257                 inode->i_gid = attr->ia_gid;
258         if (ia_valid & ATTR_ATIME)
259                 inode->i_atime = attr->ia_atime;
260         if (ia_valid & ATTR_MTIME)
261                 inode->i_mtime = attr->ia_mtime;
262         if (ia_valid & ATTR_CTIME)
263                 inode->i_ctime = attr->ia_ctime;
264         if (ia_valid & ATTR_MODE) {
265                 inode->i_mode = attr->ia_mode;
266                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
267                         inode->i_mode &= ~S_ISGID;
268         }
269 out:
270         return error;
271 }
272
273 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
274 {
275         struct ptlrpc_request *request = NULL;
276         struct ll_sb_info *sbi = ll_i2sbi(inode);
277         int err;
278
279         ENTRY;
280
281         /* change incore inode */
282         ll_attr2inode(inode, attr, do_trunc);
283
284         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
285                           &request);
286         if (err)
287                 CERROR("mdc_setattr fails (%d)\n", err);
288
289         ptlrpc_req_finished(request);
290
291         RETURN(err);
292 }
293
294 int ll_setattr(struct dentry *de, struct iattr *attr)
295 {
296         return ll_inode_setattr(de->d_inode, attr, 1);
297 }
298
299 static int ll_statfs(struct super_block *sb, struct statfs *buf)
300 {
301         struct statfs tmp;
302         int err;
303         ENTRY;
304
305         err = obd_statfs(&ll_s2sbi(sb)->ll_conn, &tmp);
306         if (err) {
307                 CERROR("obd_statfs fails (%d)\n", err);
308                 RETURN(err);
309         }
310         memcpy(buf, &tmp, sizeof(*buf));
311         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
312
313         RETURN(err);
314 }
315
316 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
317 {
318         struct ll_inode_info *ii = 
319                 (struct ll_inode_info *) &dst->u.generic_ip;
320
321         /* core attributes first */
322         if ( body->valid & OBD_MD_FLID )
323                 dst->i_ino = body->ino;
324         if ( body->valid & OBD_MD_FLATIME ) 
325                 dst->i_atime = body->atime;
326         if ( body->valid & OBD_MD_FLMTIME ) 
327                 dst->i_mtime = body->mtime;
328         if ( body->valid & OBD_MD_FLCTIME ) 
329                 dst->i_ctime = body->ctime;
330         if ( body->valid & OBD_MD_FLSIZE ) 
331                 dst->i_size = body->size;
332         if ( body->valid & OBD_MD_FLMODE ) 
333                 dst->i_mode = body->mode;
334         if ( body->valid & OBD_MD_FLUID ) 
335                 dst->i_uid = body->uid;
336         if ( body->valid & OBD_MD_FLGID ) 
337                 dst->i_gid = body->gid;
338         if ( body->valid & OBD_MD_FLFLAGS ) 
339                 dst->i_flags = body->flags;
340         if ( body->valid & OBD_MD_FLNLINK )
341                 dst->i_nlink = body->nlink;
342         if ( body->valid & OBD_MD_FLGENER )
343                 dst->i_generation = body->generation;
344
345         /* this will become more elaborate for striping etc */ 
346         if (body->valid & OBD_MD_FLOBJID) 
347                 ii->lli_objid = body->objid;
348 #if 0
349
350         if (obdo_has_inline(oa)) {
351                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
352                     S_ISFIFO(inode->i_mode)) {
353                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
354                         CDEBUG(D_INODE,
355                                "copying device %x from obdo to inode\n", rdev);
356                         init_special_inode(inode, inode->i_mode, rdev);
357                 } else {
358                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
359                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
360                 }
361                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
362         }
363 #endif 
364 } /* ll_to_inode */
365
366 static inline void ll_read_inode2(struct inode *inode, void *opaque)
367 {
368         struct mds_body *body = opaque; 
369         
370         ENTRY;
371         ll_to_inode(inode, body); 
372
373         /* OIDEBUG(inode); */
374
375         if (S_ISREG(inode->i_mode)) {
376                 inode->i_op = &ll_file_inode_operations;
377                 inode->i_fop = &ll_file_operations;
378                 inode->i_mapping->a_ops = &ll_aops;
379                 EXIT;
380         } else if (S_ISDIR(inode->i_mode)) {
381                 inode->i_op = &ll_dir_inode_operations;
382                 inode->i_fop = &ll_dir_operations; 
383                 inode->i_mapping->a_ops = &ll_dir_aops;
384                 EXIT;
385         } else if (S_ISLNK(inode->i_mode)) {
386                 inode->i_op = &ll_fast_symlink_inode_operations;
387                 EXIT;
388         } else {
389                 init_special_inode(inode, inode->i_mode,
390                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
391                 EXIT;
392         }
393
394         return;
395 }
396
397 /* exported operations */
398 struct super_operations ll_super_operations =
399 {
400         read_inode2: ll_read_inode2,
401         delete_inode: ll_delete_inode,
402         put_super: ll_put_super,
403         statfs: ll_statfs
404 };
405
406 struct file_system_type lustre_lite_fs_type = {
407         "lustre_lite", 0, ll_read_super, NULL
408 };
409
410 static int __init init_lustre_lite(void)
411 {
412         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
413         ll_file_data_slab = kmem_cache_create("ll_file_data",
414                                               sizeof(struct ll_file_data), 0,
415                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
416         if (ll_file_data_slab == NULL)
417                 return -ENOMEM;
418         return register_filesystem(&lustre_lite_fs_type);
419 }
420
421 static void __exit exit_lustre_lite(void)
422 {
423         unregister_filesystem(&lustre_lite_fs_type);
424         kmem_cache_destroy(ll_file_data_slab);
425 }
426
427 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
428 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
429 MODULE_LICENSE("GPL");
430
431 module_init(init_lustre_lite);
432 module_exit(exit_lustre_lite);