Whamcloud - gitweb
Updating obdfs/ext2obd methods for new obdo parameters.
[fs/lustre-release.git] / lustre / obdfs / super.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
5  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
6  * Copryright (C) 1999 Seagate Technology Inc.
7  *
8  */
9
10 #define EXPORT_SYMTAB
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/stat.h>
18 #include <linux/errno.h>
19 #include <linux/locks.h>
20 #include <linux/unistd.h>
21
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
24
25 #include <linux/fs.h>
26 #include <linux/stat.h>
27 #include <asm/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <asm/segment.h>
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obdfs.h>
34
35 /* VFS super_block ops */
36 static struct super_block *obdfs_read_super(struct super_block *, void *, int);
37 static int  obdfs_notify_change(struct dentry *dentry, struct iattr *attr);
38 static void obdfs_write_inode(struct inode *);
39 static void obdfs_delete_inode(struct inode *);
40 static void obdfs_put_super(struct super_block *);
41 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
42                        int bufsiz);
43
44 /* exported operations */
45 struct super_operations obdfs_super_operations =
46 {
47         obdfs_read_inode,       /* read_inode */
48         obdfs_write_inode,      /* write_inode */
49         NULL,                   /* put_inode */
50         obdfs_delete_inode,     /* delete_inode */
51         obdfs_notify_change,    /* notify_change */
52         obdfs_put_super,        /* put_super */
53         NULL,                   /* write_super */
54         obdfs_statfs,           /* statfs */
55         NULL                    /* remount_fs */
56 };
57
58 struct list_head obdfs_super_list;
59
60 static char *obdfs_read_opt(const char *opt, char *data)
61 {
62         char *value;
63         char *retval;
64
65         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66         if ( strncmp(opt, data, strlen(opt)) )
67                 return NULL;
68
69         if ( (value = strchr(data, '=')) == NULL )
70                 return NULL;
71
72         value++;
73         OBD_ALLOC(retval, char *, strlen(value) + 1);
74         if ( !retval ) {
75                 printk("OBDFS: Out of memory!\n");
76                 return NULL;
77         }
78         
79         memcpy(retval, value, strlen(value)+1);
80         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
81         return retval;
82 }
83
84 void obdfs_options(char *options, char **dev, char **vers)
85 {
86         char *this_char;
87
88         if (!options)
89                 return;
90
91         for (this_char = strtok (options, ",");
92              this_char != NULL;
93              this_char = strtok (NULL, ",")) {
94                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
95                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
96                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
97                         continue;
98                 
99         }
100 }
101
102 static int obdfs_getdev(char *devpath, int *dev)
103 {
104         struct dentry *dentry;
105         kdev_t devno;
106
107         dentry = lookup_dentry(devpath, NULL, 0);
108         if (IS_ERR(dentry))
109                 return PTR_ERR(dentry);
110         
111         if (!S_ISCHR(dentry->d_inode->i_mode))
112                 return -ENODEV;
113
114         devno = dentry->d_inode->i_rdev;
115         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
116                 return -ENODEV;
117         
118         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
119                 return -ENODEV;
120
121         *dev = devno;
122         return 0;
123 }
124
125
126 /* XXX allocate a super_entry, and add the super to the obdfs_super_list */
127 static struct super_block * obdfs_read_super(struct super_block *sb, 
128                                             void *data, int silent)
129 {
130         struct inode *root = 0; 
131         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
132         struct obd_device *obddev;
133         char *device = NULL;
134         char *version = NULL;
135         int devno;
136         int err;
137         unsigned long blocksize;
138         unsigned long blocksize_bits;
139         unsigned long root_ino;
140         int scratch;
141
142         ENTRY;
143         MOD_INC_USE_COUNT; 
144         
145         memset(sbi, 0, sizeof(*sbi));
146         
147         obdfs_options(data, &device, &version);
148         if ( !device ) {
149                 printk("No device\n");
150                 MOD_DEC_USE_COUNT;
151                 EXIT;
152                 return NULL;
153         }
154
155         if ( (err = obdfs_getdev(device, &devno)) ) {
156                 printk("Cannot get devno of %s, err %d\n", device, err);
157                 MOD_DEC_USE_COUNT;
158                 EXIT;
159                 return NULL;
160         }
161
162         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
163                 printk("Wrong major number!\n");
164                 MOD_DEC_USE_COUNT;
165                 EXIT;
166                 return NULL;
167         }
168                 
169         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
170                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
171                 MOD_DEC_USE_COUNT;
172                 EXIT;
173                 return NULL;
174         } 
175
176         obddev = &obd_dev[MINOR(devno)];
177
178         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
179              ! (obddev->obd_flags & OBD_SET_UP) ){
180                 printk("Device %s not attached or not set up (%d)\n", 
181                        device, MINOR(devno));
182                 MOD_DEC_USE_COUNT;
183                 EXIT;
184                 return NULL;
185         } 
186
187         sbi->osi_obd = obddev;
188         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
189         
190         sbi->osi_conn.oc_dev = obddev;
191         err  = sbi->osi_ops->o_connect(&sbi->osi_conn);
192         if ( err ) {
193                 printk("OBDFS: cannot connect to %s\n", device);
194                 goto ERR;
195         }
196
197         INIT_LIST_HEAD(&sbi->osi_list);
198
199         sbi->osi_super = sb;
200
201         err = sbi->osi_ops->o_get_info(&sbi->osi_conn,
202                                          strlen("blocksize"), 
203                                          "blocksize", 
204                                          &scratch, (void *)&blocksize);
205         if ( err ) {
206                 printk("Getinfo call to drive failed (blocksize)\n");
207                 goto ERR;
208         }
209
210         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
211                                        "blocksize_bits", &scratch,
212                                        (void *)&blocksize_bits);
213         if ( err ) {
214                 printk("Getinfo call to drive failed (blocksize_bits)\n");
215                 goto ERR;
216         }
217
218         err = sbi->osi_ops->o_get_info(&sbi->osi_conn,
219                                          strlen("root_ino"), 
220                                          "root_ino", 
221                                          &scratch, (void *)&root_ino);
222         if ( err ) {
223                 printk("Getinfo call to drive failed (root_ino)\n");
224                 goto ERR;
225         }
226
227         lock_super(sb);
228         
229         sb->s_blocksize = blocksize;
230         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
231         sb->s_magic = OBDFS_SUPER_MAGIC;
232         sb->s_op = &obdfs_super_operations;
233
234         /* make root inode */
235         root = iget(sb, root_ino);
236         if (!root || is_bad_inode(root)) {
237             printk("OBDFS: bad iget for root\n");
238             sb->s_dev = 0;
239             err = ENOENT;
240             unlock_super(sb);
241             goto ERR;
242         } 
243         
244
245         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
246                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
247                sb->s_dev, root->i_ino, device, MINOR(devno), 
248                blocksize, blocksize_bits);
249         sb->s_root = d_alloc_root(root);
250         unlock_super(sb);
251         EXIT;  
252         return sb;
253
254 ERR:
255         EXIT;  
256         MOD_DEC_USE_COUNT;
257         if (sbi) {
258                 sbi->osi_super = NULL;
259         }
260         if (root) {
261                 iput(root);
262         }
263         sb->s_dev = 0;
264         return NULL;
265 }
266
267 /* XXX remove the super to the obdfs_super_list */
268 static void obdfs_put_super(struct super_block *sb)
269 {
270         struct obdfs_sb_info *sbi;
271
272         ENTRY;
273         sb->s_dev = 0;
274         
275         /* XXX flush stuff */
276         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
277
278         OPS(sb,disconnect)(ID(sb));
279
280         memset(sbi, 0, sizeof(* sbi));
281         
282         printk("OBDFS: Bye bye.\n");
283
284         MOD_DEC_USE_COUNT;
285         EXIT;
286 }
287
288 /* all filling in of inodes postponed until lookup */
289 void obdfs_read_inode(struct inode *inode)
290 {
291         struct obdo *oa;
292         int err;
293
294         ENTRY;
295         oa = obdo_alloc();
296         if (!oa) {
297                 printk("obdfs_read_inode: obdo_alloc failed\n");
298                 EXIT;
299                 return;
300         }
301         oa->o_valid = OBD_MD_FLALL;
302         oa->o_id = inode->i_ino;
303         err = IOPS(inode, getattr)(IID(inode), oa);
304         if (err) {
305                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", err);
306                 obdo_free(oa);
307                 EXIT;
308                 return;
309         }
310
311         ODEBUG(oa);
312         obdo_to_inode(inode, oa);
313         obdo_free(oa);
314         IDEBUG(inode);
315
316         if (S_ISREG(inode->i_mode))
317                 inode->i_op = &obdfs_file_inode_operations;
318         else if (S_ISDIR(inode->i_mode))
319                 inode->i_op = &obdfs_dir_inode_operations;
320         else if (S_ISLNK(inode->i_mode))
321                 inode->i_op = &obdfs_symlink_inode_operations;
322         else
323                 /* XXX what do we pass here??? */
324                 init_special_inode(inode, inode->i_mode, 0 /* XXX XXX */ );
325
326         return;
327 }
328
329 static void obdfs_write_inode(struct inode *inode) 
330 {
331         struct obdo *oa;
332         int err;
333         
334         oa = obdo_alloc();
335         oa->o_valid = OBD_MD_FLALL;
336         obdo_from_inode(oa, inode);
337         err = IOPS(inode, setattr)(IID(inode), oa);
338
339         obdo_free(oa);
340
341         if (err) {
342                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", err);
343                 return;
344         }
345         
346         return;
347 }
348
349 static void obdfs_delete_inode(struct inode *inode)
350 {
351         struct obdo *oa;
352         int err;
353         ENTRY;
354
355         oa = obdo_alloc();
356         /* XXX we currently assume "id" is all that's needed for destroy */
357         oa->o_id = inode->i_ino;
358         err = IOPS(inode, destroy)(IID(inode), oa);
359         obdo_free(oa);
360
361         if (err) {
362                 printk("obdfs_delete_node: obd_destroy fails (%d)\n", err);
363                 return;
364         }
365
366         EXIT;
367 }
368
369
370 static void obdo_setattr(struct obdo *oa, struct iattr *attr)
371 {
372         unsigned int ia_valid = attr->ia_valid;
373
374         if (ia_valid & ATTR_ATIME) {
375                 oa->o_atime = attr->ia_atime;
376                 oa->o_valid |= OBD_MD_FLATIME;
377         }
378         if (ia_valid & ATTR_MTIME) {
379                 oa->o_mtime = attr->ia_mtime;
380                 oa->o_valid |= OBD_MD_FLMTIME;
381         }
382         if (ia_valid & ATTR_CTIME) {
383                 oa->o_ctime = attr->ia_ctime;
384                 oa->o_valid |= OBD_MD_FLCTIME;
385         }
386         if (ia_valid & ATTR_SIZE) {
387                 oa->o_size = attr->ia_size;
388                 oa->o_valid |= OBD_MD_FLSIZE;
389         }
390         if (ia_valid & ATTR_MODE) {
391                 oa->o_mode = attr->ia_mode;
392                 oa->o_valid |= OBD_MD_FLMODE;
393                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
394                         oa->o_mode &= ~S_ISGID;
395         }
396         if (ia_valid & ATTR_UID)
397         {
398                 oa->o_uid = attr->ia_uid;
399                 oa->o_valid |= OBD_MD_FLUID;
400         }
401         if (ia_valid & ATTR_GID) {
402                 oa->o_gid = attr->ia_gid;
403                 oa->o_valid |= OBD_MD_FLGID;
404         }
405 }
406
407
408 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
409 {
410         struct inode *inode = de->d_inode;
411         struct obdo *oa;
412         int err;
413
414         ENTRY;
415         oa = obdo_alloc();
416         if (!oa) {
417                 printk("obdfs_notify_change: obdo_alloc fails\n");
418                 return -ENOMEM;
419         }
420
421         oa->o_id = inode->i_ino;
422         obdo_setattr(oa, attr);
423         err = IOPS(inode, setattr)(IID(inode), oa);
424         if ( err ) {
425                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", err);
426                 return err;
427         }
428         inode_setattr(inode, attr);
429
430         CDEBUG(D_INODE, "inode blocks now %ld\n", inode->i_blocks);
431         EXIT;
432         return err;
433 }
434
435
436 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
437                        int bufsize)
438 {
439         struct statfs tmp;
440         int err;
441
442         ENTRY;
443
444         err = OPS(sb,statfs)(ID(sb), &tmp);
445         if ( err ) { 
446                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", err);
447                 return err;
448         }
449         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
450
451         EXIT;
452
453         return err; 
454 }
455
456 struct file_system_type obdfs_fs_type = {
457    "obdfs", 0, obdfs_read_super, NULL
458 };
459
460 int init_obdfs(void)
461 {
462         int err;
463
464         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
465
466         obdfs_sysctl_init();
467
468         INIT_LIST_HEAD(&obdfs_super_list);
469         err = obdfs_init_wreqcache();
470         if (err)
471                 return err;
472
473         flushd_init();
474
475         return register_filesystem(&obdfs_fs_type);
476 }
477
478
479 #ifdef MODULE
480 int init_module(void)
481 {
482         return init_obdfs();
483 }
484
485 void cleanup_module(void)
486 {
487         ENTRY;
488
489         obdfs_sysctl_clean();
490         unregister_filesystem(&obdfs_fs_type);
491 }
492
493 #endif