Whamcloud - gitweb
- Rename the ptlrpc-general reconnection and replay functions, and export them
[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/random.h>
16 #include <linux/lustre_lite.h>
17 #include <linux/lustre_ha.h>
18 #include <linux/obd_lov.h>
19 #include <linux/lustre_dlm.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22
23 kmem_cache_t *ll_file_data_slab;
24 extern struct address_space_operations ll_aops;
25 extern struct address_space_operations ll_dir_aops;
26 struct super_operations ll_super_operations;
27
28 extern int ll_recover(struct recovd_data *, int);
29 extern int ll_commitcbd_setup(struct ll_sb_info *);
30 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
31
32 static char *ll_read_opt(const char *opt, char *data)
33 {
34         char *value;
35         char *retval;
36         ENTRY;
37
38         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
39         if ( strncmp(opt, data, strlen(opt)) )
40                 RETURN(NULL);
41         if ( (value = strchr(data, '=')) == NULL )
42                 RETURN(NULL);
43
44         value++;
45         OBD_ALLOC(retval, strlen(value) + 1);
46         if ( !retval ) {
47                 CERROR("out of memory!\n");
48                 RETURN(NULL);
49         }
50
51         memcpy(retval, value, strlen(value)+1);
52         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
53         RETURN(retval);
54 }
55
56 static int ll_set_opt(const char *opt, char *data, int fl)
57 {
58         ENTRY;
59
60         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
61         if ( strncmp(opt, data, strlen(opt)) )
62                 RETURN(0);
63         else
64                 RETURN(fl);
65 }
66
67 static void ll_options(char *options, char **ost, char **mds, int *flags)
68 {
69         char *this_char;
70         ENTRY;
71
72         if (!options) {
73                 EXIT;
74                 return;
75         }
76
77         for (this_char = strtok (options, ",");
78              this_char != NULL;
79              this_char = strtok (NULL, ",")) {
80                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
81                 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
82                      (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
83                      (!(*flags & LL_SBI_NOLCK) && ((*flags) = (*flags) | 
84                       ll_set_opt("nolock", this_char, LL_SBI_NOLCK))) )
85                         continue;
86         }
87         EXIT;
88 }
89
90 #ifndef log2
91 #define log2(n) ffz(~(n))
92 #endif
93
94 static struct super_block * ll_read_super(struct super_block *sb,
95                                           void *data, int silent)
96 {
97         struct inode *root = 0;
98         struct obd_device *obd;
99         struct ll_sb_info *sbi;
100         char *osc = NULL;
101         char *mdc = NULL;
102         int err;
103         struct ll_fid rootfid;
104         struct obd_statfs osfs;
105         __u64 last_committed;
106         __u64 last_xid;
107         struct ptlrpc_request *request = NULL;
108         struct ptlrpc_connection *mdc_conn;
109         struct ll_read_inode2_cookie lic;
110         class_uuid_t uuid;
111
112         ENTRY;
113         MOD_INC_USE_COUNT;
114
115         OBD_ALLOC(sbi, sizeof(*sbi));
116         if (!sbi) {
117                 MOD_DEC_USE_COUNT;
118                 RETURN(NULL);
119         }
120
121         INIT_LIST_HEAD(&sbi->ll_conn_chain);
122         sbi->ll_mount_epoch = 0;
123         generate_random_uuid(uuid);
124         class_uuid_unparse(uuid, sbi->ll_sb_uuid);
125
126         sb->u.generic_sbp = sbi;
127
128         ll_options(data, &osc, &mdc, &sbi->ll_flags);
129
130         if (!osc) {
131                 CERROR("no osc\n");
132                 GOTO(out_free, sb = NULL);
133         }
134
135         if (!mdc) {
136                 CERROR("no mdc\n");
137                 GOTO(out_free, sb = NULL);
138         }
139
140         obd = class_uuid2obd(mdc);
141         if (!obd) {
142                 CERROR("MDC %s: not setup or attached\n", mdc);
143                 GOTO(out_free, sb = NULL);
144         }
145
146         err = obd_connect(&sbi->ll_mdc_conn, obd, sbi->ll_sb_uuid);
147         if (err) {
148                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
149                 GOTO(out_free, sb = NULL);
150         }
151
152 #warning Peter: is this the right place to raise the connection level?
153         mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
154         mdc_conn->c_level = LUSTRE_CONN_FULL;
155         list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
156         spin_lock(&ptlrpc_recovd->recovd_lock);
157         recovd_conn_manage(class_conn2export(&sbi->ll_mdc_conn)->exp_connection,
158                            ptlrpc_recovd, ll_recover);
159         spin_unlock(&ptlrpc_recovd->recovd_lock);
160
161         obd = class_uuid2obd(osc);
162         if (!obd) {
163                 CERROR("OSC %s: not setup or attached\n", osc);
164                 GOTO(out_mdc, sb = NULL);
165         }
166
167         err = obd_connect(&sbi->ll_osc_conn, obd, sbi->ll_sb_uuid);
168         if (err) {
169                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
170                 GOTO(out_mdc, sb = NULL);
171         }
172         spin_lock(&ptlrpc_recovd->recovd_lock);
173         recovd_conn_manage(class_conn2export(&sbi->ll_osc_conn)->exp_connection,
174                            ptlrpc_recovd, ll_recover);
175         spin_unlock(&ptlrpc_recovd->recovd_lock);
176
177         /* XXX: need to store the last_* values somewhere */
178         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid, &last_committed,
179                             &last_xid, &request);
180         ptlrpc_req_finished(request);
181         if (err) {
182                 CERROR("cannot mds_connect: rc = %d\n", err);
183                 GOTO(out_mdc, sb = NULL);
184         }
185         CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
186         sbi->ll_rootino = rootfid.id;
187
188         memset(&osfs, 0, sizeof(osfs));
189         request = NULL;
190         err = mdc_statfs(&sbi->ll_mdc_conn, &osfs, &request);
191         ptlrpc_req_finished(request);
192         sb->s_blocksize = osfs.os_bsize;
193         sb->s_blocksize_bits = log2(osfs.os_bsize);
194         sb->s_magic = LL_SUPER_MAGIC;
195         sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
196
197         sb->s_op = &ll_super_operations;
198
199         /* make root inode */
200         request = NULL;
201         err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
202                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
203         if (err) {
204                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
205                 GOTO(out_request, sb = NULL);
206         }
207
208         /* initialize committed transaction callback daemon */
209         spin_lock_init(&sbi->ll_commitcbd_lock);
210         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
211         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
212         sbi->ll_commitcbd_flags = 0;
213         err = ll_commitcbd_setup(sbi);
214         if (err) {
215                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
216                 GOTO(out_request, sb = NULL);
217         }
218
219         lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
220         lic.lic_lmm = NULL;
221         root = iget4(sb, sbi->ll_rootino, NULL, &lic);
222
223         if (root) {
224                 sb->s_root = d_alloc_root(root);
225         } else {
226                 CERROR("lustre_lite: bad iget4 for root\n");
227                 GOTO(out_cdb, sb = NULL);
228         }
229
230         ptlrpc_req_finished(request);
231
232 out_dev:
233         if (mdc)
234                 OBD_FREE(mdc, strlen(mdc) + 1);
235         if (osc)
236                 OBD_FREE(osc, strlen(osc) + 1);
237
238         RETURN(sb);
239
240 out_cdb:
241         ll_commitcbd_cleanup(sbi);
242 out_request:
243         ptlrpc_req_finished(request);
244         obd_disconnect(&sbi->ll_osc_conn);
245 out_mdc:
246         obd_disconnect(&sbi->ll_mdc_conn);
247 out_free:
248         OBD_FREE(sbi, sizeof(*sbi));
249
250         MOD_DEC_USE_COUNT;
251         goto out_dev;
252 } /* ll_read_super */
253
254 static void ll_put_super(struct super_block *sb)
255 {
256         struct ll_sb_info *sbi = ll_s2sbi(sb);
257
258         ENTRY;
259         list_del(&sbi->ll_conn_chain);
260         ll_commitcbd_cleanup(sbi);
261         obd_disconnect(&sbi->ll_osc_conn);
262         obd_disconnect(&sbi->ll_mdc_conn);
263         OBD_FREE(sbi, sizeof(*sbi));
264
265         MOD_DEC_USE_COUNT;
266         EXIT;
267 } /* ll_put_super */
268
269 static void ll_clear_inode(struct inode *inode)
270 {
271         ENTRY;
272
273         if (atomic_read(&inode->i_count) == 0) {
274                 struct ll_inode_info *lli = ll_i2info(inode);
275                 struct lov_stripe_md *lsm = lli->lli_smd;
276                 char *symlink_name = lli->lli_symlink_name;
277
278                 if (lsm) {
279                         OBD_FREE(lsm, ll_ost_easize(inode->i_sb));
280                         lli->lli_smd = NULL;
281                 }
282                 if (symlink_name) {
283                         OBD_FREE(symlink_name, strlen(symlink_name) + 1);
284                         lli->lli_symlink_name = NULL;
285                 }
286         }
287         EXIT;
288 }
289
290 static void ll_delete_inode(struct inode *inode)
291 {
292         ENTRY;
293         if (S_ISREG(inode->i_mode)) {
294                 int err;
295                 struct obdo *oa;
296                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
297
298                 if (!lsm)
299                         GOTO(out, -EINVAL);
300
301                 if (lsm->lsm_object_id == 0) {
302                         CERROR("This really happens\n");
303                         /* No obdo was ever created */
304                         GOTO(out, 0);
305                 }
306
307                 oa = obdo_alloc();
308                 if (oa == NULL)
309                         GOTO(out, -ENOMEM);
310
311                 oa->o_id = lsm->lsm_object_id;
312                 oa->o_easize = ll_mds_easize(inode->i_sb);
313                 oa->o_mode = inode->i_mode;
314                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLEASIZE | OBD_MD_FLTYPE;
315
316                 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
317                 obdo_free(oa);
318                 CDEBUG(D_SUPER, "obd destroy of objid "LPX64" error %d\n",
319                        lsm->lsm_object_id, err);
320         }
321 out:
322         clear_inode(inode);
323         EXIT;
324 }
325
326 /* like inode_setattr, but doesn't mark the inode dirty */
327 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
328 {
329         unsigned int ia_valid = attr->ia_valid;
330         int error = 0;
331
332         if ((ia_valid & ATTR_SIZE) && trunc) {
333                 error = vmtruncate(inode, attr->ia_size);
334                 if (error)
335                         goto out;
336         } else if (ia_valid & ATTR_SIZE)
337                 inode->i_size = attr->ia_size;
338
339         if (ia_valid & ATTR_UID)
340                 inode->i_uid = attr->ia_uid;
341         if (ia_valid & ATTR_GID)
342                 inode->i_gid = attr->ia_gid;
343         if (ia_valid & ATTR_ATIME)
344                 inode->i_atime = attr->ia_atime;
345         if (ia_valid & ATTR_MTIME)
346                 inode->i_mtime = attr->ia_mtime;
347         if (ia_valid & ATTR_CTIME)
348                 inode->i_ctime = attr->ia_ctime;
349         if (ia_valid & ATTR_MODE) {
350                 inode->i_mode = attr->ia_mode;
351                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
352                         inode->i_mode &= ~S_ISGID;
353         }
354 out:
355         return error;
356 }
357
358 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
359 {
360         struct ptlrpc_request *request = NULL;
361         struct ll_sb_info *sbi = ll_i2sbi(inode);
362         int err;
363
364         ENTRY;
365
366         /* change incore inode */
367         ll_attr2inode(inode, attr, do_trunc);
368
369         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
370         if (err)
371                 CERROR("mdc_setattr fails (%d)\n", err);
372
373         ptlrpc_req_finished(request);
374
375         RETURN(err);
376 }
377
378 int ll_setattr(struct dentry *de, struct iattr *attr)
379 {
380         int rc = inode_change_ok(de->d_inode, attr);
381
382         if (rc)
383                 return rc;
384
385         return ll_inode_setattr(de->d_inode, attr, 1);
386 }
387
388 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
389 {
390         struct ptlrpc_request *request = NULL;
391         struct ll_sb_info *sbi = ll_s2sbi(sb);
392         struct obd_statfs osfs;
393         int rc;
394         ENTRY;
395
396         memset(sfs, 0, sizeof(*sfs));
397         rc = mdc_statfs(&sbi->ll_mdc_conn, &osfs, &request);
398         statfs_unpack(sfs, &osfs);
399         ptlrpc_req_finished(request);
400         if (rc)
401                 CERROR("mdc_statfs fails: rc = %d\n", rc);
402         else
403                 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
404                        " objects "LPU64"/"LPU64"\n",
405                        osfs.os_bavail, osfs.os_blocks,
406                        osfs.os_ffree, osfs.os_files);
407
408         /* temporary until mds_statfs returns statfs info for all OSTs */
409         if (!rc) {
410                 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
411                 if (rc) {
412                         CERROR("obd_statfs fails: rc = %d\n", rc);
413                         GOTO(out, rc);
414                 }
415                 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
416                        " objects "LPU64"/"LPU64"\n",
417                        osfs.os_bavail, osfs.os_blocks,
418                        osfs.os_ffree, osfs.os_files);
419
420                 while (osfs.os_blocks > ~0UL) {
421                         sfs->f_bsize <<= 1;
422
423                         osfs.os_blocks >>= 1;
424                         osfs.os_bfree >>= 1;
425                         osfs.os_bavail >>= 1;
426                 }
427                 sfs->f_blocks = osfs.os_blocks;
428                 sfs->f_bfree = osfs.os_bfree;
429                 sfs->f_bavail = osfs.os_bavail;
430                 if (osfs.os_ffree < (__u64)sfs->f_ffree)
431                         sfs->f_ffree = osfs.os_ffree;
432         }
433
434 out:
435         RETURN(rc);
436 }
437
438 void ll_update_inode(struct inode *inode, struct mds_body *body)
439 {
440         if (body->valid & OBD_MD_FLID)
441                 inode->i_ino = body->ino;
442         if (body->valid & OBD_MD_FLATIME)
443                 inode->i_atime = body->atime;
444         if (body->valid & OBD_MD_FLMTIME)
445                 inode->i_mtime = body->mtime;
446         if (body->valid & OBD_MD_FLCTIME)
447                 inode->i_ctime = body->ctime;
448         if (body->valid & OBD_MD_FLMODE)
449                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
450         if (body->valid & OBD_MD_FLTYPE)
451                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
452         if (body->valid & OBD_MD_FLUID)
453                 inode->i_uid = body->uid;
454         if (body->valid & OBD_MD_FLGID)
455                 inode->i_gid = body->gid;
456         if (body->valid & OBD_MD_FLFLAGS)
457                 inode->i_flags = body->flags;
458         if (body->valid & OBD_MD_FLNLINK)
459                 inode->i_nlink = body->nlink;
460         if (body->valid & OBD_MD_FLGENER)
461                 inode->i_generation = body->generation;
462         if (body->valid & OBD_MD_FLRDEV)
463                 inode->i_rdev = body->rdev;
464         if (body->valid & OBD_MD_FLSIZE)
465                 inode->i_size = body->size;
466 }
467
468 static void ll_read_inode2(struct inode *inode, void *opaque)
469 {
470         struct ll_read_inode2_cookie *lic = opaque;
471         struct mds_body *body = lic->lic_body;
472         struct ll_inode_info *lli = ll_i2info(inode);
473         ENTRY;
474         
475         sema_init(&lli->lli_open_sem, 1);
476         lli->lli_mount_epoch = ll_i2sbi(inode)->ll_mount_epoch;
477         
478         /* core attributes first */
479         ll_update_inode(inode, body);
480
481         //if (body->valid & OBD_MD_FLEASIZE)
482         if (lic && lic->lic_lmm) {
483                 struct lov_mds_md *lmm = lic->lic_lmm;
484                 int size;
485
486                 if (lmm->lmm_easize != ll_mds_easize(inode->i_sb)) {
487                         CERROR("Striping metadata size error %ld\n",
488                                inode->i_ino);
489                         LBUG();
490                 }
491                 size = ll_ost_easize(inode->i_sb);
492                 OBD_ALLOC(lli->lli_smd, size);
493                 if (!lli->lli_smd) {
494                         CERROR("No memory for %d\n", size);
495                         LBUG();
496                 }
497                 lov_unpackmd(lli->lli_smd, lmm);
498         } else {
499                 lli->lli_smd = NULL;
500         }
501
502         /* Get the authoritative file size */
503         if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
504                 int rc;
505
506                 rc = ll_file_size(inode, lli->lli_smd);
507                 if (rc) {
508                         CERROR("ll_file_size: %d\n", rc);
509                         /* FIXME: need to somehow prevent inode creation */
510                         LBUG();
511                 }
512         }
513
514         /* OIDEBUG(inode); */
515
516         if (S_ISREG(inode->i_mode)) {
517                 inode->i_op = &ll_file_inode_operations;
518                 inode->i_fop = &ll_file_operations;
519                 inode->i_mapping->a_ops = &ll_aops;
520                 EXIT;
521         } else if (S_ISDIR(inode->i_mode)) {
522                 inode->i_op = &ll_dir_inode_operations;
523                 inode->i_fop = &ll_dir_operations;
524                 inode->i_mapping->a_ops = &ll_dir_aops;
525                 EXIT;
526         } else if (S_ISLNK(inode->i_mode)) {
527                 inode->i_op = &ll_fast_symlink_inode_operations;
528                 EXIT;
529         } else {
530                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
531                 EXIT;
532         }
533 }
534
535 static inline void invalidate_request_list(struct list_head *req_list)
536 {
537         struct list_head *tmp, *n;
538         list_for_each_safe(tmp, n, req_list) {
539                 struct ptlrpc_request *req = 
540                         list_entry(tmp, struct ptlrpc_request, rq_list);
541                 CERROR("invalidating req xid %d op %d to %s:%d\n",
542                        (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
543                        req->rq_connection->c_remote_uuid,
544                        req->rq_import->imp_client->cli_request_portal);
545                 req->rq_flags |= PTL_RPC_FL_ERR;
546                 wake_up(&req->rq_wait_for_rep);
547         }
548 }
549
550 void ll_umount_begin(struct super_block *sb)
551 {
552         struct ll_sb_info *sbi = ll_s2sbi(sb);
553         struct list_head *ctmp;
554
555         ENTRY;
556        
557         list_for_each(ctmp, &sbi->ll_conn_chain) {
558                 struct ptlrpc_connection *conn;
559                 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
560
561                 spin_lock(&conn->c_lock);
562                 conn->c_flags |= CONN_INVALID;
563                 invalidate_request_list(&conn->c_sending_head);
564                 invalidate_request_list(&conn->c_delayed_head);
565                 spin_unlock(&conn->c_lock);
566         }
567
568         EXIT;
569 }
570
571 /* exported operations */
572 struct super_operations ll_super_operations =
573 {
574         read_inode2: ll_read_inode2,
575         clear_inode: ll_clear_inode,
576         delete_inode: ll_delete_inode,
577         put_super: ll_put_super,
578         statfs: ll_statfs,
579         umount_begin: ll_umount_begin
580 };
581
582 struct file_system_type lustre_lite_fs_type = {
583         "lustre_lite", 0, ll_read_super, NULL
584 };
585
586 static int __init init_lustre_lite(void)
587 {
588         printk(KERN_INFO "Lustre Lite 0.5.14, info@clusterfs.com\n");
589         ll_file_data_slab = kmem_cache_create("ll_file_data",
590                                               sizeof(struct ll_file_data), 0,
591                                               SLAB_HWCACHE_ALIGN, NULL, NULL);
592         if (ll_file_data_slab == NULL)
593                 return -ENOMEM;
594         return register_filesystem(&lustre_lite_fs_type);
595 }
596
597 static void __exit exit_lustre_lite(void)
598 {
599         unregister_filesystem(&lustre_lite_fs_type);
600         kmem_cache_destroy(ll_file_data_slab);
601 }
602
603 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
604 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
605 MODULE_LICENSE("GPL");
606
607 module_init(init_lustre_lite);
608 module_exit(exit_lustre_lite);