Whamcloud - gitweb
Changes to accomodate the snapshot features!
[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 static char *obdfs_read_opt(const char *opt, char *data)
59 {
60         char *value;
61         char *retval;
62
63         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
64         if ( strncmp(opt, data, strlen(opt)) )
65                 return NULL;
66
67         if ( (value = strchr(data, '=')) == NULL )
68                 return NULL;
69
70         value++;
71         OBD_ALLOC(retval, char *, strlen(value) + 1);
72         if ( !retval ) {
73                 printk("OBDFS: Out of memory!\n");
74                 return NULL;
75         }
76         
77         memcpy(retval, value, strlen(value)+1);
78         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
79         return retval;
80 }
81
82 void obdfs_options(char *options, char **dev, char **vers)
83 {
84         char *this_char;
85
86         if (!options)
87                 return;
88
89         for (this_char = strtok (options, ",");
90              this_char != NULL;
91              this_char = strtok (NULL, ",")) {
92                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
93                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
94                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
95                         continue;
96                 
97         }
98 }
99
100 static int obdfs_getdev(char *devpath, int *dev)
101 {
102         struct dentry *dentry;
103         kdev_t devno;
104
105         dentry = lookup_dentry(devpath, NULL, 0);
106         if (IS_ERR(dentry))
107                 return PTR_ERR(dentry);
108         
109         if (!S_ISCHR(dentry->d_inode->i_mode))
110                 return -ENODEV;
111
112         devno = dentry->d_inode->i_rdev;
113         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
114                 return -ENODEV;
115         
116         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
117                 return -ENODEV;
118
119         *dev = devno;
120         return 0;
121 }
122
123 static struct super_block * obdfs_read_super(struct super_block *sb, 
124                                             void *data, int silent)
125 {
126         struct inode *root = 0; 
127         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
128         struct obd_device *obddev;
129         int error = 0;
130         char *device = NULL;
131         char *version = NULL;
132         int devno;
133         int err;
134         unsigned long blocksize;
135         unsigned long blocksize_bits;
136         unsigned long root_ino;
137         int scratch;
138         
139
140         ENTRY;
141         MOD_INC_USE_COUNT; 
142         
143         memset(sbi, 0, sizeof(*sbi));
144         
145         obdfs_options(data, &device, &version);
146         if ( !device ) {
147                 printk("No device\n");
148                 MOD_DEC_USE_COUNT;
149                 return NULL;
150         }
151
152         if ( (err = obdfs_getdev(device, &devno)) ) {
153                 printk("Cannot get devno of %s, error %d\n", device, err);
154                 MOD_DEC_USE_COUNT;
155                 return NULL;
156         }
157
158         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
159                 printk("Wrong major number!\n");
160                 MOD_DEC_USE_COUNT;
161                 return NULL;
162         }
163                 
164         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
165                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
166                 MOD_DEC_USE_COUNT;
167                 return NULL;
168         } 
169
170         obddev = &obd_dev[MINOR(devno)];
171
172         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
173              ! (obddev->obd_flags & OBD_SET_UP) ){
174                 MOD_DEC_USE_COUNT;
175                 return NULL;
176         } 
177
178         sbi->osi_obd = obddev;
179         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
180         
181         sbi->osi_conn.oc_dev = obddev;
182         error  = sbi->osi_ops->o_connect(&sbi->osi_conn);
183         if ( error ) {
184                 printk("OBDFS: cannot connect to %s\n", device);
185                 goto error;
186         }
187
188         
189
190         sbi->osi_super = sb;
191
192         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
193                                          strlen("blocksize"), 
194                                          "blocksize", 
195                                          &scratch, (void *)&blocksize);
196         if ( error ) {
197                 printk("Getinfo call to drive failed (blocksize)\n");
198                 goto error;
199         }
200
201         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
202                                          strlen("blocksize_bits"), 
203                                          "blocksize_bits", 
204                                          &scratch, (void *)&blocksize_bits);
205         if ( error ) {
206                 printk("Getinfo call to drive failed (blocksize_bits)\n");
207                 goto error;
208         }
209
210         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
211                                          strlen("root_ino"), 
212                                          "root_ino", 
213                                          &scratch, (void *)&root_ino);
214         if ( error ) {
215                 printk("Getinfo call to drive failed (root_ino)\n");
216                 goto error;
217         }
218         
219
220
221         lock_super(sb);
222         
223         sb->s_blocksize = blocksize;
224         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
225         sb->s_magic = OBDFS_SUPER_MAGIC;
226         sb->s_op = &obdfs_super_operations;
227
228         /* make root inode */
229         root = iget(sb, root_ino);
230         if (!root || is_bad_inode(root)) {
231             printk("OBDFS: bad iget for root\n");
232             sb->s_dev = 0;
233             error = ENOENT;
234             unlock_super(sb);
235             goto error;
236         } 
237         
238
239         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
240                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
241                sb->s_dev, root->i_ino, device, MINOR(devno), 
242                blocksize, blocksize_bits);
243         sb->s_root = d_alloc_root(root);
244         unlock_super(sb);
245         EXIT;  
246         return sb;
247
248  error:
249         EXIT;  
250         MOD_DEC_USE_COUNT;
251         if (sbi) {
252                 sbi->osi_super = NULL;
253         }
254         if (root) {
255                 iput(root);
256         }
257         sb->s_dev = 0;
258         return NULL;
259 }
260
261 static void obdfs_put_super(struct super_block *sb)
262 {
263         struct obdfs_sb_info *sbi;
264
265         ENTRY;
266
267
268         sb->s_dev = 0;
269         
270         /* XXX flush stuff */
271         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
272
273         OPS(sb,disconnect)(ID(sb));
274
275         memset(sbi, 0, sizeof(* sbi));
276         
277         printk("OBDFS: Bye bye.\n");
278
279         MOD_DEC_USE_COUNT;
280         EXIT;
281 }
282
283 extern struct inode_operations obdfs_inode_ops;
284
285 /* all filling in of inodes postponed until lookup */
286 void obdfs_read_inode(struct inode *inode)
287 {
288         int error;
289         ENTRY;
290
291         error = IOPS(inode, getattr)(IID(inode), inode);
292         if (error) {
293                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", error);
294                 return;
295         }
296         CDEBUG(D_INODE, "ino %ld, COWFL %x\n", inode->i_ino, inode->i_flags & 0x0010000);
297         IDEBUG(inode);
298         inode->i_op = &obdfs_inode_ops;
299         return;
300 }
301
302 static void obdfs_write_inode(struct inode *inode) 
303 {
304         int error;
305         
306         error = IOPS(inode, setattr)(IID(inode), inode);
307         if (error) {
308                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", error);
309                 return;
310         }
311         
312         return;
313 }
314
315 static void obdfs_delete_inode(struct inode *inode)
316 {
317         int error;
318         ENTRY;
319
320         error = IOPS(inode, destroy)(IID(inode), inode);
321         if (error) {
322                 printk("obdfs_delete_node: ibd_destroy fails (%d)\n", error);
323                 return;
324         }
325
326         EXIT;
327 }
328
329 static int  obdfs_notify_change(struct dentry *de, struct iattr *iattr)
330 {
331         struct inode *inode = de->d_inode;
332         struct iattr saved_copy;
333         int error;
334
335         ENTRY;
336         inode_to_iattr(inode, &saved_copy);
337
338         inode_setattr(inode, iattr);
339         error = IOPS(inode, setattr)(IID(inode), inode);
340         if ( error ) {
341                 inode_setattr(inode, &saved_copy);
342                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", error);
343                 return error;
344         }
345
346         CDEBUG(D_INODE, "inode blocks now %ld\n", inode->i_blocks);
347         EXIT;
348         return error;
349 }
350
351
352 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
353                        int bufsize)
354 {
355         struct statfs tmp;
356         int error;
357
358         ENTRY;
359
360         error = OPS(sb,statfs)(ID(sb), &tmp);
361         if ( error ) { 
362                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", error);
363                 return error;
364         }
365         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
366
367         EXIT;
368
369         return error; 
370 }
371
372 struct file_system_type obdfs_fs_type = {
373    "obdfs", 0, obdfs_read_super, NULL
374 };
375
376 int init_obdfs(void)
377 {
378         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
379
380         obdfs_sysctl_init();
381
382         return register_filesystem(&obdfs_fs_type);
383 }
384
385
386 #ifdef MODULE
387 int init_module(void)
388 {
389         return init_obdfs();
390 }
391
392 void cleanup_module(void)
393 {
394         ENTRY;
395
396         obdfs_sysctl_clean();
397         unregister_filesystem(&obdfs_fs_type);
398 }
399
400 #endif