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