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