Whamcloud - gitweb
- another attempt to get the blocksize right
[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_free, 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_disc, 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                 ptlrpc_put_connection(sbi->ll_mds_conn);
145                 GOTO(out_disc, 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         CERROR("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_req, 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_req, sb = NULL);
187         }
188
189         root = iget4(sb, sbi->ll_rootino, NULL,
190                      lustre_msg_buf(request->rq_repmsg, 0));
191         if (root) {
192                 sb->s_root = d_alloc_root(root);
193         } else {
194                 CERROR("lustre_lite: bad iget4 for root\n");
195                 GOTO(out_req, sb = NULL);
196         }
197
198 out_req:
199         ptlrpc_free_req(request);
200         if (!sb) {
201 out_disc:
202                 obd_disconnect(&sbi->ll_conn);
203 out_free:
204                 MOD_DEC_USE_COUNT;
205                 if (sbi->ll_namespace)
206                         ldlm_namespace_free(sbi->ll_namespace);
207                 OBD_FREE(sbi, sizeof(*sbi));
208         }
209         if (device)
210                 OBD_FREE(device, strlen(device) + 1);
211         if (version)
212                 OBD_FREE(version, strlen(version) + 1);
213
214         RETURN(sb);
215 } /* ll_read_super */
216
217 static void ll_put_super(struct super_block *sb)
218 {
219         struct ll_sb_info *sbi = sb->u.generic_sbp;
220         ENTRY;
221         ll_commitcbd_cleanup(sbi);
222         obd_disconnect(&sbi->ll_conn);
223         ldlm_namespace_free(sbi->ll_namespace);
224         ptlrpc_put_connection(sbi->ll_mds_conn);
225         ptlrpc_cleanup_client(&sbi->ll_mds_client);
226         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
227         MOD_DEC_USE_COUNT;
228         EXIT;
229 } /* ll_put_super */
230
231
232 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
233 static void ll_delete_inode(struct inode *inode)
234 {
235         if (S_ISREG(inode->i_mode)) { 
236                 int err; 
237                 struct obdo *oa; 
238                 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
239                 if (!oa) { 
240                         CERROR("no memory\n"); 
241                 }
242
243                 err = obd_destroy(ll_i2obdconn(inode), oa); 
244                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
245                        (unsigned long long)oa->o_id, err);
246                 obdo_free(oa);
247         }
248
249         clear_inode(inode); 
250 }
251
252 /* like inode_setattr, but doesn't mark the inode dirty */ 
253 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
254 {
255         unsigned int ia_valid = attr->ia_valid;
256         int error = 0;
257
258         if ((ia_valid & ATTR_SIZE) && trunc ) {
259                 error = vmtruncate(inode, attr->ia_size);
260                 if (error)
261                         goto out;
262         } else if (ia_valid & ATTR_SIZE) { 
263                 inode->i_size = attr->ia_size;
264         }               
265
266         if (ia_valid & ATTR_UID)
267                 inode->i_uid = attr->ia_uid;
268         if (ia_valid & ATTR_GID)
269                 inode->i_gid = attr->ia_gid;
270         if (ia_valid & ATTR_ATIME)
271                 inode->i_atime = attr->ia_atime;
272         if (ia_valid & ATTR_MTIME)
273                 inode->i_mtime = attr->ia_mtime;
274         if (ia_valid & ATTR_CTIME)
275                 inode->i_ctime = attr->ia_ctime;
276         if (ia_valid & ATTR_MODE) {
277                 inode->i_mode = attr->ia_mode;
278                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
279                         inode->i_mode &= ~S_ISGID;
280         }
281 out:
282         return error;
283 }
284
285 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
286 {
287         struct ptlrpc_request *request = NULL;
288         struct ll_sb_info *sbi = ll_i2sbi(inode);
289         int err;
290
291         ENTRY;
292
293         /* change incore inode */
294         ll_attr2inode(inode, attr, do_trunc);
295
296         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
297                           &request);
298         if (err)
299                 CERROR("mdc_setattr fails (%d)\n", err);
300
301         ptlrpc_req_finished(request);
302
303         RETURN(err);
304 }
305
306 int ll_setattr(struct dentry *de, struct iattr *attr)
307 {
308         return ll_inode_setattr(de->d_inode, attr, 1);
309 }
310
311 static int ll_statfs(struct super_block *sb, struct statfs *buf)
312 {
313         struct statfs tmp;
314         int err;
315         ENTRY;
316
317         err = obd_statfs(&ll_s2sbi(sb)->ll_conn, &tmp);
318         if (err) {
319                 CERROR("obd_statfs fails (%d)\n", err);
320                 RETURN(err);
321         }
322         memcpy(buf, &tmp, sizeof(*buf));
323         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
324
325         RETURN(err);
326 }
327
328 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
329 {
330         struct ll_inode_info *ii = 
331                 (struct ll_inode_info *) &dst->u.generic_ip;
332
333         /* core attributes first */
334         if ( body->valid & OBD_MD_FLID )
335                 dst->i_ino = body->ino;
336         if ( body->valid & OBD_MD_FLATIME ) 
337                 dst->i_atime = body->atime;
338         if ( body->valid & OBD_MD_FLMTIME ) 
339                 dst->i_mtime = body->mtime;
340         if ( body->valid & OBD_MD_FLCTIME ) 
341                 dst->i_ctime = body->ctime;
342         if ( body->valid & OBD_MD_FLSIZE ) 
343                 dst->i_size = body->size;
344         if ( body->valid & OBD_MD_FLMODE ) 
345                 dst->i_mode = body->mode;
346         if ( body->valid & OBD_MD_FLUID ) 
347                 dst->i_uid = body->uid;
348         if ( body->valid & OBD_MD_FLGID ) 
349                 dst->i_gid = body->gid;
350         if ( body->valid & OBD_MD_FLFLAGS ) 
351                 dst->i_flags = body->flags;
352         if ( body->valid & OBD_MD_FLNLINK )
353                 dst->i_nlink = body->nlink;
354         if ( body->valid & OBD_MD_FLGENER )
355                 dst->i_generation = body->generation;
356
357         /* this will become more elaborate for striping etc */ 
358         if (body->valid & OBD_MD_FLOBJID) 
359                 ii->lli_objid = body->objid;
360 #if 0
361
362         if (obdo_has_inline(oa)) {
363                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
364                     S_ISFIFO(inode->i_mode)) {
365                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
366                         CDEBUG(D_INODE,
367                                "copying device %x from obdo to inode\n", rdev);
368                         init_special_inode(inode, inode->i_mode, rdev);
369                 } else {
370                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
371                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
372                 }
373                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
374         }
375 #endif 
376 } /* ll_to_inode */
377
378 static inline void ll_read_inode2(struct inode *inode, void *opaque)
379 {
380         struct mds_body *body = opaque; 
381         
382         ENTRY;
383         ll_to_inode(inode, body); 
384
385         /* OIDEBUG(inode); */
386
387         if (S_ISREG(inode->i_mode)) {
388                 inode->i_op = &ll_file_inode_operations;
389                 inode->i_fop = &ll_file_operations;
390                 inode->i_mapping->a_ops = &ll_aops;
391                 EXIT;
392         } else if (S_ISDIR(inode->i_mode)) {
393                 inode->i_op = &ll_dir_inode_operations;
394                 inode->i_fop = &ll_dir_operations; 
395                 inode->i_mapping->a_ops = &ll_dir_aops;
396                 EXIT;
397         } else if (S_ISLNK(inode->i_mode)) {
398                 inode->i_op = &ll_fast_symlink_inode_operations;
399                 EXIT;
400         } else {
401                 init_special_inode(inode, inode->i_mode,
402                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
403                 EXIT;
404         }
405
406         return;
407 }
408
409 /* exported operations */
410 struct super_operations ll_super_operations =
411 {
412         read_inode2: ll_read_inode2,
413         delete_inode: ll_delete_inode,
414         put_super: ll_put_super,
415         statfs: ll_statfs
416 };
417
418 struct file_system_type lustre_lite_fs_type = {
419         "lustre_lite", 0, ll_read_super, NULL
420 };
421
422 static int __init init_lustre_lite(void)
423 {
424         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
425         ll_file_data_slab = kmem_cache_create("ll_file_data",
426                                               sizeof(struct ll_file_data), 0,
427                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
428         if (ll_file_data_slab == NULL)
429                 return -ENOMEM;
430         return register_filesystem(&lustre_lite_fs_type);
431 }
432
433 static void __exit exit_lustre_lite(void)
434 {
435         unregister_filesystem(&lustre_lite_fs_type);
436         kmem_cache_destroy(ll_file_data_slab);
437 }
438
439 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
440 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
441 MODULE_LICENSE("GPL");
442
443 module_init(init_lustre_lite);
444 module_exit(exit_lustre_lite);