Whamcloud - gitweb
WARNING: This commit breaks everything. It will be back in shape within 12
[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 **dev, char **vers)
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 ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
72                      (!*vers && (*vers = ll_read_opt("version", 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 *device = NULL;
84         char *version = 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, &device, &version);
105
106         if (!device) {
107                 CERROR("no device\n");
108                 GOTO(out_free, sb = NULL);
109         }
110
111         devno = simple_strtoul(device, NULL, 0);
112         if (devno >= MAX_OBD_DEVICES) {
113                 CERROR("device of %s too high\n", device);
114                 GOTO(out_free, sb = NULL);
115         }
116
117         sbi->ll_conn.oc_dev = &obd_dev[devno];
118         err = obd_connect(&sbi->ll_conn);
119         if (err) {
120                 CERROR("cannot connect to %s: rc = %d\n", device, err);
121                 GOTO(out_free, sb = NULL);
122         }
123
124         sbi->ll_namespace = ldlm_namespace_new(NULL, 1);
125         if (sbi->ll_namespace == NULL) {
126                 CERROR("failed to create local lock namespace\n");
127                 GOTO(out_obd, sb = NULL);
128         }
129
130         ptlrpc_init_client(ptlrpc_connmgr, ll_recover,
131                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
132                            &sbi->ll_mds_client);
133
134         sbi->ll_mds_client.cli_data = sbi;
135         sbi->ll_mds_client.cli_name = "mdc";
136         sbi->ll_mds_conn = ptlrpc_uuid_to_connection("mds");
137         if (!sbi->ll_mds_conn) {
138                 CERROR("cannot find MDS\n");
139                 GOTO(out_ldlm, sb = NULL);
140         }
141
142         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
143         if (err) {
144                 CERROR("cannot connect to MDS: rc = %d\n", err);
145                 GOTO(out_rpc, sb = NULL);
146         }
147
148         sbi->ll_mds_conn->c_level = LUSTRE_CONN_FULL;
149
150         /* XXX: need to store the last_* values somewhere */
151         err = mdc_connect(&sbi->ll_mds_client, sbi->ll_mds_conn,
152                           &rootfid, &last_committed, &last_rcvd, &last_xid,
153                           &request);
154         if (err) {
155                 CERROR("cannot mds_connect: rc = %d\n", err);
156                 GOTO(out_disc, sb = NULL);
157         }
158         CDEBUG(D_SUPER, "rootfid %ld\n", (unsigned long)rootfid.id);
159         sbi->ll_rootino = rootfid.id;
160
161         sb->s_maxbytes = 1ULL << 36;
162         /* XXX get this with a get_info call (like we have in OBDFS),
163            this info call should return the blocksize of the MDS */
164         sb->s_blocksize = 4096;
165         sb->s_blocksize_bits = 12;
166         sb->s_magic = LL_SUPER_MAGIC;
167         sb->s_op = &ll_super_operations;
168
169         /* make root inode */
170         err = mdc_getattr(&sbi->ll_mds_client, sbi->ll_mds_conn,
171                           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_mdc, 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_mdc, sb = NULL);
187         }
188
189         md.body = lustre_msg_buf(request->rq_repmsg, 0);
190         md.obdo = 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_free_req(request);
201
202 out_dev:
203         if (device)
204                 OBD_FREE(device, strlen(device) + 1);
205         if (version)
206                 OBD_FREE(version, strlen(version) + 1);
207
208         RETURN(sb);
209
210 out_cdb:
211         ll_commitcbd_cleanup(sbi);
212 out_mdc:
213         ptlrpc_cleanup_client(&sbi->ll_mds_client);
214 out_disc:
215         ptlrpc_free_req(request);
216 out_rpc:
217         ptlrpc_put_connection(sbi->ll_mds_conn);
218 out_ldlm:
219         ldlm_namespace_free(sbi->ll_namespace);
220 out_obd:
221         obd_disconnect(&sbi->ll_conn);
222 out_free:
223         OBD_FREE(sbi, sizeof(*sbi));
224
225         MOD_DEC_USE_COUNT;
226         goto out_dev;
227 } /* ll_read_super */
228
229 static void ll_put_super(struct super_block *sb)
230 {
231         struct ll_sb_info *sbi = ll_s2sbi(sb);
232         ENTRY;
233         ll_commitcbd_cleanup(sbi);
234         ptlrpc_cleanup_client(&sbi->ll_mds_client);
235         ptlrpc_put_connection(sbi->ll_mds_conn);
236         ldlm_namespace_free(sbi->ll_namespace);
237         obd_disconnect(&sbi->ll_conn);
238         OBD_FREE(sbi, sizeof(*sbi));
239         MOD_DEC_USE_COUNT;
240         EXIT;
241 } /* ll_put_super */
242
243 static void ll_clear_inode(struct inode *inode)
244 {
245         if (atomic_read(&inode->i_count) == 0) {
246                 struct obdo *oa = ll_i2info(inode)->lli_obdo;
247                 if (oa) {
248                         obdo_free(oa);
249                         ll_i2info(inode)->lli_obdo = NULL;
250                 }
251                 if (ll_i2info(inode)->lli_symlink_name) {
252                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
253                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
254                         ll_i2info(inode)->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                 oa = ll_i2info(inode)->lli_obdo;
265
266                 if (!oa) {
267                         CERROR("no memory\n");
268                         GOTO(out, -ENOMEM);
269                 }
270
271                 err = obd_destroy(ll_i2obdconn(inode), oa);
272                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
273                        (unsigned long long)oa->o_id, err);
274                 obdo_free(oa);
275         }
276 out:
277         clear_inode(inode);
278 }
279
280 /* like inode_setattr, but doesn't mark the inode dirty */ 
281 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
282 {
283         unsigned int ia_valid = attr->ia_valid;
284         int error = 0;
285
286         if ((ia_valid & ATTR_SIZE) && trunc ) {
287                 error = vmtruncate(inode, attr->ia_size);
288                 if (error)
289                         goto out;
290         } else if (ia_valid & ATTR_SIZE) { 
291                 inode->i_size = attr->ia_size;
292         }               
293
294         if (ia_valid & ATTR_UID)
295                 inode->i_uid = attr->ia_uid;
296         if (ia_valid & ATTR_GID)
297                 inode->i_gid = attr->ia_gid;
298         if (ia_valid & ATTR_ATIME)
299                 inode->i_atime = attr->ia_atime;
300         if (ia_valid & ATTR_MTIME)
301                 inode->i_mtime = attr->ia_mtime;
302         if (ia_valid & ATTR_CTIME)
303                 inode->i_ctime = attr->ia_ctime;
304         if (ia_valid & ATTR_MODE) {
305                 inode->i_mode = attr->ia_mode;
306                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
307                         inode->i_mode &= ~S_ISGID;
308         }
309 out:
310         return error;
311 }
312
313 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
314 {
315         struct ptlrpc_request *request = NULL;
316         struct ll_sb_info *sbi = ll_i2sbi(inode);
317         int err;
318
319         ENTRY;
320
321         /* change incore inode */
322         ll_attr2inode(inode, attr, do_trunc);
323
324         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
325                           &request);
326         if (err)
327                 CERROR("mdc_setattr fails (%d)\n", err);
328
329         ptlrpc_req_finished(request);
330
331         RETURN(err);
332 }
333
334 int ll_setattr(struct dentry *de, struct iattr *attr)
335 {
336         return ll_inode_setattr(de->d_inode, attr, 1);
337 }
338
339 static int ll_statfs(struct super_block *sb, struct statfs *buf)
340 {
341         struct statfs tmp;
342         int err;
343         ENTRY;
344
345         err = obd_statfs(&ll_s2sbi(sb)->ll_conn, &tmp);
346         if (err) {
347                 CERROR("obd_statfs fails (%d)\n", err);
348                 RETURN(err);
349         }
350         memcpy(buf, &tmp, sizeof(*buf));
351 #if 0
352         err = mdc_statfs(&sbi->ll_mds_client, sbi->ll_mds_conn, &tmp,
353                           &request);
354         if (err) {
355                 CERROR("obd_statfs fails (%d)\n", err);
356                 RETURN(err);
357         }
358         if (tmp.f_files < buf->f_files)
359                 buf->f_files = tmp.f_files;
360         if (tmp.f_ffree < buf->f_ffree)
361                 buf->f_ffree = tmp.f_ffree;
362         buf->f_namelen = tmp.f_namelen;
363 #endif
364         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
365
366         RETURN(err);
367 }
368
369 static void inline ll_to_inode(struct inode *dst, struct ll_inode_md *md)
370 {
371         struct mds_body *body = md->body;
372         struct ll_inode_info *ii = ll_i2info(dst);
373
374         /* core attributes first */
375         if ( body->valid & OBD_MD_FLID )
376                 dst->i_ino = body->ino;
377         if ( body->valid & OBD_MD_FLATIME ) 
378                 dst->i_atime = body->atime;
379         if ( body->valid & OBD_MD_FLMTIME ) 
380                 dst->i_mtime = body->mtime;
381         if ( body->valid & OBD_MD_FLCTIME ) 
382                 dst->i_ctime = body->ctime;
383         if ( body->valid & OBD_MD_FLSIZE ) 
384                 dst->i_size = body->size;
385         if ( body->valid & OBD_MD_FLMODE ) 
386                 dst->i_mode = body->mode;
387         if ( body->valid & OBD_MD_FLUID ) 
388                 dst->i_uid = body->uid;
389         if ( body->valid & OBD_MD_FLGID ) 
390                 dst->i_gid = body->gid;
391         if ( body->valid & OBD_MD_FLFLAGS ) 
392                 dst->i_flags = body->flags;
393         if ( body->valid & OBD_MD_FLNLINK )
394                 dst->i_nlink = body->nlink;
395         if ( body->valid & OBD_MD_FLGENER )
396                 dst->i_generation = body->generation;
397
398         /* this will become more elaborate for striping etc */ 
399         if (md->obdo != NULL) {
400                 ii->lli_obdo = obdo_alloc();
401                 memcpy(ii->lli_obdo, md->obdo, sizeof(*md->obdo));
402         }
403 #if 0
404
405         if (obdo_has_inline(oa)) {
406                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
407                     S_ISFIFO(inode->i_mode)) {
408                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
409                         CDEBUG(D_INODE,
410                                "copying device %x from obdo to inode\n", rdev);
411                         init_special_inode(inode, inode->i_mode, rdev);
412                 } else {
413                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
414                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
415                 }
416                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
417         }
418 #endif 
419 } /* ll_to_inode */
420
421 static inline void ll_read_inode2(struct inode *inode, void *opaque)
422 {
423         struct ll_inode_md *md = opaque;
424         
425         ENTRY;
426         ll_to_inode(inode, md);
427
428         /* OIDEBUG(inode); */
429
430         if (S_ISREG(inode->i_mode)) {
431                 inode->i_op = &ll_file_inode_operations;
432                 inode->i_fop = &ll_file_operations;
433                 inode->i_mapping->a_ops = &ll_aops;
434                 EXIT;
435         } else if (S_ISDIR(inode->i_mode)) {
436                 inode->i_op = &ll_dir_inode_operations;
437                 inode->i_fop = &ll_dir_operations; 
438                 inode->i_mapping->a_ops = &ll_dir_aops;
439                 EXIT;
440         } else if (S_ISLNK(inode->i_mode)) {
441                 inode->i_op = &ll_fast_symlink_inode_operations;
442                 EXIT;
443         } else {
444                 init_special_inode(inode, inode->i_mode,
445                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
446                 EXIT;
447         }
448
449         return;
450 }
451
452 /* exported operations */
453 struct super_operations ll_super_operations =
454 {
455         read_inode2: ll_read_inode2,
456         clear_inode: ll_clear_inode,
457         delete_inode: ll_delete_inode,
458         put_super: ll_put_super,
459         statfs: ll_statfs
460 };
461
462 struct file_system_type lustre_lite_fs_type = {
463         "lustre_lite", 0, ll_read_super, NULL
464 };
465
466 static int __init init_lustre_lite(void)
467 {
468         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
469         ll_file_data_slab = kmem_cache_create("ll_file_data",
470                                               sizeof(struct ll_file_data), 0,
471                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
472         if (ll_file_data_slab == NULL)
473                 return -ENOMEM;
474         return register_filesystem(&lustre_lite_fs_type);
475 }
476
477 static void __exit exit_lustre_lite(void)
478 {
479         unregister_filesystem(&lustre_lite_fs_type);
480         kmem_cache_destroy(ll_file_data_slab);
481 }
482
483 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
484 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
485 MODULE_LICENSE("GPL");
486
487 module_init(init_lustre_lite);
488 module_exit(exit_lustre_lite);