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