Whamcloud - gitweb
- much of the striping configuration managment and setup builds up
[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 #define DEBUG_SUBSYSTEM S_LLITE
18
19 #include <linux/module.h>
20 #include <linux/lustre_lite.h>
21 #include <linux/lustre_ha.h>
22 #include <linux/lustre_dlm.h>
23
24 kmem_cache_t *ll_file_data_slab;
25 extern struct address_space_operations ll_aops;
26 extern struct address_space_operations ll_dir_aops;
27 struct super_operations ll_super_operations;
28
29 extern int ll_recover(struct ptlrpc_client *);
30 extern int ll_commitcbd_setup(struct ll_sb_info *);
31 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
32
33 static char *ll_read_opt(const char *opt, char *data)
34 {
35         char *value;
36         char *retval;
37         ENTRY;
38
39         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
40         if ( strncmp(opt, data, strlen(opt)) )
41                 RETURN(NULL);
42         if ( (value = strchr(data, '=')) == NULL )
43                 RETURN(NULL);
44
45         value++;
46         OBD_ALLOC(retval, strlen(value) + 1);
47         if ( !retval ) {
48                 CERROR("out of memory!\n");
49                 RETURN(NULL);
50         }
51         
52         memcpy(retval, value, strlen(value)+1);
53         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
54         RETURN(retval);
55 }
56
57 static void ll_options(char *options, char **ost, char **mds)
58 {
59         char *this_char;
60         ENTRY;
61
62         if (!options) {
63                 EXIT;
64                 return;
65         }
66
67         for (this_char = strtok (options, ",");
68              this_char != NULL;
69              this_char = strtok (NULL, ",")) {
70                 CDEBUG(D_INFO, "this_char %s\n", this_char);
71                 if ( (!*ost && (*ost = ll_read_opt("ost", this_char)))||
72                      (!*mds && (*mds = ll_read_opt("mds", this_char))) )
73                         continue;
74         }
75         EXIT;
76 }
77
78 static struct super_block * ll_read_super(struct super_block *sb,
79                                           void *data, int silent)
80 {
81         struct inode *root = 0;
82         struct ll_sb_info *sbi;
83         char *ost = NULL;
84         char *mds = NULL;
85         int devno;
86         int err;
87         struct ll_fid rootfid;
88         __u64 last_committed, last_rcvd;
89         __u32 last_xid;
90         struct ptlrpc_request *request = NULL;
91         struct ll_inode_md md;
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, &ost, &mds);
105
106         if (!ost) {
107                 CERROR("no ost\n");
108                 GOTO(out_free, sb = NULL);
109         }
110
111         if (!mds) {
112                 CERROR("no mds\n");
113                 GOTO(out_free, sb = NULL);
114         }
115
116         devno = simple_strtoul(mds, NULL, 0);
117         if (devno >= MAX_OBD_DEVICES) {
118                 CERROR("devno of %s too high\n", mds);
119                 GOTO(out_free, sb = NULL);
120         }
121
122         /* First the MDS since an LOV requires an the MDC connection 
123            to find its descriptor */ 
124 #if 0
125         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
126         if (err) {
127                 CERROR("cannot connect to MDS: rc = %d\n", err);
128                 GOTO(out_rpc, sb = NULL);
129         }
130 #endif 
131
132         err = obd_connect(&sbi->ll_mdc_conn, &obd_dev[devno]);
133         if (err) {
134                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
135                 GOTO(out_free, sb = NULL);
136         }
137         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
138
139         /* now the OST, which could be an LOV */ 
140         devno = simple_strtoul(ost, NULL, 0);
141         if (devno >= MAX_OBD_DEVICES) {
142                 CERROR("devno of %s too high\n", ost);
143                 GOTO(out_free, sb = NULL);
144         }
145         err = obd_connect(&sbi->ll_osc_conn, &obd_dev[devno]);
146         if (err) {
147                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
148                 GOTO(out_free, sb = NULL);
149         }
150
151         /* XXX: need to store the last_* values somewhere */
152         err = mdc_getstatus(&sbi->ll_mdc_conn,
153                             &rootfid, &last_committed, 
154                             &last_rcvd, &last_xid,
155                             &request);
156         if (err) {
157                 CERROR("cannot mds_connect: rc = %d\n", err);
158                 GOTO(out_disc, sb = NULL);
159         }
160         CDEBUG(D_SUPER, "rootfid %ld\n", (unsigned long)rootfid.id);
161         sbi->ll_rootino = rootfid.id;
162
163         sb->s_maxbytes = 1ULL << 36;
164         /* XXX get this with a get_info call (like we have in OBDFS),
165            this info call should return the blocksize of the MDS (statfs?) */
166         sb->s_blocksize = 4096;
167         sb->s_blocksize_bits = 12;
168         sb->s_magic = LL_SUPER_MAGIC;
169         sb->s_op = &ll_super_operations;
170
171         /* make root inode */
172         err = mdc_getattr(&sbi->ll_mdc_conn,
173                           sbi->ll_rootino, S_IFDIR,
174                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
175         if (err) {
176                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
177                 GOTO(out_mdc, sb = NULL);
178         }
179
180         /* initialize committed transaction callback daemon */
181         spin_lock_init(&sbi->ll_commitcbd_lock); 
182         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
183         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
184         sbi->ll_commitcbd_flags = 0;
185         err = ll_commitcbd_setup(sbi);
186         if (err) {
187                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
188                 GOTO(out_mdc, sb = NULL);
189         }
190
191         md.body = lustre_msg_buf(request->rq_repmsg, 0);
192         md.obdo = NULL;
193         root = iget4(sb, sbi->ll_rootino, NULL, &md);
194                      
195         if (root) {
196                 sb->s_root = d_alloc_root(root);
197         } else {
198                 CERROR("lustre_lite: bad iget4 for root\n");
199                 GOTO(out_cdb, sb = NULL);
200         }
201
202         ptlrpc_free_req(request);
203
204 out_dev:
205         if (mds)
206                 OBD_FREE(mds, strlen(mds) + 1);
207         if (ost)
208                 OBD_FREE(ost, strlen(ost) + 1);
209
210         RETURN(sb);
211
212 out_cdb:
213         ll_commitcbd_cleanup(sbi);
214 out_mdc:
215         obd_disconnect(&sbi->ll_mdc_conn);
216         obd_disconnect(&sbi->ll_osc_conn);
217 out_disc:
218         ptlrpc_free_req(request);
219         obd_disconnect(&sbi->ll_osc_conn);
220 out_free:
221         OBD_FREE(sbi, sizeof(*sbi));
222
223         MOD_DEC_USE_COUNT;
224         goto out_dev;
225 } /* ll_read_super */
226
227 static void ll_put_super(struct super_block *sb)
228 {
229         struct ll_sb_info *sbi = ll_s2sbi(sb);
230         ENTRY;
231         ll_commitcbd_cleanup(sbi);
232         obd_disconnect(&sbi->ll_osc_conn);
233         obd_disconnect(&sbi->ll_mdc_conn);
234         OBD_FREE(sbi, sizeof(*sbi));
235
236         MOD_DEC_USE_COUNT;
237         EXIT;
238 } /* ll_put_super */
239
240 static void ll_clear_inode(struct inode *inode)
241 {
242         if (atomic_read(&inode->i_count) == 0) {
243                 struct obdo *oa = ll_i2info(inode)->lli_obdo;
244                 if (oa) {
245                         obdo_free(oa);
246                         ll_i2info(inode)->lli_obdo = NULL;
247                 }
248                 if (ll_i2info(inode)->lli_symlink_name) {
249                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
250                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
251                         ll_i2info(inode)->lli_symlink_name = NULL;
252                 }
253         }
254 }
255
256 static void ll_delete_inode(struct inode *inode)
257 {
258         if (S_ISREG(inode->i_mode)) { 
259                 int err;
260                 struct obdo *oa;
261                 oa = ll_i2info(inode)->lli_obdo;
262
263                 if (!oa)
264                         GOTO(out, -EINVAL);
265
266                 if (oa->o_id == 0)
267                         /* No obdo was ever created */
268                         GOTO(out, 0);
269
270                 err = obd_destroy(ll_i2obdconn(inode), oa);
271                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
272                        (unsigned long long)oa->o_id, err);
273         }
274 out:
275         clear_inode(inode);
276 }
277
278 /* like inode_setattr, but doesn't mark the inode dirty */
279 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
280 {
281         unsigned int ia_valid = attr->ia_valid;
282         int error = 0;
283
284         if ((ia_valid & ATTR_SIZE) && trunc) {
285                 error = vmtruncate(inode, attr->ia_size);
286                 if (error)
287                         goto out;
288         } else if (ia_valid & ATTR_SIZE)
289                 inode->i_size = attr->ia_size;
290
291         if (ia_valid & ATTR_UID)
292                 inode->i_uid = attr->ia_uid;
293         if (ia_valid & ATTR_GID)
294                 inode->i_gid = attr->ia_gid;
295         if (ia_valid & ATTR_ATIME)
296                 inode->i_atime = attr->ia_atime;
297         if (ia_valid & ATTR_MTIME)
298                 inode->i_mtime = attr->ia_mtime;
299         if (ia_valid & ATTR_CTIME)
300                 inode->i_ctime = attr->ia_ctime;
301         if (ia_valid & ATTR_MODE) {
302                 inode->i_mode = attr->ia_mode;
303                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
304                         inode->i_mode &= ~S_ISGID;
305         }
306 out:
307         return error;
308 }
309
310 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
311 {
312         struct ptlrpc_request *request = NULL;
313         struct ll_sb_info *sbi = ll_i2sbi(inode);
314         int err;
315
316         ENTRY;
317
318         /* change incore inode */
319         ll_attr2inode(inode, attr, do_trunc);
320
321         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr,
322                           &request);
323         if (err)
324                 CERROR("mdc_setattr fails (%d)\n", err);
325
326         ptlrpc_req_finished(request);
327
328         RETURN(err);
329 }
330
331 int ll_setattr(struct dentry *de, struct iattr *attr)
332 {
333         int rc = inode_change_ok(de->d_inode, attr);
334
335         if (rc)
336                 return rc;
337
338         return ll_inode_setattr(de->d_inode, attr, 1);
339 }
340
341 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
342 {
343         struct ptlrpc_request *request = NULL;
344         struct ll_sb_info *sbi = ll_s2sbi(sb);
345         int err;
346         ENTRY;
347
348         memset(sfs, 0, sizeof(*sfs));
349         err = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
350         if (err)
351                 CERROR("obd_statfs fails (%d)\n", err);
352         else
353                 CDEBUG(D_SUPER,
354                        "statfs returns blocks %ld/%ld objects %ld/%ld\n",
355                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
356
357         ptlrpc_req_finished(request);
358
359         RETURN(err);
360 }
361
362 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
363 {
364         struct mds_body *body = md->body;
365         struct ll_inode_info *ii = ll_i2info(dst);
366
367         /* core attributes first */
368         if (body->valid & OBD_MD_FLID)
369                 dst->i_ino = body->ino;
370         if (body->valid & OBD_MD_FLATIME)
371                 dst->i_atime = body->atime;
372         if (body->valid & OBD_MD_FLMTIME)
373                 dst->i_mtime = body->mtime;
374         if (body->valid & OBD_MD_FLCTIME)
375                 dst->i_ctime = body->ctime;
376         if (body->valid & OBD_MD_FLSIZE)
377                 dst->i_size = body->size;
378         if (body->valid & OBD_MD_FLMODE)
379                 dst->i_mode = body->mode;
380         if (body->valid & OBD_MD_FLUID)
381                 dst->i_uid = body->uid;
382         if (body->valid & OBD_MD_FLGID)
383                 dst->i_gid = body->gid;
384         if (body->valid & OBD_MD_FLFLAGS)
385                 dst->i_flags = body->flags;
386         if (body->valid & OBD_MD_FLNLINK)
387                 dst->i_nlink = body->nlink;
388         if (body->valid & OBD_MD_FLGENER)
389                 dst->i_generation = body->generation;
390
391         /* this will become more elaborate for striping etc */
392         if (md->obdo != NULL && md->obdo->o_valid != 0) {
393                 ii->lli_obdo = obdo_alloc();
394                 memcpy(ii->lli_obdo, md->obdo, sizeof(*md->obdo));
395         }
396 #if 0
397
398         if (obdo_has_inline(oa)) {
399                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
400                     S_ISFIFO(inode->i_mode)) {
401                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
402                         CDEBUG(D_INODE,
403                                "copying device %x from obdo to inode\n", rdev);
404                         init_special_inode(inode, inode->i_mode, rdev);
405                 } else {
406                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
407                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
408                 }
409                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
410         }
411 #endif 
412 } /* ll_to_inode */
413
414 static void ll_read_inode2(struct inode *inode, void *opaque)
415 {
416         struct ll_inode_md *md = opaque;
417
418         ENTRY;
419         ll_to_inode(inode, md);
420
421         /* OIDEBUG(inode); */
422
423         if (S_ISREG(inode->i_mode)) {
424                 inode->i_op = &ll_file_inode_operations;
425                 inode->i_fop = &ll_file_operations;
426                 inode->i_mapping->a_ops = &ll_aops;
427                 EXIT;
428         } else if (S_ISDIR(inode->i_mode)) {
429                 inode->i_op = &ll_dir_inode_operations;
430                 inode->i_fop = &ll_dir_operations;
431                 inode->i_mapping->a_ops = &ll_dir_aops;
432                 EXIT;
433         } else if (S_ISLNK(inode->i_mode)) {
434                 inode->i_op = &ll_fast_symlink_inode_operations;
435                 EXIT;
436         } else {
437                 init_special_inode(inode, inode->i_mode,
438                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
439                 EXIT;
440         }
441
442         return;
443 }
444
445 /* exported operations */
446 struct super_operations ll_super_operations =
447 {
448         read_inode2: ll_read_inode2,
449         clear_inode: ll_clear_inode,
450         delete_inode: ll_delete_inode,
451         put_super: ll_put_super,
452         statfs: ll_statfs
453 };
454
455 struct file_system_type lustre_lite_fs_type = {
456         "lustre_lite", 0, ll_read_super, NULL
457 };
458
459 static int __init init_lustre_lite(void)
460 {
461         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
462         ll_file_data_slab = kmem_cache_create("ll_file_data",
463                                               sizeof(struct ll_file_data), 0,
464                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
465         if (ll_file_data_slab == NULL)
466                 return -ENOMEM;
467         return register_filesystem(&lustre_lite_fs_type);
468 }
469
470 static void __exit exit_lustre_lite(void)
471 {
472         unregister_filesystem(&lustre_lite_fs_type);
473         kmem_cache_destroy(ll_file_data_slab);
474 }
475
476 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
477 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
478 MODULE_LICENSE("GPL");
479
480 module_init(init_lustre_lite);
481 module_exit(exit_lustre_lite);