Whamcloud - gitweb
Small fixes to get lustre_light mount working over the request handling
[fs/lustre-release.git] / lustre / llite / super.c
1 /*
2  * OBDFS Super operations
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
7  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
8  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
9  * Copryright (C) 1999 Seagate Technology Inc.
10  * Copryright (C) 2001 Mountain View Data, Inc.
11  * Copryright (C) 2002 Cluster File Systems, Inc.
12  *
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/locks.h>
23 #include <linux/unistd.h>
24
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27
28 #include <linux/fs.h>
29 #include <linux/stat.h>
30 #include <asm/uaccess.h>
31 #include <linux/vmalloc.h>
32 #include <asm/segment.h>
33
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/lustre_light.h>
37
38 //struct list_head ll_super_list;
39 extern struct address_space_operations ll_aops;
40 struct super_operations ll_super_operations;
41 long ll_cache_count = 0;
42 long ll_mutex_start = 0;
43 long obd_memory = 0;
44
45 static char *ll_read_opt(const char *opt, char *data)
46 {
47         char *value;
48         char *retval;
49
50         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
51         if ( strncmp(opt, data, strlen(opt)) )
52                 return NULL;
53
54         if ( (value = strchr(data, '=')) == NULL )
55                 return NULL;
56
57         value++;
58         OBD_ALLOC(retval, char *, strlen(value) + 1);
59         if ( !retval ) {
60                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
61                 return NULL;
62         }
63         
64         memcpy(retval, value, strlen(value)+1);
65         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
66         return retval;
67 }
68
69 static void ll_options(char *options, char **dev, char **vers)
70 {
71         char *this_char;
72
73         if (!options)
74                 return;
75
76         for (this_char = strtok (options, ",");
77              this_char != NULL;
78              this_char = strtok (NULL, ",")) {
79                 CDEBUG(D_INFO, "this_char %s\n", this_char);
80                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
81                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
82                         continue;
83                 
84         }
85 }
86
87 static struct super_block * ll_read_super(struct super_block *sb, 
88                                             void *data, int silent)
89 {
90         struct inode *root = 0; 
91         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
92         struct obd_device *obddev;
93         char *device = NULL;
94         char *version = NULL;
95         int root_ino = 2;
96         int connected = 0;
97         int devno;
98         int err;
99         struct mds_rep *rep; 
100         struct mds_rep_hdr *hdr = NULL; 
101         
102
103         ENTRY;
104         MOD_INC_USE_COUNT; 
105         memset(sbi, 0, sizeof(*sbi));
106         
107         CDEBUG(D_INFO, "\n"); 
108         ll_options(data, &device, &version);
109         if ( !device ) {
110                 printk(__FUNCTION__ ": no device\n");
111                 EXIT;
112                 goto ERR;
113         }
114
115         devno = simple_strtoul(device, NULL, 0);
116         CDEBUG(D_INFO, "\n"); 
117         if ( devno >= MAX_OBD_DEVICES ) {
118                 printk(__FUNCTION__ ": device of %s too high (%d)\n", device, devno);
119                 EXIT;
120                 goto ERR;
121         } 
122
123         CDEBUG(D_INFO, "\n"); 
124         obddev = &obd_dev[devno];
125
126
127         CDEBUG(D_INFO, "\n"); 
128         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
129              ! (obddev->obd_flags & OBD_SET_UP) ){
130                 printk("device %s not attached or not set up (%d)\n", 
131                        device, MINOR(devno));
132                 EXIT;
133                 goto ERR;;
134         } 
135
136         CDEBUG(D_INFO, "\n"); 
137         sbi->ll_obd = obddev;
138         sbi->ll_ops = sbi->ll_obd->obd_type->typ_ops;
139         sbi->ll_conn.oc_dev = obddev;
140
141         CDEBUG(D_INFO, "\n"); 
142         err = sbi->ll_ops->o_connect(&sbi->ll_conn);
143         CDEBUG(D_INFO, "\n"); 
144         if ( err ) {
145                 printk("OBDFS: cannot connect to %s\n", device);
146                 EXIT;
147                 goto ERR;
148         }
149         connected = 1;
150
151         CDEBUG(D_INFO, "\n"); 
152         /* list of dirty inodes, and a mutex to hold while modifying it */
153         INIT_LIST_HEAD(&sbi->ll_inodes);
154         init_MUTEX (&sbi->ll_list_mutex);
155
156         CDEBUG(D_INFO, "\n"); 
157         sbi->ll_super = sb;
158         sbi->ll_rootino = 2;
159         
160         CDEBUG(D_INFO, "\n"); 
161         sb->s_maxbytes = 1LL << 36;
162         printk("Max bytes: %Lx\n", sb->s_maxbytes);
163         sb->s_blocksize = PAGE_SIZE;
164         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
165         sb->s_magic = LL_SUPER_MAGIC;
166         sb->s_op = &ll_super_operations;
167
168         /* make root inode */
169         CDEBUG(D_INFO, "\n"); 
170         err = mdc_getattr(root_ino, S_IFDIR, OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 
171                          &rep, &hdr);
172         if (err) {
173                 printk(__FUNCTION__ ": mds_getattr failed %d\n", err);
174                 iput(root); 
175                 if (rep)
176                         kfree(rep);
177                 EXIT;
178                 goto ERR;
179         }
180                          
181         CDEBUG(D_INFO, "mode %o\n", rep->mode);
182         CDEBUG(D_INFO, "\n"); 
183         root = iget4(sb, root_ino, NULL, rep);
184         kfree(hdr);
185         CDEBUG(D_INFO, "\n"); 
186         if (!root) {
187             printk("OBDFS: bad iget4 for root\n");
188             sb->s_dev = 0;
189             err = -ENOENT;
190             EXIT;
191             goto ERR;
192         } 
193         
194         sb->s_root = d_alloc_root(root);
195         //        list_add(&sbi->ll_list, &ll_super_list);
196         OBD_FREE(device, strlen(device) + 1);
197         if (version)
198                 OBD_FREE(version, strlen(version) + 1);
199         EXIT;  
200         return sb;
201
202 ERR:
203         MOD_DEC_USE_COUNT;
204         if (device)
205                 OBD_FREE(device, strlen(device) + 1);
206         if (version)
207                 OBD_FREE(version, strlen(version) + 1);
208         if (connected) 
209                 sbi->ll_ops->o_disconnect(&sbi->ll_conn);
210
211         if (sbi) {
212                 sbi->ll_super = NULL;
213         }
214         if (root) {
215                 iput(root);
216         }
217         sb->s_dev = 0;
218         return NULL;
219 } /* ll_read_super */
220
221
222 static void ll_put_super(struct super_block *sb)
223 {
224         struct ll_sb_info *sbi;
225
226         ENTRY;
227         sb->s_dev = 0;
228         
229         sbi = (struct ll_sb_info *) &sb->u.generic_sbp;
230         //ll_flush_reqs(&sbi->ll_inodes, ~0UL);
231
232         OPS(sb,disconnect)(ID(sb));
233         list_del(&sbi->ll_list);
234         
235         printk(KERN_INFO "OBDFS: Bye bye.\n");
236
237         MOD_DEC_USE_COUNT;
238         EXIT;
239 } /* ll_put_super */
240
241
242 void ll_do_change_inode(struct inode *inode, int valid)
243 {
244         struct obdo *oa;
245         int err;
246         
247         ENTRY;
248         if (IOPS(inode, setattr) == NULL) {
249                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
250                 EXIT;
251                 return;
252         }
253         oa = obdo_alloc();
254         if ( !oa ) {
255                 printk(__FUNCTION__ ": obdo_alloc failed\n");
256                 EXIT;
257                 return;
258         }
259
260         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
261         ll_from_inode(oa, inode);
262         oa->o_mode = inode->i_mode;
263         err = IOPS(inode, setattr)(IID(inode), oa);
264
265         if ( err )
266                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
267
268         EXIT;
269         obdo_free(oa);
270 } /* ll_write_inode */
271
272 void ll_change_inode(struct inode *inode, int mask)
273 {
274         return ll_do_change_inode(inode, OBD_MD_FLNLINK); 
275 }
276
277
278 extern void write_inode_pages(struct inode *);
279 /* This routine is called from iput() (for each unlink on the inode).
280  * We can't put this call into delete_inode() since that is called only
281  * when i_count == 0, and we need to keep a reference on the inode while
282  * it is in the page cache, which means i_count > 0.  Catch 22.
283  */
284 static void ll_put_inode(struct inode *inode)
285 {
286         ENTRY;
287         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
288                 write_inode_pages(inode);
289                 EXIT;
290                 return;
291         }
292
293         //ll_dequeue_pages(inode);
294         EXIT;
295 } /* ll_put_inode */
296
297
298 static void ll_delete_inode(struct inode *inode)
299 {
300         ll_do_change_inode(inode, ~0);
301         clear_inode(inode); 
302 }
303 #if 0
304 {
305         struct obdo *oa;
306         int err;
307
308         ENTRY;
309         if (IOPS(inode, destroy) == NULL) {
310                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
311                 EXIT;
312                 return;
313         }
314
315         oa = obdo_alloc();
316         if ( !oa ) {
317                 printk(__FUNCTION__ ": obdo_alloc failed\n");
318                 EXIT;
319                 return;
320         }
321         oa->o_valid = OBD_MD_FLNOTOBD;
322         ll_from_inode(oa, inode);
323
324         /* XXX how do we know that this inode is now clean? */
325         printk("delete_inode ------> link %d\n", inode->i_nlink);
326         ODEBUG(oa);
327         err = IOPS(inode, destroy)(IID(inode), oa);
328         obdo_free(oa);
329         clear_inode(inode);
330         if (err) {
331                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
332                 EXIT;
333                 return;
334         }
335
336         EXIT;
337 } /* ll_delete_inode */
338 #endif
339
340
341 static int ll_attr2inode(struct inode * inode, struct iattr * attr)
342 {
343         unsigned int ia_valid = attr->ia_valid;
344         int error = 0;
345
346         if (ia_valid & ATTR_SIZE) {
347                 error = vmtruncate(inode, attr->ia_size);
348                 if (error)
349                         goto out;
350         }
351
352         if (ia_valid & ATTR_UID)
353                 inode->i_uid = attr->ia_uid;
354         if (ia_valid & ATTR_GID)
355                 inode->i_gid = attr->ia_gid;
356         if (ia_valid & ATTR_ATIME)
357                 inode->i_atime = attr->ia_atime;
358         if (ia_valid & ATTR_MTIME)
359                 inode->i_mtime = attr->ia_mtime;
360         if (ia_valid & ATTR_CTIME)
361                 inode->i_ctime = attr->ia_ctime;
362         if (ia_valid & ATTR_MODE) {
363                 inode->i_mode = attr->ia_mode;
364                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
365                         inode->i_mode &= ~S_ISGID;
366         }
367 out:
368         return error;
369 }
370
371 int ll_setattr(struct dentry *de, struct iattr *attr)
372 {
373         struct inode *inode = de->d_inode;
374         struct obdo *oa;
375         int err;
376
377         ENTRY;
378         if (IOPS(inode, setattr) == NULL) {
379                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
380                 EXIT;
381                 return -EIO;
382         }
383         oa = obdo_alloc();
384         if ( !oa ) {
385                 printk(__FUNCTION__ ": obdo_alloc failed\n");
386                 return -ENOMEM;
387         }
388
389         ll_attr2inode(inode, attr);
390         oa->o_id = inode->i_ino;
391         oa->o_mode = inode->i_mode;
392         obdo_from_iattr(oa, attr);
393         err = IOPS(inode, setattr)(IID(inode), oa);
394
395         if ( err )
396                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
397
398         EXIT;
399         obdo_free(oa);
400         return err;
401 } /* ll_setattr */
402
403
404
405 static int ll_statfs(struct super_block *sb, struct statfs *buf)
406 {
407         struct statfs tmp;
408         int err;
409
410         ENTRY;
411
412         err = OPS(sb,statfs)(ID(sb), &tmp);
413         if ( err ) { 
414                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
415                 return err;
416         }
417         memcpy(buf, &tmp, sizeof(*buf));
418         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
419         EXIT;
420
421         return err; 
422 }
423
424 static inline void ll_read_inode2(struct inode *inode, void *opaque)
425 {
426         struct mds_rep *rep = opaque; 
427         
428         ENTRY;
429         ll_to_inode(inode, rep); 
430
431         INIT_LIST_HEAD(ll_iplist(inode)); /* list of dirty pages on inode */
432         INIT_LIST_HEAD(ll_islist(inode)); /* list of inodes in superblock */
433
434         /* OIDEBUG(inode); */
435
436         if (S_ISREG(inode->i_mode)) {
437                 inode->i_op = &ll_file_inode_operations;
438                 inode->i_fop = &ll_file_operations;
439                 inode->i_mapping->a_ops = &ll_aops;
440                 EXIT;
441         } else if (S_ISDIR(inode->i_mode)) {
442                 inode->i_op = &ll_dir_inode_operations;
443                 inode->i_fop = &ll_dir_operations; 
444                 inode->i_mapping->a_ops = &ll_aops;
445                 EXIT;
446         } else if (S_ISLNK(inode->i_mode)) {
447                 if (inode->i_blocks) { 
448                         inode->i_op = &ll_symlink_inode_operations;
449                         inode->i_mapping->a_ops = &ll_aops;
450                 }else {
451                         inode->i_op = &ll_fast_symlink_inode_operations;
452                 }
453                 EXIT;
454         } else {
455                 init_special_inode(inode, inode->i_mode,
456                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
457         }
458
459         EXIT;
460         return;
461 }
462
463 /* exported operations */
464 struct super_operations ll_super_operations =
465 {
466         read_inode2: ll_read_inode2,
467         // put_inode: ll_put_inode,
468         // delete_inode: ll_delete_inode,
469         // put_super: ll_put_super,
470         // statfs: ll_statfs
471 };
472
473
474
475 struct file_system_type lustre_light_fs_type = {
476    "lustre_light", 0, ll_read_super, NULL
477 };
478
479 static int __init init_lustre_light(void)
480 {
481         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
482
483         return register_filesystem(&lustre_light_fs_type);
484 }
485
486 static void __exit exit_lustre_light(void)
487 {
488         unregister_filesystem(&lustre_light_fs_type);
489 }
490
491 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
492 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
493 MODULE_LICENSE("GPL");
494
495 module_init(init_lustre_light);
496 module_exit(exit_lustre_light);