Whamcloud - gitweb
Numerous changes:
[fs/lustre-release.git] / lustre / obdfs / 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  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/errno.h>
21 #include <linux/locks.h>
22 #include <linux/unistd.h>
23
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
26
27 #include <linux/fs.h>
28 #include <linux/stat.h>
29 #include <asm/uaccess.h>
30 #include <linux/vmalloc.h>
31 #include <asm/segment.h>
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obdfs.h>
36
37 struct list_head obdfs_super_list;
38 struct address_space_operations obdfs_aops;
39 struct super_operations obdfs_super_operations;
40 long obdfs_cache_count = 0;
41 long obdfs_mutex_start = 0;
42 long obd_memory = 0;
43
44 static char *obdfs_read_opt(const char *opt, char *data)
45 {
46         char *value;
47         char *retval;
48
49         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
50         if ( strncmp(opt, data, strlen(opt)) )
51                 return NULL;
52
53         if ( (value = strchr(data, '=')) == NULL )
54                 return NULL;
55
56         value++;
57         OBD_ALLOC(retval, char *, strlen(value) + 1);
58         if ( !retval ) {
59                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
60                 return NULL;
61         }
62         
63         memcpy(retval, value, strlen(value)+1);
64         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
65         return retval;
66 }
67
68 static void obdfs_options(char *options, char **dev, char **vers)
69 {
70         char *this_char;
71
72         if (!options)
73                 return;
74
75         for (this_char = strtok (options, ",");
76              this_char != NULL;
77              this_char = strtok (NULL, ",")) {
78                 CDEBUG(D_INFO, "this_char %s\n", this_char);
79                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
80                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
81                         continue;
82                 
83         }
84 }
85
86 static int obdfs_getdev(char *devpath, int *dev)
87 {
88         struct dentry *dentry;
89         kdev_t devno;
90         struct nameidata nd;
91         int error = 0; 
92
93         ENTRY;
94         if (path_init(devpath, LOOKUP_POSITIVE, &nd))
95                 error = path_walk(devpath, &nd);
96         if (error)
97                 return error;
98
99         dentry = nd.dentry;
100         if (!S_ISCHR(dentry->d_inode->i_mode))
101                 return -ENODEV;
102
103         devno = dentry->d_inode->i_rdev;
104         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
105                 return -ENODEV;
106         
107         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
108                 return -ENODEV;
109
110         *dev = devno;
111         return 0;
112 }
113
114
115 static struct super_block * obdfs_read_super(struct super_block *sb, 
116                                             void *data, int silent)
117 {
118         struct inode *root = 0; 
119         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
120         struct obd_device *obddev;
121         char *device = NULL;
122         char *version = NULL;
123         int devno;
124         int err;
125         unsigned long blocksize;
126         unsigned long blocksize_bits;
127         unsigned long root_ino;
128         int scratch;
129         struct obdo *oa;
130         
131
132         ENTRY;
133         MOD_INC_USE_COUNT; 
134         
135         memset(sbi, 0, sizeof(*sbi));
136         
137         CDEBUG(D_INFO, "\n"); 
138         obdfs_options(data, &device, &version);
139         if ( !device ) {
140                 printk(__FUNCTION__ ": no device\n");
141                 EXIT;
142                 goto ERR;
143         }
144
145         CDEBUG(D_INFO, "\n"); 
146         if ( (err = obdfs_getdev(device, &devno)) ) {
147                 printk("Cannot get devno of %s, error %d\n", device, err);
148                 EXIT;
149                 goto ERR;;
150         }
151
152         CDEBUG(D_INFO, "\n"); 
153         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
154                 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
155                 EXIT;
156                 goto ERR;
157         }
158                 
159         CDEBUG(D_INFO, "\n"); 
160         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
161                 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
162                        device, MINOR(devno));
163                 EXIT;
164                 goto ERR;
165         } 
166
167         CDEBUG(D_INFO, "\n"); 
168         obddev = &obd_dev[MINOR(devno)];
169
170         CDEBUG(D_INFO, "\n"); 
171         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
172              ! (obddev->obd_flags & OBD_SET_UP) ){
173                 printk("device %s not attached or not set up (%d)\n", 
174                        device, MINOR(devno));
175                 EXIT;
176                 goto ERR;;
177         } 
178
179         CDEBUG(D_INFO, "\n"); 
180         sbi->osi_obd = obddev;
181         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
182         
183         sbi->osi_conn.oc_dev = obddev;
184         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
185         if ( err ) {
186                 printk("OBDFS: cannot connect to %s\n", device);
187                 EXIT;
188                 goto ERR;
189         }
190
191         CDEBUG(D_INFO, "\n"); 
192         /* list of dirty inodes, and a mutex to hold while modifying it */
193         INIT_LIST_HEAD(&sbi->osi_inodes);
194         init_MUTEX (&sbi->osi_list_mutex);
195
196         CDEBUG(D_INFO, "\n"); 
197         sbi->osi_super = sb;
198
199         CDEBUG(D_INFO, "\n"); 
200         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
201                                        "blocksize", &scratch,
202                                        (void *)&blocksize);
203         if ( err ) {
204                 printk("getinfo call to drive failed (blocksize)\n");
205                 EXIT;
206                 goto ERR;
207         }
208
209         CDEBUG(D_INFO, "\n"); 
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                 EXIT;
216                 goto ERR;
217         }
218
219         CDEBUG(D_INFO, "\n"); 
220         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
221                                        "root_ino", &scratch, (void *)&root_ino);
222         if ( err ) {
223                 printk("getinfo call to drive failed (root_ino)\n");
224                 EXIT;
225                 goto ERR;
226         }
227         
228         CDEBUG(D_INFO, "\n"); 
229         sb->s_maxbytes = 1LL << 36;
230         printk("Max bytes: %Lx\n", sb->s_maxbytes);
231         sb->s_blocksize = PAGE_SIZE;
232         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
233         sb->s_magic = OBDFS_SUPER_MAGIC;
234         sb->s_op = &obdfs_super_operations;
235
236         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
237
238         /* make root inode */
239         CDEBUG(D_INFO, "\n"); 
240         oa = obdo_fromid(&sbi->osi_conn, root_ino,
241                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
242         CDEBUG(D_INFO, "\n"); 
243         if ( IS_ERR(oa) ) {
244                 printk(__FUNCTION__ ": obdo_fromid failed\n");
245                 iput(root); 
246                 EXIT;
247                 goto ERR;
248         }
249         CDEBUG(D_INFO, "\n"); 
250         root = iget4(sb, root_ino, NULL, oa);
251         obdo_free(oa);
252         CDEBUG(D_INFO, "\n"); 
253         if (!root) {
254             printk("OBDFS: bad iget4 for root\n");
255             sb->s_dev = 0;
256             err = -ENOENT;
257             EXIT;
258             goto ERR;
259         } 
260         
261         CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
262                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
263                sb->s_dev, root->i_ino, device, MINOR(devno), 
264                blocksize, blocksize_bits);
265         sb->s_root = d_alloc_root(root);
266         list_add(&sbi->osi_list, &obdfs_super_list);
267         OBD_FREE(device, strlen(device) + 1);
268         if (version)
269                 OBD_FREE(version, strlen(version) + 1);
270         EXIT;  
271         return sb;
272
273 ERR:
274         MOD_DEC_USE_COUNT;
275         if (device)
276                 OBD_FREE(device, strlen(device) + 1);
277         if (version)
278                 OBD_FREE(version, strlen(version) + 1);
279         if (sbi) {
280                 sbi->osi_super = NULL;
281         }
282         if (root) {
283                 iput(root);
284         }
285         sb->s_dev = 0;
286         return NULL;
287 } /* obdfs_read_super */
288
289
290 static void obdfs_put_super(struct super_block *sb)
291 {
292         struct obdfs_sb_info *sbi;
293
294         ENTRY;
295         sb->s_dev = 0;
296         
297         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
298         //obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
299
300         OPS(sb,disconnect)(ID(sb));
301         list_del(&sbi->osi_list);
302         
303         printk(KERN_INFO "OBDFS: Bye bye.\n");
304
305         MOD_DEC_USE_COUNT;
306         EXIT;
307 } /* obdfs_put_super */
308
309
310 void obdfs_do_change_inode(struct inode *inode, int valid)
311 {
312         struct obdo *oa;
313         int err;
314         
315         ENTRY;
316         if (IOPS(inode, setattr) == NULL) {
317                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
318                 EXIT;
319                 return;
320         }
321         oa = obdo_alloc();
322         if ( !oa ) {
323                 printk(__FUNCTION__ ": obdo_alloc failed\n");
324                 EXIT;
325                 return;
326         }
327
328         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
329         obdfs_from_inode(oa, inode);
330         err = IOPS(inode, setattr)(IID(inode), oa);
331
332         if ( err )
333                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
334
335         EXIT;
336         obdo_free(oa);
337 } /* obdfs_write_inode */
338
339 void obdfs_change_inode(struct inode *inode, int mask)
340 {
341         return obdfs_do_change_inode(inode, OBD_MD_FLNLINK); 
342 }
343
344
345 extern void write_inode_pages(struct inode *);
346 /* This routine is called from iput() (for each unlink on the inode).
347  * We can't put this call into delete_inode() since that is called only
348  * when i_count == 0, and we need to keep a reference on the inode while
349  * it is in the page cache, which means i_count > 0.  Catch 22.
350  */
351 static void obdfs_put_inode(struct inode *inode)
352 {
353         ENTRY;
354         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
355                 write_inode_pages(inode);
356                 EXIT;
357                 return;
358         }
359
360         //obdfs_dequeue_pages(inode);
361         EXIT;
362 } /* obdfs_put_inode */
363
364
365 static void obdfs_delete_inode(struct inode *inode)
366 {
367         obdfs_do_change_inode(inode, ~0);
368         clear_inode(inode); 
369 }
370 #if 0
371 {
372         struct obdo *oa;
373         int err;
374
375         ENTRY;
376         if (IOPS(inode, destroy) == NULL) {
377                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
378                 EXIT;
379                 return;
380         }
381
382         oa = obdo_alloc();
383         if ( !oa ) {
384                 printk(__FUNCTION__ ": obdo_alloc failed\n");
385                 EXIT;
386                 return;
387         }
388         oa->o_valid = OBD_MD_FLNOTOBD;
389         obdfs_from_inode(oa, inode);
390
391         /* XXX how do we know that this inode is now clean? */
392         printk("delete_inode ------> link %d\n", inode->i_nlink);
393         ODEBUG(oa);
394         err = IOPS(inode, destroy)(IID(inode), oa);
395         obdo_free(oa);
396         clear_inode(inode);
397         if (err) {
398                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
399                 EXIT;
400                 return;
401         }
402
403         EXIT;
404 } /* obdfs_delete_inode */
405 #endif
406
407
408 static int obdfs_attr2inode(struct inode * inode, struct iattr * attr)
409 {
410         unsigned int ia_valid = attr->ia_valid;
411         int error = 0;
412
413         if (ia_valid & ATTR_SIZE) {
414                 error = vmtruncate(inode, attr->ia_size);
415                 if (error)
416                         goto out;
417         }
418
419         if (ia_valid & ATTR_UID)
420                 inode->i_uid = attr->ia_uid;
421         if (ia_valid & ATTR_GID)
422                 inode->i_gid = attr->ia_gid;
423         if (ia_valid & ATTR_ATIME)
424                 inode->i_atime = attr->ia_atime;
425         if (ia_valid & ATTR_MTIME)
426                 inode->i_mtime = attr->ia_mtime;
427         if (ia_valid & ATTR_CTIME)
428                 inode->i_ctime = attr->ia_ctime;
429         if (ia_valid & ATTR_MODE) {
430                 inode->i_mode = attr->ia_mode;
431                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
432                         inode->i_mode &= ~S_ISGID;
433         }
434 out:
435         return error;
436 }
437
438 int obdfs_setattr(struct dentry *de, struct iattr *attr)
439 {
440         struct inode *inode = de->d_inode;
441         struct obdo *oa;
442         int err;
443
444         ENTRY;
445         if (IOPS(inode, setattr) == NULL) {
446                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
447                 EXIT;
448                 return -EIO;
449         }
450         oa = obdo_alloc();
451         if ( !oa ) {
452                 printk(__FUNCTION__ ": obdo_alloc failed\n");
453                 return -ENOMEM;
454         }
455
456         obdfs_attr2inode(inode, attr);
457         oa->o_id = inode->i_ino;
458         obdo_from_iattr(oa, attr);
459         err = IOPS(inode, setattr)(IID(inode), oa);
460
461         if ( err )
462                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
463
464         EXIT;
465         obdo_free(oa);
466         return err;
467 } /* obdfs_setattr */
468
469
470
471 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
472 {
473         struct statfs tmp;
474         int err;
475
476         ENTRY;
477
478         err = OPS(sb,statfs)(ID(sb), &tmp);
479         if ( err ) { 
480                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
481                 return err;
482         }
483         memcpy(buf, &tmp, sizeof(*buf));
484         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
485         EXIT;
486
487         return err; 
488 }
489
490 static inline void obdfs_read_inode2(struct inode *inode, void *opaque)
491 {
492         struct obdo *oa = opaque; 
493         
494         ENTRY;
495         obdfs_to_inode(inode, oa); 
496
497         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
498         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
499
500         /* OIDEBUG(inode); */
501
502         if (S_ISREG(inode->i_mode)) {
503                 inode->i_op = &obdfs_file_inode_operations;
504                 inode->i_fop = &obdfs_file_operations;
505                 inode->i_mapping->a_ops = &obdfs_aops;
506                 EXIT;
507         } else if (S_ISDIR(inode->i_mode)) {
508                 inode->i_op = &obdfs_dir_inode_operations;
509                 inode->i_fop = &obdfs_dir_operations; 
510                 inode->i_mapping->a_ops = &obdfs_aops;
511                 EXIT;
512         } else if (S_ISLNK(inode->i_mode)) {
513                 if (inode->i_blocks) { 
514                         inode->i_op = &obdfs_symlink_inode_operations;
515                         inode->i_mapping->a_ops = &obdfs_aops;
516                 }else {
517                         inode->i_op = &obdfs_fast_symlink_inode_operations;
518                 }
519                 EXIT;
520         } else {
521                 init_special_inode(inode, inode->i_mode,
522                                    ((int *)obdfs_i2info(inode)->oi_inline)[0]);
523         }
524
525         EXIT;
526         return;
527 }
528
529 /* exported operations */
530 struct super_operations obdfs_super_operations =
531 {
532         read_inode2: obdfs_read_inode2,
533         put_inode: obdfs_put_inode,
534         delete_inode: obdfs_delete_inode,
535         put_super: obdfs_put_super,
536         statfs: obdfs_statfs
537 };
538
539
540 struct file_system_type obdfs_fs_type = {
541    "obdfs", 0, obdfs_read_super, NULL
542 };
543
544 int init_obdfs(void)
545 {
546         int err;
547
548         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
549
550         obdfs_sysctl_init();
551
552         INIT_LIST_HEAD(&obdfs_super_list);
553         //err = obdfs_init_pgrqcache();
554         //if (err)
555         //return err;
556
557         //obdfs_flushd_init();
558         return register_filesystem(&obdfs_fs_type);
559 }
560
561
562
563
564 #ifdef MODULE
565 int init_module(void)
566 {
567         return init_obdfs();
568 }
569
570 void cleanup_module(void)
571 {
572         ENTRY;
573
574         //obdfs_flushd_cleanup();
575         obdfs_sysctl_clean();
576         //obdfs_cleanup_pgrqcache();
577         unregister_filesystem(&obdfs_fs_type);
578         CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);
579         EXIT;
580 }
581
582 #endif