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