Whamcloud - gitweb
Clean up in the right order on the failure path.
[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
92         ENTRY;
93         MOD_INC_USE_COUNT;
94
95         OBD_ALLOC(sbi, sizeof(*sbi));
96         if (!sbi) {
97                 MOD_DEC_USE_COUNT;
98                 RETURN(NULL);
99         }
100
101         sb->u.generic_sbp = sbi;
102
103         ll_options(data, &device, &version);
104
105         if (!device) {
106                 CERROR("no device\n");
107                 GOTO(out_free, sb = NULL);
108         }
109
110         devno = simple_strtoul(device, NULL, 0);
111         if (devno >= MAX_OBD_DEVICES) {
112                 CERROR("device of %s too high\n", device);
113                 GOTO(out_free, sb = NULL);
114         }
115
116         sbi->ll_conn.oc_dev = &obd_dev[devno];
117         err = obd_connect(&sbi->ll_conn);
118         if (err) {
119                 CERROR("cannot connect to %s: rc = %d\n", device, err);
120                 GOTO(out_free, sb = NULL);
121         }
122
123         sbi->ll_namespace = ldlm_namespace_new(NULL, 1);
124         if (sbi->ll_namespace == NULL) {
125                 CERROR("failed to create local lock namespace\n");
126                 GOTO(out_obd, sb = NULL);
127         }
128
129         ptlrpc_init_client(ptlrpc_connmgr, ll_recover,
130                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
131                            &sbi->ll_mds_client);
132
133         sbi->ll_mds_client.cli_data = sbi;
134         sbi->ll_mds_client.cli_name = "mdc";
135         sbi->ll_mds_conn = ptlrpc_uuid_to_connection("mds");
136         if (!sbi->ll_mds_conn) {
137                 CERROR("cannot find MDS\n");
138                 GOTO(out_ldlm, sb = NULL);
139         }
140
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
147         sbi->ll_mds_conn->c_level = LUSTRE_CONN_FULL;
148
149         /* XXX: need to store the last_* values somewhere */
150         err = mdc_connect(&sbi->ll_mds_client, sbi->ll_mds_conn,
151                           &rootfid, &last_committed, &last_rcvd, &last_xid,
152                           &request);
153         if (err) {
154                 CERROR("cannot mds_connect: rc = %d\n", err);
155                 GOTO(out_disc, sb = NULL);
156         }
157         CDEBUG(D_SUPER, "rootfid %ld\n", (unsigned long)rootfid.id);
158         sbi->ll_rootino = rootfid.id;
159
160         sb->s_maxbytes = 1ULL << 36;
161         /* XXX get this with a get_info call (like we have in OBDFS),
162            this info call should return the blocksize of the MDS */
163         sb->s_blocksize = 4096;
164         sb->s_blocksize_bits = 12;
165         sb->s_magic = LL_SUPER_MAGIC;
166         sb->s_op = &ll_super_operations;
167
168         /* make root inode */
169         err = mdc_getattr(&sbi->ll_mds_client, sbi->ll_mds_conn,
170                           sbi->ll_rootino, S_IFDIR,
171                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
172         if (err) {
173                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
174                 GOTO(out_mdc, sb = NULL);
175         }
176
177         /* initialize committed transaction callback daemon */
178         spin_lock_init(&sbi->ll_commitcbd_lock); 
179         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
180         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
181         sbi->ll_commitcbd_flags = 0;
182         err = ll_commitcbd_setup(sbi);
183         if (err) {
184                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
185                 GOTO(out_mdc, sb = NULL);
186         }
187
188         root = iget4(sb, sbi->ll_rootino, NULL,
189                      lustre_msg_buf(request->rq_repmsg, 0));
190         if (root) {
191                 sb->s_root = d_alloc_root(root);
192         } else {
193                 CERROR("lustre_lite: bad iget4 for root\n");
194                 GOTO(out_cdb, sb = NULL);
195         }
196
197         ptlrpc_free_req(request);
198
199 out_dev:
200         if (device)
201                 OBD_FREE(device, strlen(device) + 1);
202         if (version)
203                 OBD_FREE(version, strlen(version) + 1);
204
205         RETURN(sb);
206
207 out_cdb:
208         ll_commitcbd_cleanup(sbi);
209 out_mdc:
210         ptlrpc_cleanup_client(&sbi->ll_mds_client);
211 out_disc:
212         ptlrpc_free_req(request);
213 out_rpc:
214         ptlrpc_put_connection(sbi->ll_mds_conn);
215 out_ldlm:
216         ldlm_namespace_free(sbi->ll_namespace);
217 out_obd:
218         obd_disconnect(&sbi->ll_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 = sb->u.generic_sbp;
229         ENTRY;
230         ll_commitcbd_cleanup(sbi);
231         ptlrpc_cleanup_client(&sbi->ll_mds_client);
232         ptlrpc_put_connection(sbi->ll_mds_conn);
233         ldlm_namespace_free(sbi->ll_namespace);
234         obd_disconnect(&sbi->ll_conn);
235         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
236         MOD_DEC_USE_COUNT;
237         EXIT;
238 } /* ll_put_super */
239
240
241 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
242
243 static void ll_delete_inode(struct inode *inode)
244 {
245         if (S_ISREG(inode->i_mode)) {
246                 int err;
247                 struct obdo *oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
248
249                 if (!oa) {
250                         CERROR("no memory\n");
251                         GOTO(out, -ENOMEM);
252                 }
253
254                 err = obd_destroy(ll_i2obdconn(inode), oa);
255                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
256                        (unsigned long long)oa->o_id, err);
257                 obdo_free(oa);
258         }
259 out:
260         clear_inode(inode);
261 }
262
263 /* like inode_setattr, but doesn't mark the inode dirty */ 
264 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
265 {
266         unsigned int ia_valid = attr->ia_valid;
267         int error = 0;
268
269         if ((ia_valid & ATTR_SIZE) && trunc ) {
270                 error = vmtruncate(inode, attr->ia_size);
271                 if (error)
272                         goto out;
273         } else if (ia_valid & ATTR_SIZE) { 
274                 inode->i_size = attr->ia_size;
275         }               
276
277         if (ia_valid & ATTR_UID)
278                 inode->i_uid = attr->ia_uid;
279         if (ia_valid & ATTR_GID)
280                 inode->i_gid = attr->ia_gid;
281         if (ia_valid & ATTR_ATIME)
282                 inode->i_atime = attr->ia_atime;
283         if (ia_valid & ATTR_MTIME)
284                 inode->i_mtime = attr->ia_mtime;
285         if (ia_valid & ATTR_CTIME)
286                 inode->i_ctime = attr->ia_ctime;
287         if (ia_valid & ATTR_MODE) {
288                 inode->i_mode = attr->ia_mode;
289                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
290                         inode->i_mode &= ~S_ISGID;
291         }
292 out:
293         return error;
294 }
295
296 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
297 {
298         struct ptlrpc_request *request = NULL;
299         struct ll_sb_info *sbi = ll_i2sbi(inode);
300         int err;
301
302         ENTRY;
303
304         /* change incore inode */
305         ll_attr2inode(inode, attr, do_trunc);
306
307         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
308                           &request);
309         if (err)
310                 CERROR("mdc_setattr fails (%d)\n", err);
311
312         ptlrpc_req_finished(request);
313
314         RETURN(err);
315 }
316
317 int ll_setattr(struct dentry *de, struct iattr *attr)
318 {
319         return ll_inode_setattr(de->d_inode, attr, 1);
320 }
321
322 static int ll_statfs(struct super_block *sb, struct statfs *buf)
323 {
324         struct statfs tmp;
325         int err;
326         ENTRY;
327
328         err = obd_statfs(&ll_s2sbi(sb)->ll_conn, &tmp);
329         if (err) {
330                 CERROR("obd_statfs fails (%d)\n", err);
331                 RETURN(err);
332         }
333         memcpy(buf, &tmp, sizeof(*buf));
334         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
335
336         RETURN(err);
337 }
338
339 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
340 {
341         struct ll_inode_info *ii = 
342                 (struct ll_inode_info *) &dst->u.generic_ip;
343
344         /* core attributes first */
345         if ( body->valid & OBD_MD_FLID )
346                 dst->i_ino = body->ino;
347         if ( body->valid & OBD_MD_FLATIME ) 
348                 dst->i_atime = body->atime;
349         if ( body->valid & OBD_MD_FLMTIME ) 
350                 dst->i_mtime = body->mtime;
351         if ( body->valid & OBD_MD_FLCTIME ) 
352                 dst->i_ctime = body->ctime;
353         if ( body->valid & OBD_MD_FLSIZE ) 
354                 dst->i_size = body->size;
355         if ( body->valid & OBD_MD_FLMODE ) 
356                 dst->i_mode = body->mode;
357         if ( body->valid & OBD_MD_FLUID ) 
358                 dst->i_uid = body->uid;
359         if ( body->valid & OBD_MD_FLGID ) 
360                 dst->i_gid = body->gid;
361         if ( body->valid & OBD_MD_FLFLAGS ) 
362                 dst->i_flags = body->flags;
363         if ( body->valid & OBD_MD_FLNLINK )
364                 dst->i_nlink = body->nlink;
365         if ( body->valid & OBD_MD_FLGENER )
366                 dst->i_generation = body->generation;
367
368         /* this will become more elaborate for striping etc */ 
369         if (body->valid & OBD_MD_FLOBJID) 
370                 ii->lli_objid = body->objid;
371 #if 0
372
373         if (obdo_has_inline(oa)) {
374                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
375                     S_ISFIFO(inode->i_mode)) {
376                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
377                         CDEBUG(D_INODE,
378                                "copying device %x from obdo to inode\n", rdev);
379                         init_special_inode(inode, inode->i_mode, rdev);
380                 } else {
381                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
382                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
383                 }
384                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
385         }
386 #endif 
387 } /* ll_to_inode */
388
389 static inline void ll_read_inode2(struct inode *inode, void *opaque)
390 {
391         struct mds_body *body = opaque; 
392         
393         ENTRY;
394         ll_to_inode(inode, body); 
395
396         /* OIDEBUG(inode); */
397
398         if (S_ISREG(inode->i_mode)) {
399                 inode->i_op = &ll_file_inode_operations;
400                 inode->i_fop = &ll_file_operations;
401                 inode->i_mapping->a_ops = &ll_aops;
402                 EXIT;
403         } else if (S_ISDIR(inode->i_mode)) {
404                 inode->i_op = &ll_dir_inode_operations;
405                 inode->i_fop = &ll_dir_operations; 
406                 inode->i_mapping->a_ops = &ll_dir_aops;
407                 EXIT;
408         } else if (S_ISLNK(inode->i_mode)) {
409                 inode->i_op = &ll_fast_symlink_inode_operations;
410                 EXIT;
411         } else {
412                 init_special_inode(inode, inode->i_mode,
413                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
414                 EXIT;
415         }
416
417         return;
418 }
419
420 /* exported operations */
421 struct super_operations ll_super_operations =
422 {
423         read_inode2: ll_read_inode2,
424         delete_inode: ll_delete_inode,
425         put_super: ll_put_super,
426         statfs: ll_statfs
427 };
428
429 struct file_system_type lustre_lite_fs_type = {
430         "lustre_lite", 0, ll_read_super, NULL
431 };
432
433 static int __init init_lustre_lite(void)
434 {
435         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
436         ll_file_data_slab = kmem_cache_create("ll_file_data",
437                                               sizeof(struct ll_file_data), 0,
438                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
439         if (ll_file_data_slab == NULL)
440                 return -ENOMEM;
441         return register_filesystem(&lustre_lite_fs_type);
442 }
443
444 static void __exit exit_lustre_lite(void)
445 {
446         unregister_filesystem(&lustre_lite_fs_type);
447         kmem_cache_destroy(ll_file_data_slab);
448 }
449
450 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
451 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
452 MODULE_LICENSE("GPL");
453
454 module_init(init_lustre_lite);
455 module_exit(exit_lustre_lite);