Whamcloud - gitweb
WARNING - if an RPC times out you will crash older UML's.
[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 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/stat.h>
23 #include <linux/errno.h>
24 #include <linux/locks.h>
25 #include <linux/unistd.h>
26
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29
30 #include <linux/fs.h>
31 #include <linux/stat.h>
32 #include <asm/uaccess.h>
33 #include <asm/segment.h>
34
35 #define DEBUG_SUBSYSTEM S_LLITE
36
37 #include <linux/lustre_lite.h>
38
39 kmem_cache_t *ll_file_data_slab;
40 extern struct address_space_operations ll_aops;
41 extern struct address_space_operations ll_dir_aops;
42 struct super_operations ll_super_operations;
43 extern int ll_commitcbd_setup(struct ll_sb_info *);
44 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
45
46 static char *ll_read_opt(const char *opt, char *data)
47 {
48         char *value;
49         char *retval;
50         ENTRY;
51
52         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
53         if ( strncmp(opt, data, strlen(opt)) ) {
54                 EXIT;
55                 return NULL;
56         }
57         if ( (value = strchr(data, '=')) == NULL ) {
58                 EXIT;
59                 return NULL;
60         }
61
62         value++;
63         OBD_ALLOC(retval, strlen(value) + 1);
64         if ( !retval ) {
65                 CERROR("out of memory!\n");
66                 return NULL;
67         }
68         
69         memcpy(retval, value, strlen(value)+1);
70         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
71         EXIT;
72         return retval;
73 }
74
75 static void ll_options(char *options, char **dev, char **vers)
76 {
77         char *this_char;
78         ENTRY;
79
80         if (!options) {
81                 EXIT;
82                 return;
83         }
84
85         for (this_char = strtok (options, ",");
86              this_char != NULL;
87              this_char = strtok (NULL, ",")) {
88                 CDEBUG(D_INFO, "this_char %s\n", this_char);
89                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
90                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
91                         continue;
92         }
93         EXIT;
94 }
95
96 static struct super_block * ll_read_super(struct super_block *sb,
97                                           void *data, int silent)
98 {
99         struct inode *root = 0;
100         struct ll_sb_info *sbi;
101         char *device = NULL;
102         char *version = NULL;
103         int devno;
104         int err;
105         struct ptlrpc_request *request = NULL;
106
107         ENTRY;
108         MOD_INC_USE_COUNT;
109
110         OBD_ALLOC(sbi, sizeof(*sbi));
111         if (!sbi) {
112                 MOD_DEC_USE_COUNT;
113                 RETURN(NULL);
114         }
115
116         sb->u.generic_sbp = sbi;
117
118         ll_options(data, &device, &version);
119
120         if ( !device ) {
121                 CERROR("no device\n");
122                 GOTO(out_free, sb = NULL);
123         }
124
125         devno = simple_strtoul(device, NULL, 0);
126         if ( devno >= MAX_OBD_DEVICES ) {
127                 CERROR("device of %s too high\n", device);
128                 GOTO(out_free, sb = NULL);
129         }
130
131         sbi->ll_conn.oc_dev = &obd_dev[devno];
132         err = obd_connect(&sbi->ll_conn);
133         if ( err ) {
134                 CERROR("cannot connect to %s\n", device);
135                 GOTO(out_free, sb = NULL);
136         }
137
138         ptlrpc_init_client(ptlrpc_connmgr, 
139                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
140                            &sbi->ll_mds_client);
141
142         sbi->ll_mds_conn = ptlrpc_uuid_to_connection("mds");
143         if (!sbi->ll_mds_conn) {
144                 CERROR("cannot find MDS\n");
145                 GOTO(out_disc, sb = NULL);
146         }
147
148         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
149         if (err) { 
150                 CERROR("cannot connect to MDS\n"); 
151                 GOTO(out_disc, sb = NULL);
152         }                
153
154         sbi->ll_rootino = 2;
155
156         sb->s_maxbytes = 1LL << 36;
157         sb->s_blocksize = PAGE_SIZE;
158         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
159         sb->s_magic = LL_SUPER_MAGIC;
160         sb->s_op = &ll_super_operations;
161
162         /* make root inode */
163         err = mdc_getattr(&sbi->ll_mds_client, sbi->ll_mds_conn,
164                           sbi->ll_rootino, S_IFDIR,
165                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, &request);
166         if (err) {
167                 CERROR("mdc_getattr failed for root %d\n", err);
168                 GOTO(out_req, sb = NULL);
169         }
170
171         /* initialize committed transaction callback daemon */
172         INIT_LIST_HEAD(&sbi->ll_commitcbd_not_committed);
173         spin_lock_init(&sbi->ll_commitcbd_lock); 
174         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
175         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
176         sbi->ll_commitcbd_flags = 0;
177         err = ll_commitcbd_setup(sbi);
178         if (err) { 
179                 CERROR("failed to start commit callback daemon\n");
180                 GOTO(out_req, sb = NULL); 
181         }
182
183         root = iget4(sb, sbi->ll_rootino, NULL,
184                      lustre_msg_buf(request->rq_repmsg, 0));
185         if (root) {
186                 sb->s_root = d_alloc_root(root);
187         } else {
188                 CERROR("lustre_lite: bad iget4 for root\n");
189                 GOTO(out_req, sb = NULL);
190         }
191
192 out_req:
193         ptlrpc_free_req(request);
194         if (!sb) {
195 out_disc:
196                 obd_disconnect(&sbi->ll_conn);
197 out_free:
198                 MOD_DEC_USE_COUNT;
199                 OBD_FREE(sbi, sizeof(*sbi));
200         }
201         if (device) 
202                 OBD_FREE(device, strlen(device) + 1);
203         if (version)
204                 OBD_FREE(version, strlen(version) + 1);
205
206         RETURN(sb);
207 } /* ll_read_super */
208
209 static void ll_put_super(struct super_block *sb)
210 {
211         struct ll_sb_info *sbi = sb->u.generic_sbp;
212         ENTRY;
213         ll_commitcbd_cleanup(sbi);
214         obd_disconnect(&sbi->ll_conn);
215         ptlrpc_put_connection(sbi->ll_mds_conn);
216         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
217         MOD_DEC_USE_COUNT;
218         EXIT;
219 } /* ll_put_super */
220
221
222 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
223 static void ll_delete_inode(struct inode *inode)
224 {
225         if (S_ISREG(inode->i_mode)) { 
226                 int err; 
227                 struct obdo *oa; 
228                 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
229                 if (!oa) { 
230                         CERROR("no memory\n"); 
231                 }
232
233                 err = obd_destroy(ll_i2obdconn(inode), oa); 
234                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
235                        oa->o_id, err);
236                 obdo_free(oa);
237         }
238
239         clear_inode(inode); 
240 }
241
242 /* like inode_setattr, but doesn't mark the inode dirty */ 
243 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
244 {
245         unsigned int ia_valid = attr->ia_valid;
246         int error = 0;
247
248         if ((ia_valid & ATTR_SIZE) && trunc ) {
249                 error = vmtruncate(inode, attr->ia_size);
250                 if (error)
251                         goto out;
252         } else if (ia_valid & ATTR_SIZE) { 
253                 inode->i_size = attr->ia_size;
254         }               
255
256         if (ia_valid & ATTR_UID)
257                 inode->i_uid = attr->ia_uid;
258         if (ia_valid & ATTR_GID)
259                 inode->i_gid = attr->ia_gid;
260         if (ia_valid & ATTR_ATIME)
261                 inode->i_atime = attr->ia_atime;
262         if (ia_valid & ATTR_MTIME)
263                 inode->i_mtime = attr->ia_mtime;
264         if (ia_valid & ATTR_CTIME)
265                 inode->i_ctime = attr->ia_ctime;
266         if (ia_valid & ATTR_MODE) {
267                 inode->i_mode = attr->ia_mode;
268                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
269                         inode->i_mode &= ~S_ISGID;
270         }
271 out:
272         return error;
273 }
274
275 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
276 {
277         struct ptlrpc_request *request = NULL;
278         struct ll_sb_info *sbi = ll_i2sbi(inode);
279         int err;
280
281         ENTRY;
282
283         /* change incore inode */
284         ll_attr2inode(inode, attr, do_trunc);
285
286         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
287                           &request);
288         if (err)
289                 CERROR("mdc_setattr fails (%d)\n", err);
290
291         ptlrpc_free_req(request);
292
293         EXIT;
294         return err;
295 }
296
297 int ll_setattr(struct dentry *de, struct iattr *attr)
298 {
299         return ll_inode_setattr(de->d_inode, attr, 1);
300 }
301
302 static int ll_statfs(struct super_block *sb, struct statfs *buf)
303 {
304         struct statfs tmp;
305         int err;
306
307         ENTRY;
308
309         err = obd_statfs(ID(sb), &tmp);
310         if ( err ) { 
311                 CERROR("obd_statfs fails (%d)\n", err);
312                 return err;
313         }
314         memcpy(buf, &tmp, sizeof(*buf));
315         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
316         EXIT;
317
318         return err; 
319 }
320
321 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
322 {
323         struct ll_inode_info *ii = 
324                 (struct ll_inode_info *) &dst->u.generic_ip;
325
326         /* core attributes first */
327         if ( body->valid & OBD_MD_FLID )
328                 dst->i_ino = body->ino;
329         if ( body->valid & OBD_MD_FLATIME ) 
330                 dst->i_atime = body->atime;
331         if ( body->valid & OBD_MD_FLMTIME ) 
332                 dst->i_mtime = body->mtime;
333         if ( body->valid & OBD_MD_FLCTIME ) 
334                 dst->i_ctime = body->ctime;
335         if ( body->valid & OBD_MD_FLSIZE ) 
336                 dst->i_size = body->size;
337         if ( body->valid & OBD_MD_FLMODE ) 
338                 dst->i_mode = body->mode;
339         if ( body->valid & OBD_MD_FLUID ) 
340                 dst->i_uid = body->uid;
341         if ( body->valid & OBD_MD_FLGID ) 
342                 dst->i_gid = body->gid;
343         if ( body->valid & OBD_MD_FLFLAGS ) 
344                 dst->i_flags = body->flags;
345         if ( body->valid & OBD_MD_FLNLINK )
346                 dst->i_nlink = body->nlink;
347         if ( body->valid & OBD_MD_FLGENER )
348                 dst->i_generation = body->generation;
349
350         /* this will become more elaborate for striping etc */ 
351         if (body->valid & OBD_MD_FLOBJID) 
352                 ii->lli_objid = body->objid;
353 #if 0
354
355         if (obdo_has_inline(oa)) {
356                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
357                     S_ISFIFO(inode->i_mode)) {
358                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
359                         CDEBUG(D_INODE,
360                                "copying device %x from obdo to inode\n", rdev);
361                         init_special_inode(inode, inode->i_mode, rdev);
362                 } else {
363                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
364                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
365                 }
366                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
367         }
368 #endif 
369 } /* ll_to_inode */
370
371 static inline void ll_read_inode2(struct inode *inode, void *opaque)
372 {
373         struct mds_body *body = opaque; 
374         
375         ENTRY;
376         ll_to_inode(inode, body); 
377
378         /* OIDEBUG(inode); */
379
380         if (S_ISREG(inode->i_mode)) {
381                 inode->i_op = &ll_file_inode_operations;
382                 inode->i_fop = &ll_file_operations;
383                 inode->i_mapping->a_ops = &ll_aops;
384                 EXIT;
385         } else if (S_ISDIR(inode->i_mode)) {
386                 inode->i_op = &ll_dir_inode_operations;
387                 inode->i_fop = &ll_dir_operations; 
388                 inode->i_mapping->a_ops = &ll_dir_aops;
389                 EXIT;
390         } else if (S_ISLNK(inode->i_mode)) {
391                 inode->i_op = &ll_fast_symlink_inode_operations;
392                 EXIT;
393         } else {
394                 init_special_inode(inode, inode->i_mode,
395                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
396         }
397
398         EXIT;
399         return;
400 }
401
402 /* exported operations */
403 struct super_operations ll_super_operations =
404 {
405         read_inode2: ll_read_inode2,
406         delete_inode: ll_delete_inode,
407         put_super: ll_put_super,
408         // statfs: ll_statfs
409 };
410
411 struct file_system_type lustre_lite_fs_type = {
412         "lustre_lite", 0, ll_read_super, NULL
413 };
414
415 static int __init init_lustre_lite(void)
416 {
417         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
418         ll_file_data_slab = kmem_cache_create("ll_file_data",
419                                               sizeof(struct ll_file_data), 0,
420                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
421         if (ll_file_data_slab == NULL)
422                 return -ENOMEM;
423         return register_filesystem(&lustre_lite_fs_type);
424 }
425
426 static void __exit exit_lustre_lite(void)
427 {
428         unregister_filesystem(&lustre_lite_fs_type);
429         kmem_cache_destroy(ll_file_data_slab);
430 }
431
432 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
433 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
434 MODULE_LICENSE("GPL");
435
436 module_init(init_lustre_lite);
437 module_exit(exit_lustre_lite);