Whamcloud - gitweb
Add statfs support for proper blocks count to head.
[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 #ifndef log2
79 #define log2(n) ffz(~(n))
80 #endif
81
82 static struct super_block * ll_read_super(struct super_block *sb,
83                                           void *data, int silent)
84 {
85         struct inode *root = 0;
86         struct ll_sb_info *sbi;
87         char *ost = NULL;
88         char *mds = NULL;
89         int devno;
90         int err;
91         struct ll_fid rootfid;
92         struct statfs sfs;
93         __u64 last_committed, last_rcvd;
94         __u32 last_xid;
95         struct ptlrpc_request *request = NULL;
96         struct ll_inode_md md;
97
98         ENTRY;
99         MOD_INC_USE_COUNT;
100
101         OBD_ALLOC(sbi, sizeof(*sbi));
102         if (!sbi) {
103                 MOD_DEC_USE_COUNT;
104                 RETURN(NULL);
105         }
106
107         sb->u.generic_sbp = sbi;
108
109         ll_options(data, &ost, &mds);
110
111         if (!ost) {
112                 CERROR("no ost\n");
113                 GOTO(out_free, sb = NULL);
114         }
115
116         if (!mds) {
117                 CERROR("no mds\n");
118                 GOTO(out_free, sb = NULL);
119         }
120
121         devno = simple_strtoul(mds, NULL, 0);
122         if (devno >= MAX_OBD_DEVICES) {
123                 CERROR("devno of %s too high\n", mds);
124                 GOTO(out_free, sb = NULL);
125         }
126
127         /* First the MDS since an LOV requires an the MDC connection 
128            to find its descriptor */ 
129 #if 0
130         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
131         if (err) {
132                 CERROR("cannot connect to MDS: rc = %d\n", err);
133                 GOTO(out_rpc, sb = NULL);
134         }
135 #endif 
136
137         err = obd_connect(&sbi->ll_mdc_conn, &obd_dev[devno]);
138         if (err) {
139                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
140                 GOTO(out_free, sb = NULL);
141         }
142         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
143
144         /* now the OST, which could be an LOV */ 
145         devno = simple_strtoul(ost, NULL, 0);
146         if (devno >= MAX_OBD_DEVICES) {
147                 CERROR("devno of %s too high\n", ost);
148                 GOTO(out_free, sb = NULL);
149         }
150         err = obd_connect(&sbi->ll_osc_conn, &obd_dev[devno]);
151         if (err) {
152                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
153                 GOTO(out_free, sb = NULL);
154         }
155
156         /* XXX: need to store the last_* values somewhere */
157         err = mdc_getstatus(&sbi->ll_mdc_conn,
158                             &rootfid, &last_committed, 
159                             &last_rcvd, &last_xid,
160                             &request);
161         if (err) {
162                 CERROR("cannot mds_connect: rc = %d\n", err);
163                 GOTO(out_disc, sb = NULL);
164         }
165         CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
166         sbi->ll_rootino = rootfid.id;
167
168         memset(&sfs, 0, sizeof(sfs));
169         err = mdc_statfs(&sbi->ll_mdc_conn, &sfs, &request);
170         sb->s_blocksize = sfs.f_bsize;
171         sb->s_blocksize_bits = log2(sfs.f_bsize);
172         sb->s_magic = LL_SUPER_MAGIC;
173         sb->s_maxbytes = (1ULL << (32 + 9)) - sfs.f_bsize;
174
175         sb->s_op = &ll_super_operations;
176
177         /* make root inode */
178         err = mdc_getattr(&sbi->ll_mdc_conn,
179                           sbi->ll_rootino, S_IFDIR,
180                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
181         if (err) {
182                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
183                 GOTO(out_mdc, sb = NULL);
184         }
185
186         /* initialize committed transaction callback daemon */
187         spin_lock_init(&sbi->ll_commitcbd_lock); 
188         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
189         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
190         sbi->ll_commitcbd_flags = 0;
191         err = ll_commitcbd_setup(sbi);
192         if (err) {
193                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
194                 GOTO(out_mdc, sb = NULL);
195         }
196
197         md.body = lustre_msg_buf(request->rq_repmsg, 0);
198         md.obdo = NULL;
199         root = iget4(sb, sbi->ll_rootino, NULL, &md);
200                      
201         if (root) {
202                 sb->s_root = d_alloc_root(root);
203         } else {
204                 CERROR("lustre_lite: bad iget4 for root\n");
205                 GOTO(out_cdb, sb = NULL);
206         }
207
208         ptlrpc_free_req(request);
209
210 out_dev:
211         if (mds)
212                 OBD_FREE(mds, strlen(mds) + 1);
213         if (ost)
214                 OBD_FREE(ost, strlen(ost) + 1);
215
216         RETURN(sb);
217
218 out_cdb:
219         ll_commitcbd_cleanup(sbi);
220 out_mdc:
221         obd_disconnect(&sbi->ll_mdc_conn);
222         obd_disconnect(&sbi->ll_osc_conn);
223 out_disc:
224         ptlrpc_free_req(request);
225         obd_disconnect(&sbi->ll_osc_conn);
226 out_free:
227         OBD_FREE(sbi, sizeof(*sbi));
228
229         MOD_DEC_USE_COUNT;
230         goto out_dev;
231 } /* ll_read_super */
232
233 static void ll_put_super(struct super_block *sb)
234 {
235         struct ll_sb_info *sbi = ll_s2sbi(sb);
236         ENTRY;
237         ll_commitcbd_cleanup(sbi);
238         obd_disconnect(&sbi->ll_osc_conn);
239         obd_disconnect(&sbi->ll_mdc_conn);
240         OBD_FREE(sbi, sizeof(*sbi));
241
242         MOD_DEC_USE_COUNT;
243         EXIT;
244 } /* ll_put_super */
245
246 static void ll_clear_inode(struct inode *inode)
247 {
248         if (atomic_read(&inode->i_count) == 0) {
249                 struct obdo *oa = ll_i2info(inode)->lli_obdo;
250                 if (oa) {
251                         obdo_free(oa);
252                         ll_i2info(inode)->lli_obdo = NULL;
253                 }
254                 if (ll_i2info(inode)->lli_symlink_name) {
255                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
256                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
257                         ll_i2info(inode)->lli_symlink_name = NULL;
258                 }
259         }
260 }
261
262 static void ll_delete_inode(struct inode *inode)
263 {
264         if (S_ISREG(inode->i_mode)) { 
265                 int err;
266                 struct obdo *oa;
267                 oa = ll_i2info(inode)->lli_obdo;
268
269                 if (!oa)
270                         GOTO(out, -EINVAL);
271
272                 if (oa->o_id == 0)
273                         /* No obdo was ever created */
274                         GOTO(out, 0);
275
276                 err = obd_destroy(ll_i2obdconn(inode), oa);
277                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
278                        (unsigned long long)oa->o_id, err);
279         }
280 out:
281         clear_inode(inode);
282 }
283
284 /* like inode_setattr, but doesn't mark the inode dirty */
285 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
286 {
287         unsigned int ia_valid = attr->ia_valid;
288         int error = 0;
289
290         if ((ia_valid & ATTR_SIZE) && trunc) {
291                 error = vmtruncate(inode, attr->ia_size);
292                 if (error)
293                         goto out;
294         } else if (ia_valid & ATTR_SIZE)
295                 inode->i_size = attr->ia_size;
296
297         if (ia_valid & ATTR_UID)
298                 inode->i_uid = attr->ia_uid;
299         if (ia_valid & ATTR_GID)
300                 inode->i_gid = attr->ia_gid;
301         if (ia_valid & ATTR_ATIME)
302                 inode->i_atime = attr->ia_atime;
303         if (ia_valid & ATTR_MTIME)
304                 inode->i_mtime = attr->ia_mtime;
305         if (ia_valid & ATTR_CTIME)
306                 inode->i_ctime = attr->ia_ctime;
307         if (ia_valid & ATTR_MODE) {
308                 inode->i_mode = attr->ia_mode;
309                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
310                         inode->i_mode &= ~S_ISGID;
311         }
312 out:
313         return error;
314 }
315
316 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
317 {
318         struct ptlrpc_request *request = NULL;
319         struct ll_sb_info *sbi = ll_i2sbi(inode);
320         int err;
321
322         ENTRY;
323
324         /* change incore inode */
325         ll_attr2inode(inode, attr, do_trunc);
326
327         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr,
328                           &request);
329         if (err)
330                 CERROR("mdc_setattr fails (%d)\n", err);
331
332         ptlrpc_req_finished(request);
333
334         RETURN(err);
335 }
336
337 int ll_setattr(struct dentry *de, struct iattr *attr)
338 {
339         int rc = inode_change_ok(de->d_inode, attr);
340
341         if (rc)
342                 return rc;
343
344         return ll_inode_setattr(de->d_inode, attr, 1);
345 }
346
347 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
348 {
349         struct ptlrpc_request *request = NULL;
350         struct statfs obd_sfs;
351         struct ll_sb_info *sbi = ll_s2sbi(sb);
352         int rc;
353         ENTRY;
354
355         memset(sfs, 0, sizeof(*sfs));
356         rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
357         ptlrpc_req_finished(request);
358         if (rc) {
359                 CERROR("obd_statfs fails: rc = %d\n", rc);
360                 GOTO(out, rc);
361         }
362         CDEBUG(D_SUPER, "statfs returns blocks %ld/%ld objects %ld/%ld\n",
363                sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
364
365         /* temporary until mds_statfs returns statfs info for all OSTs */
366         rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
367         if (rc) {
368                 CERROR("obd_statfs fails: rc = %d\n", rc);
369                 GOTO(out, rc);
370         }
371         CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld\n",
372                obd_sfs.f_bavail, obd_sfs.f_blocks);
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
378 out:
379         RETURN(rc);
380 }
381
382 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
383 {
384         struct mds_body *body = md->body;
385         struct ll_inode_info *ii = ll_i2info(dst);
386
387         /* core attributes first */
388         if (body->valid & OBD_MD_FLID)
389                 dst->i_ino = body->ino;
390         if (body->valid & OBD_MD_FLATIME)
391                 dst->i_atime = body->atime;
392         if (body->valid & OBD_MD_FLMTIME)
393                 dst->i_mtime = body->mtime;
394         if (body->valid & OBD_MD_FLCTIME)
395                 dst->i_ctime = body->ctime;
396         if (body->valid & OBD_MD_FLSIZE)
397                 dst->i_size = body->size;
398         if (body->valid & OBD_MD_FLMODE)
399                 dst->i_mode = body->mode;
400         if (body->valid & OBD_MD_FLUID)
401                 dst->i_uid = body->uid;
402         if (body->valid & OBD_MD_FLGID)
403                 dst->i_gid = body->gid;
404         if (body->valid & OBD_MD_FLFLAGS)
405                 dst->i_flags = body->flags;
406         if (body->valid & OBD_MD_FLNLINK)
407                 dst->i_nlink = body->nlink;
408         if (body->valid & OBD_MD_FLGENER)
409                 dst->i_generation = body->generation;
410
411         /* this will become more elaborate for striping etc */
412         if (md->obdo != NULL && md->obdo->o_valid != 0) {
413                 ii->lli_obdo = obdo_alloc();
414                 memcpy(ii->lli_obdo, md->obdo, sizeof(*md->obdo));
415         }
416 #if 0
417
418         if (obdo_has_inline(oa)) {
419                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
420                     S_ISFIFO(inode->i_mode)) {
421                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
422                         CDEBUG(D_INODE,
423                                "copying device %x from obdo to inode\n", rdev);
424                         init_special_inode(inode, inode->i_mode, rdev);
425                 } else {
426                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
427                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
428                 }
429                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
430         }
431 #endif 
432 } /* ll_to_inode */
433
434 static void ll_read_inode2(struct inode *inode, void *opaque)
435 {
436         struct ll_inode_md *md = opaque;
437
438         ENTRY;
439         ll_to_inode(inode, md);
440
441         /* OIDEBUG(inode); */
442
443         if (S_ISREG(inode->i_mode)) {
444                 inode->i_op = &ll_file_inode_operations;
445                 inode->i_fop = &ll_file_operations;
446                 inode->i_mapping->a_ops = &ll_aops;
447                 EXIT;
448         } else if (S_ISDIR(inode->i_mode)) {
449                 inode->i_op = &ll_dir_inode_operations;
450                 inode->i_fop = &ll_dir_operations;
451                 inode->i_mapping->a_ops = &ll_dir_aops;
452                 EXIT;
453         } else if (S_ISLNK(inode->i_mode)) {
454                 inode->i_op = &ll_fast_symlink_inode_operations;
455                 EXIT;
456         } else {
457                 init_special_inode(inode, inode->i_mode,
458                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
459                 EXIT;
460         }
461
462         return;
463 }
464
465 /* exported operations */
466 struct super_operations ll_super_operations =
467 {
468         read_inode2: ll_read_inode2,
469         clear_inode: ll_clear_inode,
470         delete_inode: ll_delete_inode,
471         put_super: ll_put_super,
472         statfs: ll_statfs
473 };
474
475 struct file_system_type lustre_lite_fs_type = {
476         "lustre_lite", 0, ll_read_super, NULL
477 };
478
479 static int __init init_lustre_lite(void)
480 {
481         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
482         ll_file_data_slab = kmem_cache_create("ll_file_data",
483                                               sizeof(struct ll_file_data), 0,
484                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
485         if (ll_file_data_slab == NULL)
486                 return -ENOMEM;
487         return register_filesystem(&lustre_lite_fs_type);
488 }
489
490 static void __exit exit_lustre_lite(void)
491 {
492         unregister_filesystem(&lustre_lite_fs_type);
493         kmem_cache_destroy(ll_file_data_slab);
494 }
495
496 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
497 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
498 MODULE_LICENSE("GPL");
499
500 module_init(init_lustre_lite);
501 module_exit(exit_lustre_lite);