Whamcloud - gitweb
obdfs/*.c: fixed bug with flushing of cache of deleted obdo
[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 struct list_head obdfs_super_list;
36 struct super_operations obdfs_super_operations;
37
38 static char *obdfs_read_opt(const char *opt, char *data)
39 {
40         char *value;
41         char *retval;
42
43         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
44         if ( strncmp(opt, data, strlen(opt)) )
45                 return NULL;
46
47         if ( (value = strchr(data, '=')) == NULL )
48                 return NULL;
49
50         value++;
51         OBD_ALLOC(retval, char *, strlen(value) + 1);
52         if ( !retval ) {
53                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
54                 return NULL;
55         }
56         
57         memcpy(retval, value, strlen(value)+1);
58         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
59         return retval;
60 }
61
62 static void obdfs_options(char *options, char **dev, char **vers)
63 {
64         char *this_char;
65
66         if (!options)
67                 return;
68
69         for (this_char = strtok (options, ",");
70              this_char != NULL;
71              this_char = strtok (NULL, ",")) {
72                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
73                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
74                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
75                         continue;
76                 
77         }
78 }
79
80 static int obdfs_getdev(char *devpath, int *dev)
81 {
82         struct dentry *dentry;
83         kdev_t devno;
84
85         dentry = lookup_dentry(devpath, NULL, 0);
86         if (IS_ERR(dentry))
87                 return PTR_ERR(dentry);
88         
89         if (!S_ISCHR(dentry->d_inode->i_mode))
90                 return -ENODEV;
91
92         devno = dentry->d_inode->i_rdev;
93         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
94                 return -ENODEV;
95         
96         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
97                 return -ENODEV;
98
99         *dev = devno;
100         return 0;
101 }
102
103
104 static struct super_block * obdfs_read_super(struct super_block *sb, 
105                                             void *data, int silent)
106 {
107         struct inode *root = 0; 
108         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
109         struct obd_device *obddev;
110         char *device = NULL;
111         char *version = NULL;
112         int devno;
113         int err;
114         unsigned long blocksize;
115         unsigned long blocksize_bits;
116         unsigned long root_ino;
117         int scratch;
118         
119
120         ENTRY;
121         MOD_INC_USE_COUNT; 
122         
123         memset(sbi, 0, sizeof(*sbi));
124         
125         obdfs_options(data, &device, &version);
126         if ( !device ) {
127                 printk(__FUNCTION__ ": no device\n");
128                 MOD_DEC_USE_COUNT;
129                 EXIT;
130                 return NULL;
131         }
132
133         if ( (err = obdfs_getdev(device, &devno)) ) {
134                 printk("Cannot get devno of %s, error %d\n", device, err);
135                 MOD_DEC_USE_COUNT;
136                 EXIT;
137                 return NULL;
138         }
139
140         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
141                 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
142                 MOD_DEC_USE_COUNT;
143                 EXIT;
144                 return NULL;
145         }
146                 
147         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
148                 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
149                        device, MINOR(devno));
150                 MOD_DEC_USE_COUNT;
151                 EXIT;
152                 return NULL;
153         } 
154
155         obddev = &obd_dev[MINOR(devno)];
156
157         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
158              ! (obddev->obd_flags & OBD_SET_UP) ){
159                 printk("device %s not attached or not set up (%d)\n", 
160                        device, MINOR(devno));
161                 MOD_DEC_USE_COUNT;
162                 EXIT;
163                 return NULL;
164         } 
165
166         sbi->osi_obd = obddev;
167         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
168         
169         sbi->osi_conn.oc_dev = obddev;
170         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
171         if ( err ) {
172                 printk("OBDFS: cannot connect to %s\n", device);
173                 goto ERR;
174         }
175
176         /* list of dirty inodes, and a mutex to hold while modifying it */
177         INIT_LIST_HEAD(&sbi->osi_inodes);
178         sema_init(&sbi->osi_list_mutex, 1);
179
180         sbi->osi_super = sb;
181
182         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
183                                        "blocksize", &scratch,
184                                        (void *)&blocksize);
185         if ( err ) {
186                 printk("getinfo call to drive failed (blocksize)\n");
187                 goto ERR;
188         }
189
190         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
191                                        "blocksize_bits", &scratch,
192                                        (void *)&blocksize_bits);
193         if ( err ) {
194                 printk("getinfo call to drive failed (blocksize_bits)\n");
195                 goto ERR;
196         }
197
198         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
199                                        "root_ino", &scratch, (void *)&root_ino);
200         if ( err ) {
201                 printk("getinfo call to drive failed (root_ino)\n");
202                 goto ERR;
203         }
204         
205         lock_super(sb);
206         
207         sb->s_blocksize = blocksize;
208         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
209         sb->s_magic = OBDFS_SUPER_MAGIC;
210         sb->s_op = &obdfs_super_operations;
211
212         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
213
214         /* make root inode */
215         root = iget(sb, root_ino);
216         if (!root || is_bad_inode(root)) {
217             printk("OBDFS: bad iget for root\n");
218             sb->s_dev = 0;
219             err = -ENOENT;
220             unlock_super(sb);
221             goto ERR;
222         } 
223         
224         CDEBUG(D_SUPER, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
225                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
226                sb->s_dev, root->i_ino, device, MINOR(devno), 
227                blocksize, blocksize_bits);
228         sb->s_root = d_alloc_root(root);
229         list_add(&sbi->osi_list, &obdfs_super_list);
230         unlock_super(sb);
231         EXIT;  
232         return sb;
233
234 ERR:
235         EXIT;  
236         MOD_DEC_USE_COUNT;
237         if (sbi) {
238                 sbi->osi_super = NULL;
239         }
240         if (root) {
241                 iput(root);
242         }
243         sb->s_dev = 0;
244         return NULL;
245 } /* obdfs_read_super */
246
247
248 static void obdfs_put_super(struct super_block *sb)
249 {
250         struct obdfs_sb_info *sbi;
251
252         ENTRY;
253         sb->s_dev = 0;
254         
255         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
256         obdfs_flush_reqs(&sbi->osi_inodes, 0);
257
258         OPS(sb,disconnect)(ID(sb));
259         list_del(&sbi->osi_list);
260         memset(sbi, 0, sizeof(*sbi));
261         
262         printk(KERN_INFO "OBDFS: Bye bye.\n");
263
264         MOD_DEC_USE_COUNT;
265         EXIT;
266 } /* obdfs_put_super */
267
268
269 /* all filling in of inodes postponed until lookup */
270 static void obdfs_read_inode(struct inode *inode)
271 {
272         struct obdo *oa;
273
274         ENTRY;
275         oa = obdo_fromid(IID(inode), inode->i_ino,
276                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
277         if ( IS_ERR(oa) ) {
278                 printk(__FUNCTION__ ": obdo_fromid failed\n");
279                 EXIT;
280                 return /* PTR_ERR(oa) */;
281         }
282
283         ODEBUG(oa);
284         obdfs_to_inode(inode, oa);
285         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
286         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
287
288         obdo_free(oa);
289         /* OIDEBUG(inode); */
290
291         if (S_ISREG(inode->i_mode)) {
292                 inode->i_op = &obdfs_file_inode_operations;
293                 EXIT;
294         } else if (S_ISDIR(inode->i_mode)) {
295                 inode->i_op = &obdfs_dir_inode_operations;
296                 EXIT;
297         } else if (S_ISLNK(inode->i_mode)) {
298                 inode->i_op = &obdfs_symlink_inode_operations;
299                 EXIT;
300         } else {
301                 init_special_inode(inode, inode->i_mode,
302                                    /* XXX need to fill in the ext2 side */
303                                    ((long *)obdfs_i2info(inode)->oi_inline)[0]);
304         }
305
306         return;
307 } /* obdfs_read_inode */
308
309 static void obdfs_write_inode(struct inode *inode) 
310 {
311         struct obdo *oa;
312         int err;
313         
314         ENTRY;
315         oa = obdo_alloc();
316         if ( !oa ) {
317                 printk(__FUNCTION__ ": obdo_alloc failed\n");
318                 return;
319         }
320
321         oa->o_valid = OBD_MD_FLNOTOBD;
322         obdfs_from_inode(oa, inode);
323         err = IOPS(inode, setattr)(IID(inode), oa);
324
325         if ( err ) {
326                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
327                 EXIT;
328         } else {
329                 /* Copy back attributes from oa, as there may have been
330                  * changes at the target (e.g. obdo becomes a redirector
331                  * in the snapshot layer).
332                  */
333                 obdfs_to_inode(inode, oa);
334                 EXIT;
335         }
336
337         obdo_free(oa);
338 } /* obdfs_write_inode */
339
340
341 /* Dequeue cached pages for a dying inode without writing them to disk.
342  *
343  * This routine is called from iput() (for each unlink on the inode).
344  * We can't put this code into delete_inode() since that is called only
345  * when i_count == 0, and we need to keep a reference on the inode while
346  * it is in the page cache, which means i_count > 0.  Catch 22.
347  */
348 static void obdfs_put_inode(struct inode *inode)
349 {
350         struct list_head *tmp;
351
352         ENTRY;
353         if (inode->i_nlink) {
354                 EXIT;
355                 return;
356         }
357
358         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
359         tmp = obdfs_islist(inode);
360         if ( list_empty(tmp) ) {
361                 CDEBUG(D_INODE, __FUNCTION__ ": no dirty pages for inode %ld\n",
362                        inode->i_ino);
363                 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
364                 EXIT;
365                 return;
366         }
367
368         /* take it out of the super list */
369         list_del(tmp);
370         INIT_LIST_HEAD(obdfs_islist(inode));
371
372         tmp = obdfs_iplist(inode);
373         while ( (tmp = tmp->next) != obdfs_iplist(inode) ) {
374                 struct obdfs_pgrq *req;
375                 struct page *page;
376                 
377                 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
378                 page = req->rq_page;
379                 /* take it out of the list and free */
380                 obdfs_pgrq_del(req);
381                 /* now put the page away */
382                 put_page(page);
383         }
384
385         /* decrement inode reference for page cache */
386         inode->i_count--;
387
388         EXIT;
389         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
390 } /* obdfs_put_inode */
391
392
393 static void obdfs_delete_inode(struct inode *inode)
394 {
395         struct obdo *oa;
396         int err;
397         ENTRY;
398
399         oa = obdo_alloc();
400         if ( !oa ) {
401                 printk(__FUNCTION__ ": obdo_alloc failed\n");
402                 return;
403         }
404         oa->o_valid = OBD_MD_FLNOTOBD;
405         obdfs_from_inode(oa, inode);
406
407         err = IOPS(inode, destroy)(IID(inode), oa);
408         obdo_free(oa);
409
410         if (err) {
411                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
412                 EXIT;
413                 return;
414         }
415
416         EXIT;
417 } /* obdfs_delete_inode */
418
419
420 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
421 {
422         struct inode *inode = de->d_inode;
423         struct obdo *oa;
424         int err;
425
426         ENTRY;
427         oa = obdo_alloc();
428         if ( !oa ) {
429                 printk(__FUNCTION__ ": obdo_alloc failed\n");
430                 return -ENOMEM;
431         }
432
433         oa->o_id = inode->i_ino;
434         obdo_from_iattr(oa, attr);
435         err = IOPS(inode, setattr)(IID(inode), oa);
436
437         if ( err ) {
438                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
439                 EXIT;
440         } else {
441                 /* Copy back attributes from oa, as there may have been
442                  * changes at the target (e.g. obdo becomes a redirector
443                  * in the snapshot layer).
444                  */
445                 obdfs_to_inode(inode, oa);
446                 EXIT;
447         }
448
449         obdo_free(oa);
450         return err;
451 } /* obdfs_notify_change */
452
453
454 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
455                        int bufsize)
456 {
457         struct statfs tmp;
458         int err;
459
460         ENTRY;
461
462         err = OPS(sb,statfs)(ID(sb), &tmp);
463         if ( err ) { 
464                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
465                 return err;
466         }
467         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
468
469         EXIT;
470
471         return err; 
472 }
473
474 /* exported operations */
475 struct super_operations obdfs_super_operations =
476 {
477         obdfs_read_inode,       /* read_inode */
478         obdfs_write_inode,      /* write_inode */
479         obdfs_put_inode,        /* put_inode */
480         obdfs_delete_inode,     /* delete_inode */
481         obdfs_notify_change,    /* notify_change */
482         obdfs_put_super,        /* put_super */
483         NULL,                   /* write_super */
484         obdfs_statfs,           /* statfs */
485         NULL                    /* remount_fs */
486 };
487
488 struct file_system_type obdfs_fs_type = {
489    "obdfs", 0, obdfs_read_super, NULL
490 };
491
492 int init_obdfs(void)
493 {
494         int err;
495
496         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
497
498         obdfs_sysctl_init();
499
500         INIT_LIST_HEAD(&obdfs_super_list);
501         err = obdfs_init_pgrqcache();
502         if (err)
503                 return err;
504
505         obdfs_flushd_init();
506         return register_filesystem(&obdfs_fs_type);
507 }
508
509
510 #ifdef MODULE
511 int init_module(void)
512 {
513         return init_obdfs();
514 }
515
516 void cleanup_module(void)
517 {
518         ENTRY;
519
520         obdfs_flushd_cleanup();
521         obdfs_sysctl_clean();
522         obdfs_cleanup_pgrqcache();
523         unregister_filesystem(&obdfs_fs_type);
524
525         EXIT;
526 }
527
528 #endif