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