Whamcloud - gitweb
Fix problem with multiple connections to the same local device.
[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         if (!obd) {
118                 CERROR("MDS %s: not setup or attached\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);
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
141         obd = class_uuid2obd(ost);
142         if (!obd) {
143                 CERROR("OST %s: not setup or attached\n", ost);
144                 GOTO(out_free, sb = NULL);
145         }
146 #warning error: need to set obd->u.lov.mdc_connh from sbi->ll_mdc_conn
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         ptlrpc_req_finished(request);
172
173         sb->s_op = &ll_super_operations;
174
175         /* make root inode */
176         err = mdc_getattr(&sbi->ll_mdc_conn,
177                           sbi->ll_rootino, S_IFDIR, OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
178         if (err) {
179                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
180                 GOTO(out_mdc, sb = NULL);
181         }
182
183         /* initialize committed transaction callback daemon */
184         spin_lock_init(&sbi->ll_commitcbd_lock); 
185         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
186         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
187         sbi->ll_commitcbd_flags = 0;
188         err = ll_commitcbd_setup(sbi);
189         if (err) {
190                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
191                 GOTO(out_request, sb = NULL);
192         }
193
194         md.body = lustre_msg_buf(request->rq_repmsg, 0);
195         md.md = NULL;
196         root = iget4(sb, sbi->ll_rootino, NULL, &md);
197                      
198         if (root) {
199                 sb->s_root = d_alloc_root(root);
200         } else {
201                 CERROR("lustre_lite: bad iget4 for root\n");
202                 GOTO(out_cdb, sb = NULL);
203         }
204
205         ptlrpc_free_req(request);
206
207 out_dev:
208         if (mds)
209                 OBD_FREE(mds, strlen(mds) + 1);
210         if (ost)
211                 OBD_FREE(ost, strlen(ost) + 1);
212
213         RETURN(sb);
214
215 out_cdb:
216         ll_commitcbd_cleanup(sbi);
217 out_request:
218         ptlrpc_free_req(request);
219         obd_disconnect(&sbi->ll_osc_conn);
220 out_mdc:
221         obd_disconnect(&sbi->ll_mdc_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 lov_stripe_md *md = ll_i2info(inode)->lli_smd;
246                 if (md) {
247                         OBD_FREE(md, md->lmd_size); 
248                         ll_i2info(inode)->lli_smd = 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                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
264  
265                if (!md)
266                         GOTO(out, -EINVAL);
267
268                 oa.o_id = md->lmd_object_id;
269                 oa.o_easize = md->lmd_size;
270                 if (oa.o_id == 0) { 
271                         CERROR("This really happens\n"); 
272                         /* No obdo was ever created */
273                         GOTO(out, 0);
274                 }
275
276                 err = obd_destroy(ll_i2obdconn(inode), &oa, md);
277                 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
278                        md->lmd_object_id, err);
279         }
280 out:
281         clear_inode(inode);
282 }
283
284 /* like inode_setattr, but doesn't mark the inode dirty */
285 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
286 {
287         unsigned int ia_valid = attr->ia_valid;
288         int error = 0;
289
290         if ((ia_valid & ATTR_SIZE) && trunc) {
291                 error = vmtruncate(inode, attr->ia_size);
292                 if (error)
293                         goto out;
294         } else if (ia_valid & ATTR_SIZE)
295                 inode->i_size = attr->ia_size;
296
297         if (ia_valid & ATTR_UID)
298                 inode->i_uid = attr->ia_uid;
299         if (ia_valid & ATTR_GID)
300                 inode->i_gid = attr->ia_gid;
301         if (ia_valid & ATTR_ATIME)
302                 inode->i_atime = attr->ia_atime;
303         if (ia_valid & ATTR_MTIME)
304                 inode->i_mtime = attr->ia_mtime;
305         if (ia_valid & ATTR_CTIME)
306                 inode->i_ctime = attr->ia_ctime;
307         if (ia_valid & ATTR_MODE) {
308                 inode->i_mode = attr->ia_mode;
309                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
310                         inode->i_mode &= ~S_ISGID;
311         }
312 out:
313         return error;
314 }
315
316 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
317 {
318         struct ptlrpc_request *request = NULL;
319         struct ll_sb_info *sbi = ll_i2sbi(inode);
320         int err;
321
322         ENTRY;
323
324         /* change incore inode */
325         ll_attr2inode(inode, attr, do_trunc);
326
327         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
328         if (err)
329                 CERROR("mdc_setattr fails (%d)\n", err);
330
331         ptlrpc_req_finished(request);
332
333         RETURN(err);
334 }
335
336 int ll_setattr(struct dentry *de, struct iattr *attr)
337 {
338         int rc = inode_change_ok(de->d_inode, attr);
339
340         if (rc)
341                 return rc;
342
343         return ll_inode_setattr(de->d_inode, attr, 1);
344 }
345
346 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
347 {
348         struct ptlrpc_request *request = NULL;
349         struct ll_sb_info *sbi = ll_s2sbi(sb);
350         int rc;
351         ENTRY;
352
353         memset(sfs, 0, sizeof(*sfs));
354         rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
355         ptlrpc_req_finished(request);
356         if (rc)
357                 CERROR("obd_statfs fails: rc = %d\n", rc);
358         else
359                 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
360                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
361
362         /* temporary until mds_statfs returns statfs info for all OSTs */
363         if (!rc) {
364                 struct statfs obd_sfs;
365
366                 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
367                 if (rc) {
368                         CERROR("obd_statfs fails: rc = %d\n", rc);
369                         GOTO(out, rc);
370                 }
371                 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
372                        "objects %ld/%ld\n",
373                        obd_sfs.f_bavail, obd_sfs.f_blocks,
374                        obd_sfs.f_ffree, obd_sfs.f_files);
375
376                 sfs->f_bfree = obd_sfs.f_bfree;
377                 sfs->f_bavail = obd_sfs.f_bavail;
378                 sfs->f_blocks = obd_sfs.f_blocks;
379                 if (obd_sfs.f_ffree < sfs->f_ffree)
380                         sfs->f_ffree = obd_sfs.f_ffree;
381         }
382
383 out:
384         RETURN(rc);
385 }
386
387 inline int ll_stripe_md_size(struct super_block *sb)
388 {
389         struct mdc_obd *mdc = sbi2mdc(ll_s2sbi(sb));
390         return mdc->mdc_max_mdsize;
391 }
392
393 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
394 {
395         struct mds_body *body = md->body;
396         struct ll_inode_info *ii = ll_i2info(dst);
397
398         /* core attributes first */
399         if (body->valid & OBD_MD_FLID)
400                 dst->i_ino = body->ino;
401         if (body->valid & OBD_MD_FLATIME)
402                 dst->i_atime = body->atime;
403         if (body->valid & OBD_MD_FLMTIME)
404                 dst->i_mtime = body->mtime;
405         if (body->valid & OBD_MD_FLCTIME)
406                 dst->i_ctime = body->ctime;
407         if (body->valid & OBD_MD_FLSIZE)
408                 dst->i_size = body->size;
409         if (body->valid & OBD_MD_FLMODE)
410                 dst->i_mode = body->mode;
411         if (body->valid & OBD_MD_FLUID)
412                 dst->i_uid = body->uid;
413         if (body->valid & OBD_MD_FLGID)
414                 dst->i_gid = body->gid;
415         if (body->valid & OBD_MD_FLFLAGS)
416                 dst->i_flags = body->flags;
417         if (body->valid & OBD_MD_FLNLINK)
418                 dst->i_nlink = body->nlink;
419         if (body->valid & OBD_MD_FLGENER)
420                 dst->i_generation = body->generation;
421         if (body->valid & OBD_MD_FLRDEV)
422                 dst->i_rdev = body->extra;
423         if (md && md->md && md->md->lmd_stripe_count) { 
424                 struct lov_stripe_md *smd = md->md;
425                 int size = ll_stripe_md_size(dst->i_sb);
426                 if (md->md->lmd_size != size) { 
427                         CERROR("Striping metadata size error %ld\n",
428                                dst->i_ino); 
429                         LBUG();
430                 }
431                 OBD_ALLOC(ii->lli_smd, size);
432                 if (!ii->lli_smd){ 
433                         CERROR("No memory for %d\n", size);
434                         LBUG();
435                 }
436                 memcpy(ii->lli_smd, smd, size);
437         }
438 } /* ll_to_inode */
439
440 static void ll_read_inode2(struct inode *inode, void *opaque)
441 {
442         struct ll_inode_md *md = opaque;
443
444         ENTRY;
445         ll_to_inode(inode, md);
446
447         /* OIDEBUG(inode); */
448
449         if (S_ISREG(inode->i_mode)) {
450                 inode->i_op = &ll_file_inode_operations;
451                 inode->i_fop = &ll_file_operations;
452                 inode->i_mapping->a_ops = &ll_aops;
453                 EXIT;
454         } else if (S_ISDIR(inode->i_mode)) {
455                 inode->i_op = &ll_dir_inode_operations;
456                 inode->i_fop = &ll_dir_operations;
457                 inode->i_mapping->a_ops = &ll_dir_aops;
458                 EXIT;
459         } else if (S_ISLNK(inode->i_mode)) {
460                 inode->i_op = &ll_fast_symlink_inode_operations;
461                 EXIT;
462         } else {
463                 init_special_inode(inode, inode->i_mode,
464                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
465                 EXIT;
466         }
467
468         return;
469 }
470
471 /* exported operations */
472 struct super_operations ll_super_operations =
473 {
474         read_inode2: ll_read_inode2,
475         clear_inode: ll_clear_inode,
476         delete_inode: ll_delete_inode,
477         put_super: ll_put_super,
478         statfs: ll_statfs
479 };
480
481 struct file_system_type lustre_lite_fs_type = {
482         "lustre_lite", 0, ll_read_super, NULL
483 };
484
485 static int __init init_lustre_lite(void)
486 {
487         printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
488         ll_file_data_slab = kmem_cache_create("ll_file_data",
489                                               sizeof(struct ll_file_data), 0,
490                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
491         if (ll_file_data_slab == NULL)
492                 return -ENOMEM;
493         return register_filesystem(&lustre_lite_fs_type);
494 }
495
496 static void __exit exit_lustre_lite(void)
497 {
498         unregister_filesystem(&lustre_lite_fs_type);
499         kmem_cache_destroy(ll_file_data_slab);
500 }
501
502 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
503 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
504 MODULE_LICENSE("GPL");
505
506 module_init(init_lustre_lite);
507 module_exit(exit_lustre_lite);