Whamcloud - gitweb
Don't update your trees yet..... Brian and I are trying to get our
[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) 1996 Peter J. Braam <braam@stelias.com>
10  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11  * Copryright (C) 1999 Seagate Technology Inc.
12  * Copryright (C) 2001 Mountain View Data, Inc.
13  * Copryright (C) 2002 Cluster File Systems, Inc.
14  *
15  */
16
17 #define DEBUG_SUBSYSTEM S_LLITE
18
19 #include <linux/module.h>
20 #include <linux/lustre_lite.h>
21 #include <linux/lustre_ha.h>
22 #include <linux/lustre_dlm.h>
23
24 kmem_cache_t *ll_file_data_slab;
25 extern struct address_space_operations ll_aops;
26 extern struct address_space_operations ll_dir_aops;
27 struct super_operations ll_super_operations;
28
29 extern int ll_recover(struct ptlrpc_client *);
30 extern int ll_commitcbd_setup(struct ll_sb_info *);
31 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
32
33 static char *ll_read_opt(const char *opt, char *data)
34 {
35         char *value;
36         char *retval;
37         ENTRY;
38
39         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
40         if ( strncmp(opt, data, strlen(opt)) )
41                 RETURN(NULL);
42         if ( (value = strchr(data, '=')) == NULL )
43                 RETURN(NULL);
44
45         value++;
46         OBD_ALLOC(retval, strlen(value) + 1);
47         if ( !retval ) {
48                 CERROR("out of memory!\n");
49                 RETURN(NULL);
50         }
51         
52         memcpy(retval, value, strlen(value)+1);
53         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
54         RETURN(retval);
55 }
56
57 static void ll_options(char *options, char **ost, char **mds)
58 {
59         char *this_char;
60         ENTRY;
61
62         if (!options) {
63                 EXIT;
64                 return;
65         }
66
67         for (this_char = strtok (options, ",");
68              this_char != NULL;
69              this_char = strtok (NULL, ",")) {
70                 CDEBUG(D_INFO, "this_char %s\n", this_char);
71                 if ( (!*ost && (*ost = ll_read_opt("ost", this_char)))||
72                      (!*mds && (*mds = ll_read_opt("mds", this_char))) )
73                         continue;
74         }
75         EXIT;
76 }
77
78 static struct super_block * ll_read_super(struct super_block *sb,
79                                           void *data, int silent)
80 {
81         struct inode *root = 0;
82         struct ll_sb_info *sbi;
83         char *ost = NULL;
84         char *mds = NULL;
85         int devno;
86         int err;
87         struct ll_fid rootfid;
88         __u64 last_committed, last_rcvd;
89         __u32 last_xid;
90         struct ptlrpc_request *request = NULL;
91         struct ll_inode_md md;
92
93         ENTRY;
94         MOD_INC_USE_COUNT;
95
96         OBD_ALLOC(sbi, sizeof(*sbi));
97         if (!sbi) {
98                 MOD_DEC_USE_COUNT;
99                 RETURN(NULL);
100         }
101
102         sb->u.generic_sbp = sbi;
103
104         ll_options(data, &ost, &mds);
105
106         if (!ost) {
107                 CERROR("no ost\n");
108                 GOTO(out_free, sb = NULL);
109         }
110
111         if (!mds) {
112                 CERROR("no mds\n");
113                 GOTO(out_free, sb = NULL);
114         }
115
116         devno = simple_strtoul(ost, NULL, 0);
117         if (devno >= MAX_OBD_DEVICES) {
118                 CERROR("devno of %s too high\n", ost);
119                 GOTO(out_free, sb = NULL);
120         }
121
122         err = obd_connect(&sbi->ll_osc_conn, &obd_dev[devno]);
123         if (err) {
124                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
125                 GOTO(out_free, sb = NULL);
126         }
127
128         devno = simple_strtoul(mds, NULL, 0);
129         if (devno >= MAX_OBD_DEVICES) {
130                 CERROR("devno of %s too high\n", mds);
131                 GOTO(out_free, sb = NULL);
132         }
133
134         err = obd_connect(&sbi->ll_mdc_conn, &obd_dev[devno]);
135         if (err) {
136                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
137                 GOTO(out_free, sb = NULL);
138         }
139
140 #if 0
141         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
142         if (err) {
143                 CERROR("cannot connect to MDS: rc = %d\n", err);
144                 GOTO(out_rpc, sb = NULL);
145         }
146 #endif 
147
148         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
149
150         /* XXX: need to store the last_* values somewhere */
151         err = mdc_getstatus(&sbi->ll_mdc_conn,
152                             &rootfid, &last_committed, 
153                             &last_rcvd, &last_xid,
154                             &request);
155         if (err) {
156                 CERROR("cannot mds_connect: rc = %d\n", err);
157                 GOTO(out_disc, sb = NULL);
158         }
159         CDEBUG(D_SUPER, "rootfid %ld\n", (unsigned long)rootfid.id);
160         sbi->ll_rootino = rootfid.id;
161
162         sb->s_maxbytes = 1ULL << 36;
163         /* XXX get this with a get_info call (like we have in OBDFS),
164            this info call should return the blocksize of the MDS (statfs?) */
165         sb->s_blocksize = 4096;
166         sb->s_blocksize_bits = 12;
167         sb->s_magic = LL_SUPER_MAGIC;
168         sb->s_op = &ll_super_operations;
169
170         /* make root inode */
171         err = mdc_getattr(&sbi->ll_mdc_conn,
172                           sbi->ll_rootino, S_IFDIR,
173                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
174         if (err) {
175                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
176                 GOTO(out_mdc, sb = NULL);
177         }
178
179         /* initialize committed transaction callback daemon */
180         spin_lock_init(&sbi->ll_commitcbd_lock); 
181         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
182         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
183         sbi->ll_commitcbd_flags = 0;
184         err = ll_commitcbd_setup(sbi);
185         if (err) {
186                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
187                 GOTO(out_mdc, sb = NULL);
188         }
189
190         md.body = lustre_msg_buf(request->rq_repmsg, 0);
191         md.obdo = NULL;
192         root = iget4(sb, sbi->ll_rootino, NULL, &md);
193                      
194         if (root) {
195                 sb->s_root = d_alloc_root(root);
196         } else {
197                 CERROR("lustre_lite: bad iget4 for root\n");
198                 GOTO(out_cdb, sb = NULL);
199         }
200
201         ptlrpc_free_req(request);
202
203 out_dev:
204         if (mds)
205                 OBD_FREE(mds, strlen(mds) + 1);
206         if (ost)
207                 OBD_FREE(ost, strlen(ost) + 1);
208
209         RETURN(sb);
210
211 out_cdb:
212         ll_commitcbd_cleanup(sbi);
213 out_mdc:
214         obd_disconnect(&sbi->ll_mdc_conn);
215         obd_disconnect(&sbi->ll_osc_conn);
216 out_disc:
217         ptlrpc_free_req(request);
218         obd_disconnect(&sbi->ll_osc_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 obdo *oa = ll_i2info(inode)->lli_obdo;
243                 if (oa) {
244                         obdo_free(oa);
245                         ll_i2info(inode)->lli_obdo = NULL;
246                 }
247                 if (ll_i2info(inode)->lli_symlink_name) {
248                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
249                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
250                         ll_i2info(inode)->lli_symlink_name = NULL;
251                 }
252         }
253 }
254
255 static void ll_delete_inode(struct inode *inode)
256 {
257         if (S_ISREG(inode->i_mode)) { 
258                 int err;
259                 struct obdo *oa;
260                 oa = ll_i2info(inode)->lli_obdo;
261
262                 if (!oa)
263                         GOTO(out, -EINVAL);
264
265                 if (oa->o_id == 0)
266                         /* No obdo was ever created */
267                         GOTO(out, 0);
268
269                 err = obd_destroy(ll_i2obdconn(inode), oa);
270                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
271                        (unsigned long long)oa->o_id, err);
272         }
273 out:
274         clear_inode(inode);
275 }
276
277 /* like inode_setattr, but doesn't mark the inode dirty */
278 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
279 {
280         unsigned int ia_valid = attr->ia_valid;
281         int error = 0;
282
283         if ((ia_valid & ATTR_SIZE) && trunc) {
284                 error = vmtruncate(inode, attr->ia_size);
285                 if (error)
286                         goto out;
287         } else if (ia_valid & ATTR_SIZE)
288                 inode->i_size = attr->ia_size;
289
290         if (ia_valid & ATTR_UID)
291                 inode->i_uid = attr->ia_uid;
292         if (ia_valid & ATTR_GID)
293                 inode->i_gid = attr->ia_gid;
294         if (ia_valid & ATTR_ATIME)
295                 inode->i_atime = attr->ia_atime;
296         if (ia_valid & ATTR_MTIME)
297                 inode->i_mtime = attr->ia_mtime;
298         if (ia_valid & ATTR_CTIME)
299                 inode->i_ctime = attr->ia_ctime;
300         if (ia_valid & ATTR_MODE) {
301                 inode->i_mode = attr->ia_mode;
302                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
303                         inode->i_mode &= ~S_ISGID;
304         }
305 out:
306         return error;
307 }
308
309 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
310 {
311         struct ptlrpc_request *request = NULL;
312         struct ll_sb_info *sbi = ll_i2sbi(inode);
313         int err;
314
315         ENTRY;
316
317         /* change incore inode */
318         ll_attr2inode(inode, attr, do_trunc);
319
320         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr,
321                           &request);
322         if (err)
323                 CERROR("mdc_setattr fails (%d)\n", err);
324
325         ptlrpc_req_finished(request);
326
327         RETURN(err);
328 }
329
330 int ll_setattr(struct dentry *de, struct iattr *attr)
331 {
332         int rc = inode_change_ok(de->d_inode, attr);
333
334         if (rc)
335                 return rc;
336
337         return ll_inode_setattr(de->d_inode, attr, 1);
338 }
339
340 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
341 {
342         struct ptlrpc_request *request = NULL;
343         struct ll_sb_info *sbi = ll_s2sbi(sb);
344         int err;
345         ENTRY;
346
347         memset(sfs, 0, sizeof(*sfs));
348         err = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
349         if (err)
350                 CERROR("obd_statfs fails (%d)\n", err);
351         else
352                 CDEBUG(D_SUPER,
353                        "statfs returns blocks %ld/%ld objects %ld/%ld\n",
354                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
355
356         ptlrpc_req_finished(request);
357
358         RETURN(err);
359 }
360
361 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
362 {
363         struct mds_body *body = md->body;
364         struct ll_inode_info *ii = ll_i2info(dst);
365
366         /* core attributes first */
367         if (body->valid & OBD_MD_FLID)
368                 dst->i_ino = body->ino;
369         if (body->valid & OBD_MD_FLATIME)
370                 dst->i_atime = body->atime;
371         if (body->valid & OBD_MD_FLMTIME)
372                 dst->i_mtime = body->mtime;
373         if (body->valid & OBD_MD_FLCTIME)
374                 dst->i_ctime = body->ctime;
375         if (body->valid & OBD_MD_FLSIZE)
376                 dst->i_size = body->size;
377         if (body->valid & OBD_MD_FLMODE)
378                 dst->i_mode = body->mode;
379         if (body->valid & OBD_MD_FLUID)
380                 dst->i_uid = body->uid;
381         if (body->valid & OBD_MD_FLGID)
382                 dst->i_gid = body->gid;
383         if (body->valid & OBD_MD_FLFLAGS)
384                 dst->i_flags = body->flags;
385         if (body->valid & OBD_MD_FLNLINK)
386                 dst->i_nlink = body->nlink;
387         if (body->valid & OBD_MD_FLGENER)
388                 dst->i_generation = body->generation;
389
390         /* this will become more elaborate for striping etc */
391         if (md->obdo != NULL && md->obdo->o_valid != 0) {
392                 ii->lli_obdo = obdo_alloc();
393                 memcpy(ii->lli_obdo, md->obdo, sizeof(*md->obdo));
394         }
395 #if 0
396
397         if (obdo_has_inline(oa)) {
398                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
399                     S_ISFIFO(inode->i_mode)) {
400                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
401                         CDEBUG(D_INODE,
402                                "copying device %x from obdo to inode\n", rdev);
403                         init_special_inode(inode, inode->i_mode, rdev);
404                 } else {
405                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
406                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
407                 }
408                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
409         }
410 #endif 
411 } /* ll_to_inode */
412
413 static void ll_read_inode2(struct inode *inode, void *opaque)
414 {
415         struct ll_inode_md *md = opaque;
416
417         ENTRY;
418         ll_to_inode(inode, md);
419
420         /* OIDEBUG(inode); */
421
422         if (S_ISREG(inode->i_mode)) {
423                 inode->i_op = &ll_file_inode_operations;
424                 inode->i_fop = &ll_file_operations;
425                 inode->i_mapping->a_ops = &ll_aops;
426                 EXIT;
427         } else if (S_ISDIR(inode->i_mode)) {
428                 inode->i_op = &ll_dir_inode_operations;
429                 inode->i_fop = &ll_dir_operations;
430                 inode->i_mapping->a_ops = &ll_dir_aops;
431                 EXIT;
432         } else if (S_ISLNK(inode->i_mode)) {
433                 inode->i_op = &ll_fast_symlink_inode_operations;
434                 EXIT;
435         } else {
436                 init_special_inode(inode, inode->i_mode,
437                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
438                 EXIT;
439         }
440
441         return;
442 }
443
444 /* exported operations */
445 struct super_operations ll_super_operations =
446 {
447         read_inode2: ll_read_inode2,
448         clear_inode: ll_clear_inode,
449         delete_inode: ll_delete_inode,
450         put_super: ll_put_super,
451         statfs: ll_statfs
452 };
453
454 struct file_system_type lustre_lite_fs_type = {
455         "lustre_lite", 0, ll_read_super, NULL
456 };
457
458 static int __init init_lustre_lite(void)
459 {
460         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
461         ll_file_data_slab = kmem_cache_create("ll_file_data",
462                                               sizeof(struct ll_file_data), 0,
463                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
464         if (ll_file_data_slab == NULL)
465                 return -ENOMEM;
466         return register_filesystem(&lustre_lite_fs_type);
467 }
468
469 static void __exit exit_lustre_lite(void)
470 {
471         unregister_filesystem(&lustre_lite_fs_type);
472         kmem_cache_destroy(ll_file_data_slab);
473 }
474
475 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
476 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
477 MODULE_LICENSE("GPL");
478
479 module_init(init_lustre_lite);
480 module_exit(exit_lustre_lite);