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