Whamcloud - gitweb
Another very major cleanup:
[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 #define EXPORT_SYMTAB
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/string.h>
21 #include <linux/stat.h>
22 #include <linux/errno.h>
23 #include <linux/locks.h>
24 #include <linux/unistd.h>
25
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28
29 #include <linux/fs.h>
30 #include <linux/stat.h>
31 #include <asm/uaccess.h>
32 #include <linux/vmalloc.h>
33 #include <asm/segment.h>
34
35 #include <linux/obd_support.h>
36 #include <linux/obd_class.h>
37 #include <linux/obdfs.h>
38
39 struct list_head obdfs_super_list;
40 struct address_space_operations obdfs_aops;
41 struct super_operations obdfs_super_operations;
42 long obdfs_cache_count = 0;
43 long obdfs_mutex_start = 0;
44 long obd_memory = 0;
45
46 static char *obdfs_read_opt(const char *opt, char *data)
47 {
48         char *value;
49         char *retval;
50
51         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
52         if ( strncmp(opt, data, strlen(opt)) )
53                 return NULL;
54
55         if ( (value = strchr(data, '=')) == NULL )
56                 return NULL;
57
58         value++;
59         OBD_ALLOC(retval, char *, strlen(value) + 1);
60         if ( !retval ) {
61                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
62                 return NULL;
63         }
64         
65         memcpy(retval, value, strlen(value)+1);
66         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
67         return retval;
68 }
69
70 static void obdfs_options(char *options, char **dev, char **vers)
71 {
72         char *this_char;
73
74         if (!options)
75                 return;
76
77         for (this_char = strtok (options, ",");
78              this_char != NULL;
79              this_char = strtok (NULL, ",")) {
80                 CDEBUG(D_INFO, "this_char %s\n", this_char);
81                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
82                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
83                         continue;
84                 
85         }
86 }
87
88 static int obdfs_getdev(char *devpath, int *dev)
89 {
90         struct dentry *dentry;
91         kdev_t devno;
92         struct nameidata nd;
93         int error = 0; 
94
95         ENTRY;
96         if (path_init(devpath, LOOKUP_POSITIVE, &nd))
97                 error = path_walk(devpath, &nd);
98         if (error)
99                 return error;
100
101         dentry = nd.dentry;
102         if (!S_ISCHR(dentry->d_inode->i_mode))
103                 return -ENODEV;
104
105         devno = dentry->d_inode->i_rdev;
106         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
107                 return -ENODEV;
108         
109         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
110                 return -ENODEV;
111
112         *dev = devno;
113         return 0;
114 }
115
116
117 static struct super_block * obdfs_read_super(struct super_block *sb, 
118                                             void *data, int silent)
119 {
120         struct inode *root = 0; 
121         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
122         struct obd_device *obddev;
123         char *device = NULL;
124         char *version = NULL;
125         int devno;
126         int err;
127         unsigned long blocksize;
128         unsigned long blocksize_bits;
129         unsigned long root_ino;
130         int scratch;
131         struct obdo *oa;
132         
133
134         ENTRY;
135         MOD_INC_USE_COUNT; 
136         
137         memset(sbi, 0, sizeof(*sbi));
138         
139         CDEBUG(D_INFO, "\n"); 
140         obdfs_options(data, &device, &version);
141         if ( !device ) {
142                 printk(__FUNCTION__ ": no device\n");
143                 EXIT;
144                 goto ERR;
145         }
146
147         CDEBUG(D_INFO, "\n"); 
148         if ( (err = obdfs_getdev(device, &devno)) ) {
149                 printk("Cannot get devno of %s, error %d\n", device, err);
150                 EXIT;
151                 goto ERR;;
152         }
153
154         CDEBUG(D_INFO, "\n"); 
155         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
156                 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
157                 EXIT;
158                 goto ERR;
159         }
160                 
161         CDEBUG(D_INFO, "\n"); 
162         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
163                 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
164                        device, MINOR(devno));
165                 EXIT;
166                 goto ERR;
167         } 
168
169         CDEBUG(D_INFO, "\n"); 
170         obddev = &obd_dev[MINOR(devno)];
171
172         CDEBUG(D_INFO, "\n"); 
173         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
174              ! (obddev->obd_flags & OBD_SET_UP) ){
175                 printk("device %s not attached or not set up (%d)\n", 
176                        device, MINOR(devno));
177                 EXIT;
178                 goto ERR;;
179         } 
180
181         CDEBUG(D_INFO, "\n"); 
182         sbi->osi_obd = obddev;
183         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
184         
185         sbi->osi_conn.oc_dev = obddev;
186         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
187         if ( err ) {
188                 printk("OBDFS: cannot connect to %s\n", device);
189                 EXIT;
190                 goto ERR;
191         }
192
193         CDEBUG(D_INFO, "\n"); 
194         /* list of dirty inodes, and a mutex to hold while modifying it */
195         INIT_LIST_HEAD(&sbi->osi_inodes);
196         init_MUTEX (&sbi->osi_list_mutex);
197
198         CDEBUG(D_INFO, "\n"); 
199         sbi->osi_super = sb;
200
201         CDEBUG(D_INFO, "\n"); 
202         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
203                                        "blocksize", &scratch,
204                                        (void *)&blocksize);
205         if ( err ) {
206                 printk("getinfo call to drive failed (blocksize)\n");
207                 EXIT;
208                 goto ERR;
209         }
210
211         CDEBUG(D_INFO, "\n"); 
212         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
213                                        "blocksize_bits", &scratch,
214                                        (void *)&blocksize_bits);
215         if ( err ) {
216                 printk("getinfo call to drive failed (blocksize_bits)\n");
217                 EXIT;
218                 goto ERR;
219         }
220
221         CDEBUG(D_INFO, "\n"); 
222         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
223                                        "root_ino", &scratch, (void *)&root_ino);
224         if ( err ) {
225                 printk("getinfo call to drive failed (root_ino)\n");
226                 EXIT;
227                 goto ERR;
228         }
229         
230         CDEBUG(D_INFO, "\n"); 
231         sb->s_blocksize = blocksize;
232         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
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 mask)
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 & (~mask);
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
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) {
355                 EXIT;
356                 return;
357         }
358
359         obdfs_dequeue_pages(inode);
360         EXIT;
361 } /* obdfs_put_inode */
362
363
364 static void obdfs_delete_inode(struct inode *inode)
365 {
366         obdfs_do_change_inode(inode, 0);
367         clear_inode(inode); 
368 }
369 #if 0
370 {
371         struct obdo *oa;
372         int err;
373
374         ENTRY;
375         if (IOPS(inode, destroy) == NULL) {
376                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
377                 EXIT;
378                 return;
379         }
380
381         oa = obdo_alloc();
382         if ( !oa ) {
383                 printk(__FUNCTION__ ": obdo_alloc failed\n");
384                 EXIT;
385                 return;
386         }
387         oa->o_valid = OBD_MD_FLNOTOBD;
388         obdfs_from_inode(oa, inode);
389
390         /* XXX how do we know that this inode is now clean? */
391         printk("delete_inode ------> link %d\n", inode->i_nlink);
392         ODEBUG(oa);
393         err = IOPS(inode, destroy)(IID(inode), oa);
394         obdo_free(oa);
395         clear_inode(inode);
396         if (err) {
397                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
398                 EXIT;
399                 return;
400         }
401
402         EXIT;
403 } /* obdfs_delete_inode */
404 #endif
405
406
407 int inode_copy_attr(struct inode * inode, struct iattr * attr)
408 {
409         unsigned int ia_valid = attr->ia_valid;
410         int error = 0;
411
412         if (ia_valid & ATTR_SIZE) {
413                 error = vmtruncate(inode, attr->ia_size);
414                 if (error)
415                         goto out;
416         }
417
418         if (ia_valid & ATTR_UID)
419                 inode->i_uid = attr->ia_uid;
420         if (ia_valid & ATTR_GID)
421                 inode->i_gid = attr->ia_gid;
422         if (ia_valid & ATTR_ATIME)
423                 inode->i_atime = attr->ia_atime;
424         if (ia_valid & ATTR_MTIME)
425                 inode->i_mtime = attr->ia_mtime;
426         if (ia_valid & ATTR_CTIME)
427                 inode->i_ctime = attr->ia_ctime;
428         if (ia_valid & ATTR_MODE) {
429                 inode->i_mode = attr->ia_mode;
430                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
431                         inode->i_mode &= ~S_ISGID;
432         }
433 out:
434         return error;
435 }
436
437 int obdfs_setattr(struct dentry *de, struct iattr *attr)
438 {
439         struct inode *inode = de->d_inode;
440         struct obdo *oa;
441         int err;
442
443         ENTRY;
444         if (IOPS(inode, setattr) == NULL) {
445                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
446                 EXIT;
447                 return -EIO;
448         }
449         oa = obdo_alloc();
450         if ( !oa ) {
451                 printk(__FUNCTION__ ": obdo_alloc failed\n");
452                 return -ENOMEM;
453         }
454
455         inode_copy_attr(inode, attr);
456         oa->o_id = inode->i_ino;
457         obdo_from_iattr(oa, attr);
458         err = IOPS(inode, setattr)(IID(inode), oa);
459
460         if ( err )
461                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
462
463         EXIT;
464         obdo_free(oa);
465         return err;
466 } /* obdfs_setattr */
467
468
469
470 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
471 {
472         struct statfs tmp;
473         int err;
474
475         ENTRY;
476
477         err = OPS(sb,statfs)(ID(sb), &tmp);
478         if ( err ) { 
479                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
480                 return err;
481         }
482         memcpy(buf, &tmp, sizeof(*buf));
483         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
484         EXIT;
485
486         return err; 
487 }
488
489 static inline void obdfs_read_inode2(struct inode *inode, void *opaque)
490 {
491         struct obdo *oa = opaque; 
492         
493         ENTRY;
494         obdfs_to_inode(inode, oa); 
495
496         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
497         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
498
499         /* OIDEBUG(inode); */
500
501         if (S_ISREG(inode->i_mode)) {
502                 inode->i_op = &obdfs_file_inode_operations;
503                 inode->i_fop = &obdfs_file_operations;
504                 inode->i_mapping->a_ops = &obdfs_aops;
505                 EXIT;
506         } else if (S_ISDIR(inode->i_mode)) {
507                 inode->i_op = &obdfs_dir_inode_operations;
508                 inode->i_fop = &obdfs_dir_operations; 
509                 inode->i_mapping->a_ops = &obdfs_aops;
510                 EXIT;
511         } else if (S_ISLNK(inode->i_mode)) {
512                 if (inode->i_blocks) { 
513                         inode->i_op = &obdfs_symlink_inode_operations;
514                         inode->i_mapping->a_ops = &obdfs_aops;
515                 }else {
516                         inode->i_op = &obdfs_fast_symlink_inode_operations;
517                 }
518                 EXIT;
519         } else {
520                 init_special_inode(inode, inode->i_mode,
521                                    ((int *)obdfs_i2info(inode)->oi_inline)[0]);
522         }
523
524         EXIT;
525         return;
526 }
527
528 /* exported operations */
529 struct super_operations obdfs_super_operations =
530 {
531         read_inode2: obdfs_read_inode2,
532         put_inode: obdfs_put_inode,
533         delete_inode: obdfs_delete_inode,
534         put_super: obdfs_put_super,
535         statfs: obdfs_statfs
536 };
537
538
539 struct file_system_type obdfs_fs_type = {
540    "obdfs", 0, obdfs_read_super, NULL
541 };
542
543 int init_obdfs(void)
544 {
545         int err;
546
547         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
548
549         obdfs_sysctl_init();
550
551         INIT_LIST_HEAD(&obdfs_super_list);
552         err = obdfs_init_pgrqcache();
553         if (err)
554                 return err;
555
556         obdfs_flushd_init();
557         return register_filesystem(&obdfs_fs_type);
558 }
559
560
561 struct address_space_operations obdfs_aops = {
562         readpage: obdfs_readpage,
563         writepage: obdfs_writepage,
564         sync_page: block_sync_page,
565         prepare_write: obdfs_prepare_write, 
566         commit_write: obdfs_commit_write,
567         bmap: NULL
568 };
569
570
571 #ifdef MODULE
572 int init_module(void)
573 {
574         return init_obdfs();
575 }
576
577 void cleanup_module(void)
578 {
579         ENTRY;
580
581         obdfs_flushd_cleanup();
582         obdfs_sysctl_clean();
583         obdfs_cleanup_pgrqcache();
584         unregister_filesystem(&obdfs_fs_type);
585         CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);
586         EXIT;
587 }
588
589 #endif