Whamcloud - gitweb
Fix cleanup in llite/super.c
[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/lustre_lite.h>
16 #include <linux/lustre_ha.h>
17 #include <linux/lustre_dlm.h>
18
19 kmem_cache_t *ll_file_data_slab;
20 extern struct address_space_operations ll_aops;
21 extern struct address_space_operations ll_dir_aops;
22 struct super_operations ll_super_operations;
23
24 extern int ll_recover(struct ptlrpc_client *);
25 extern int ll_commitcbd_setup(struct ll_sb_info *);
26 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
27
28 static char *ll_read_opt(const char *opt, char *data)
29 {
30         char *value;
31         char *retval;
32         ENTRY;
33
34         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
35         if ( strncmp(opt, data, strlen(opt)) )
36                 RETURN(NULL);
37         if ( (value = strchr(data, '=')) == NULL )
38                 RETURN(NULL);
39
40         value++;
41         OBD_ALLOC(retval, strlen(value) + 1);
42         if ( !retval ) {
43                 CERROR("out of memory!\n");
44                 RETURN(NULL);
45         }
46         
47         memcpy(retval, value, strlen(value)+1);
48         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
49         RETURN(retval);
50 }
51
52 static void ll_options(char *options, char **ost, char **mds)
53 {
54         char *this_char;
55         ENTRY;
56
57         if (!options) {
58                 EXIT;
59                 return;
60         }
61
62         for (this_char = strtok (options, ",");
63              this_char != NULL;
64              this_char = strtok (NULL, ",")) {
65                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
66                 if ( (!*ost && (*ost = ll_read_opt("ost", this_char)))||
67                      (!*mds && (*mds = ll_read_opt("mds", this_char))) )
68                         continue;
69         }
70         EXIT;
71 }
72
73 #ifndef log2
74 #define log2(n) ffz(~(n))
75 #endif
76
77 static struct super_block * ll_read_super(struct super_block *sb,
78                                           void *data, int silent)
79 {
80         struct inode *root = 0;
81         struct obd_device *obd;
82         struct ll_sb_info *sbi;
83         char *ost = NULL;
84         char *mds = NULL;
85         int err;
86         struct ll_fid rootfid;
87         struct statfs sfs;
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         obd = class_uuid2obd(mds); 
117         if (!obd) {
118                 CERROR("MDS %s: not setup or attached\n", mds);
119                 GOTO(out_free, sb = NULL);
120         }
121
122         /* First the MDS since an LOV requires an the MDC connection 
123            to find its descriptor */ 
124 #if 0
125         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
126         if (err) {
127                 CERROR("cannot connect to MDS: rc = %d\n", err);
128                 GOTO(out_rpc, sb = NULL);
129         }
130 #endif 
131
132         err = obd_connect(&sbi->ll_mdc_conn, obd);
133         if (err) {
134                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
135                 GOTO(out_free, sb = NULL);
136         }
137         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
138
139         /* now the OST, which could be an LOV */ 
140
141         obd = class_uuid2obd(ost);
142         if (!obd) {
143                 CERROR("OST %s: not setup or attached\n", ost);
144                 GOTO(out_mdc, sb = NULL);
145         }
146 #warning error: need to set obd->u.lov.mdc_connh from sbi->ll_mdc_conn
147         err = obd_connect(&sbi->ll_osc_conn, obd);
148         if (err) {
149                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
150                 GOTO(out_mdc, sb = NULL);
151         }
152
153         /* XXX: need to store the last_* values somewhere */
154         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid, &last_committed,
155                             &last_rcvd, &last_xid, &request);
156         if (err) {
157                 CERROR("cannot mds_connect: rc = %d\n", err);
158                 GOTO(out_request, sb = NULL);
159         }
160         CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
161         sbi->ll_rootino = rootfid.id;
162
163         memset(&sfs, 0, sizeof(sfs));
164         err = mdc_statfs(&sbi->ll_mdc_conn, &sfs, &request);
165         sb->s_blocksize = sfs.f_bsize;
166         sb->s_blocksize_bits = log2(sfs.f_bsize);
167         sb->s_magic = LL_SUPER_MAGIC;
168         sb->s_maxbytes = (1ULL << (32 + 9)) - sfs.f_bsize;
169         ptlrpc_req_finished(request);
170
171         sb->s_op = &ll_super_operations;
172
173         /* make root inode */
174         err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
175                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
176         if (err) {
177                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
178                 GOTO(out_request, sb = NULL);
179         }
180
181         /* initialize committed transaction callback daemon */
182         spin_lock_init(&sbi->ll_commitcbd_lock);
183         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
184         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
185         sbi->ll_commitcbd_flags = 0;
186         err = ll_commitcbd_setup(sbi);
187         if (err) {
188                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
189                 GOTO(out_request, sb = NULL);
190         }
191
192         md.body = lustre_msg_buf(request->rq_repmsg, 0);
193         md.md = NULL;
194         root = iget4(sb, sbi->ll_rootino, NULL, &md);
195
196         if (root) {
197                 sb->s_root = d_alloc_root(root);
198         } else {
199                 CERROR("lustre_lite: bad iget4 for root\n");
200                 GOTO(out_cdb, sb = NULL);
201         }
202
203         ptlrpc_free_req(request);
204
205 out_dev:
206         if (mds)
207                 OBD_FREE(mds, strlen(mds) + 1);
208         if (ost)
209                 OBD_FREE(ost, strlen(ost) + 1);
210
211         RETURN(sb);
212
213 out_cdb:
214         ll_commitcbd_cleanup(sbi);
215 out_request:
216         ptlrpc_free_req(request);
217         obd_disconnect(&sbi->ll_osc_conn);
218 out_mdc:
219         obd_disconnect(&sbi->ll_mdc_conn);
220 out_free:
221         OBD_FREE(sbi, sizeof(*sbi));
222
223         MOD_DEC_USE_COUNT;
224         goto out_dev;
225 } /* ll_read_super */
226
227 static void ll_put_super(struct super_block *sb)
228 {
229         struct ll_sb_info *sbi = ll_s2sbi(sb);
230         ENTRY;
231         ll_commitcbd_cleanup(sbi);
232         obd_disconnect(&sbi->ll_osc_conn);
233         obd_disconnect(&sbi->ll_mdc_conn);
234         OBD_FREE(sbi, sizeof(*sbi));
235
236         MOD_DEC_USE_COUNT;
237         EXIT;
238 } /* ll_put_super */
239
240 static void ll_clear_inode(struct inode *inode)
241 {
242         if (atomic_read(&inode->i_count) == 0) {
243                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
244                 if (md) {
245                         OBD_FREE(md, md->lmd_size); 
246                         ll_i2info(inode)->lli_smd = NULL;
247                 }
248                 if (ll_i2info(inode)->lli_symlink_name) {
249                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
250                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
251                         ll_i2info(inode)->lli_symlink_name = NULL;
252                 }
253         }
254 }
255
256 static void ll_delete_inode(struct inode *inode)
257 {
258         if (S_ISREG(inode->i_mode)) { 
259                 int err;
260                 struct obdo oa;
261                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
262  
263                if (!md)
264                         GOTO(out, -EINVAL);
265
266                 oa.o_id = md->lmd_object_id;
267                 oa.o_easize = md->lmd_size;
268                 if (oa.o_id == 0) { 
269                         CERROR("This really happens\n"); 
270                         /* No obdo was ever created */
271                         GOTO(out, 0);
272                 }
273
274                 err = obd_destroy(ll_i2obdconn(inode), &oa, md);
275                 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
276                        md->lmd_object_id, err);
277         }
278 out:
279         clear_inode(inode);
280 }
281
282 /* like inode_setattr, but doesn't mark the inode dirty */
283 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
284 {
285         unsigned int ia_valid = attr->ia_valid;
286         int error = 0;
287
288         if ((ia_valid & ATTR_SIZE) && trunc) {
289                 error = vmtruncate(inode, attr->ia_size);
290                 if (error)
291                         goto out;
292         } else if (ia_valid & ATTR_SIZE)
293                 inode->i_size = attr->ia_size;
294
295         if (ia_valid & ATTR_UID)
296                 inode->i_uid = attr->ia_uid;
297         if (ia_valid & ATTR_GID)
298                 inode->i_gid = attr->ia_gid;
299         if (ia_valid & ATTR_ATIME)
300                 inode->i_atime = attr->ia_atime;
301         if (ia_valid & ATTR_MTIME)
302                 inode->i_mtime = attr->ia_mtime;
303         if (ia_valid & ATTR_CTIME)
304                 inode->i_ctime = attr->ia_ctime;
305         if (ia_valid & ATTR_MODE) {
306                 inode->i_mode = attr->ia_mode;
307                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
308                         inode->i_mode &= ~S_ISGID;
309         }
310 out:
311         return error;
312 }
313
314 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
315 {
316         struct ptlrpc_request *request = NULL;
317         struct ll_sb_info *sbi = ll_i2sbi(inode);
318         int err;
319
320         ENTRY;
321
322         /* change incore inode */
323         ll_attr2inode(inode, attr, do_trunc);
324
325         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &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         int rc = inode_change_ok(de->d_inode, attr);
337
338         if (rc)
339                 return rc;
340
341         return ll_inode_setattr(de->d_inode, attr, 1);
342 }
343
344 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
345 {
346         struct ptlrpc_request *request = NULL;
347         struct ll_sb_info *sbi = ll_s2sbi(sb);
348         int rc;
349         ENTRY;
350
351         memset(sfs, 0, sizeof(*sfs));
352         rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
353         ptlrpc_req_finished(request);
354         if (rc)
355                 CERROR("obd_statfs fails: rc = %d\n", rc);
356         else
357                 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
358                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
359
360         /* temporary until mds_statfs returns statfs info for all OSTs */
361         if (!rc) {
362                 struct statfs obd_sfs;
363
364                 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
365                 if (rc) {
366                         CERROR("obd_statfs fails: rc = %d\n", rc);
367                         GOTO(out, rc);
368                 }
369                 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
370                        "objects %ld/%ld\n",
371                        obd_sfs.f_bavail, obd_sfs.f_blocks,
372                        obd_sfs.f_ffree, obd_sfs.f_files);
373
374                 sfs->f_bfree = obd_sfs.f_bfree;
375                 sfs->f_bavail = obd_sfs.f_bavail;
376                 sfs->f_blocks = obd_sfs.f_blocks;
377                 if (obd_sfs.f_ffree < sfs->f_ffree)
378                         sfs->f_ffree = obd_sfs.f_ffree;
379         }
380
381 out:
382         RETURN(rc);
383 }
384
385 inline int ll_stripe_md_size(struct super_block *sb)
386 {
387         struct mdc_obd *mdc = sbi2mdc(ll_s2sbi(sb));
388         return mdc->mdc_max_mdsize;
389 }
390
391 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
392 {
393         struct mds_body *body = md->body;
394         struct ll_inode_info *ii = ll_i2info(dst);
395
396         /* core attributes first */
397         if (body->valid & OBD_MD_FLID)
398                 dst->i_ino = body->ino;
399         if (body->valid & OBD_MD_FLATIME)
400                 dst->i_atime = body->atime;
401         if (body->valid & OBD_MD_FLMTIME)
402                 dst->i_mtime = body->mtime;
403         if (body->valid & OBD_MD_FLCTIME)
404                 dst->i_ctime = body->ctime;
405         if (body->valid & OBD_MD_FLSIZE)
406                 dst->i_size = body->size;
407         if (body->valid & OBD_MD_FLMODE)
408                 dst->i_mode = body->mode;
409         if (body->valid & OBD_MD_FLUID)
410                 dst->i_uid = body->uid;
411         if (body->valid & OBD_MD_FLGID)
412                 dst->i_gid = body->gid;
413         if (body->valid & OBD_MD_FLFLAGS)
414                 dst->i_flags = body->flags;
415         if (body->valid & OBD_MD_FLNLINK)
416                 dst->i_nlink = body->nlink;
417         if (body->valid & OBD_MD_FLGENER)
418                 dst->i_generation = body->generation;
419         if (body->valid & OBD_MD_FLRDEV)
420                 dst->i_rdev = body->extra;
421         if (md && md->md && md->md->lmd_stripe_count) { 
422                 struct lov_stripe_md *smd = md->md;
423                 int size = ll_stripe_md_size(dst->i_sb);
424                 if (md->md->lmd_size != size) { 
425                         CERROR("Striping metadata size error %ld\n",
426                                dst->i_ino); 
427                         LBUG();
428                 }
429                 OBD_ALLOC(ii->lli_smd, size);
430                 if (!ii->lli_smd){ 
431                         CERROR("No memory for %d\n", size);
432                         LBUG();
433                 }
434                 memcpy(ii->lli_smd, smd, size);
435         }
436 } /* ll_to_inode */
437
438 static void ll_read_inode2(struct inode *inode, void *opaque)
439 {
440         struct ll_inode_md *md = opaque;
441
442         ENTRY;
443         ll_to_inode(inode, md);
444
445         /* OIDEBUG(inode); */
446
447         if (S_ISREG(inode->i_mode)) {
448                 inode->i_op = &ll_file_inode_operations;
449                 inode->i_fop = &ll_file_operations;
450                 inode->i_mapping->a_ops = &ll_aops;
451                 EXIT;
452         } else if (S_ISDIR(inode->i_mode)) {
453                 inode->i_op = &ll_dir_inode_operations;
454                 inode->i_fop = &ll_dir_operations;
455                 inode->i_mapping->a_ops = &ll_dir_aops;
456                 EXIT;
457         } else if (S_ISLNK(inode->i_mode)) {
458                 inode->i_op = &ll_fast_symlink_inode_operations;
459                 EXIT;
460         } else {
461                 init_special_inode(inode, inode->i_mode,
462                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
463                 EXIT;
464         }
465
466         return;
467 }
468
469 /* exported operations */
470 struct super_operations ll_super_operations =
471 {
472         read_inode2: ll_read_inode2,
473         clear_inode: ll_clear_inode,
474         delete_inode: ll_delete_inode,
475         put_super: ll_put_super,
476         statfs: ll_statfs
477 };
478
479 struct file_system_type lustre_lite_fs_type = {
480         "lustre_lite", 0, ll_read_super, NULL
481 };
482
483 static int __init init_lustre_lite(void)
484 {
485         printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
486         ll_file_data_slab = kmem_cache_create("ll_file_data",
487                                               sizeof(struct ll_file_data), 0,
488                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
489         if (ll_file_data_slab == NULL)
490                 return -ENOMEM;
491         return register_filesystem(&lustre_lite_fs_type);
492 }
493
494 static void __exit exit_lustre_lite(void)
495 {
496         unregister_filesystem(&lustre_lite_fs_type);
497         kmem_cache_destroy(ll_file_data_slab);
498 }
499
500 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
501 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
502 MODULE_LICENSE("GPL");
503
504 module_init(init_lustre_lite);
505 module_exit(exit_lustre_lite);