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