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