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