Whamcloud - gitweb
Small fixes to the request processing.
[fs/lustre-release.git] / lustre / llite / 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/lustre_light.h>
38
39 struct list_head ll_super_list;
40 extern struct address_space_operations ll_aops;
41 struct super_operations ll_super_operations;
42 long ll_cache_count = 0;
43 long ll_mutex_start = 0;
44 long obd_memory = 0;
45
46 static char *ll_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 ll_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 = ll_read_opt("device", this_char)))||
82                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
83                         continue;
84                 
85         }
86 }
87
88 static struct super_block * ll_read_super(struct super_block *sb, 
89                                             void *data, int silent)
90 {
91         struct inode *root = 0; 
92         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
93         struct obd_device *obddev;
94         char *device = NULL;
95         char *version = NULL;
96         int root_ino = 2;
97         int connected = 0;
98         int devno;
99         int err;
100         struct obdo *oa;
101         
102
103         ENTRY;
104         MOD_INC_USE_COUNT; 
105         memset(sbi, 0, sizeof(*sbi));
106         
107         CDEBUG(D_INFO, "\n"); 
108         ll_options(data, &device, &version);
109         if ( !device ) {
110                 printk(__FUNCTION__ ": no device\n");
111                 EXIT;
112                 goto ERR;
113         }
114
115         devno = simple_strtoul(device, NULL, 0);
116         CDEBUG(D_INFO, "\n"); 
117         if ( devno >= MAX_OBD_DEVICES ) {
118                 printk(__FUNCTION__ ": device of %s too high (%d)\n", device, devno);
119                 EXIT;
120                 goto ERR;
121         } 
122
123         CDEBUG(D_INFO, "\n"); 
124         obddev = &obd_dev[devno];
125
126
127         CDEBUG(D_INFO, "\n"); 
128         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
129              ! (obddev->obd_flags & OBD_SET_UP) ){
130                 printk("device %s not attached or not set up (%d)\n", 
131                        device, MINOR(devno));
132                 EXIT;
133                 goto ERR;;
134         } 
135
136         CDEBUG(D_INFO, "\n"); 
137         sbi->ll_obd = obddev;
138         sbi->ll_ops = sbi->ll_obd->obd_type->typ_ops;
139         
140         sbi->ll_conn.oc_dev = obddev;
141         err = sbi->ll_ops->o_connect(&sbi->ll_conn);
142         if ( err ) {
143                 printk("OBDFS: cannot connect to %s\n", device);
144                 EXIT;
145                 goto ERR;
146         }
147
148         connected = 1;
149         CDEBUG(D_INFO, "\n"); 
150         /* list of dirty inodes, and a mutex to hold while modifying it */
151         INIT_LIST_HEAD(&sbi->ll_inodes);
152         init_MUTEX (&sbi->ll_list_mutex);
153
154         CDEBUG(D_INFO, "\n"); 
155         sbi->ll_super = sb;
156         sbi->ll_rootino = 2;
157         
158         CDEBUG(D_INFO, "\n"); 
159         sb->s_maxbytes = 1LL << 36;
160         printk("Max bytes: %Lx\n", sb->s_maxbytes);
161         sb->s_blocksize = PAGE_SIZE;
162         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
163         sb->s_magic = LL_SUPER_MAGIC;
164         sb->s_op = &ll_super_operations;
165
166         /* make root inode */
167         CDEBUG(D_INFO, "\n"); 
168         oa = obdo_fromid(&sbi->ll_conn, root_ino, S_IFDIR,
169                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
170         CDEBUG(D_INFO, "mode %o\n", oa->o_mode); 
171         if ( IS_ERR(oa) ) {
172                 printk(__FUNCTION__ ": obdo_fromid failed\n");
173                 iput(root); 
174                 EXIT;
175                 goto ERR;
176         }
177         CDEBUG(D_INFO, "\n"); 
178         root = iget4(sb, root_ino, NULL, oa);
179         obdo_free(oa);
180         CDEBUG(D_INFO, "\n"); 
181         if (!root) {
182             printk("OBDFS: bad iget4 for root\n");
183             sb->s_dev = 0;
184             err = -ENOENT;
185             EXIT;
186             goto ERR;
187         } 
188         
189         sb->s_root = d_alloc_root(root);
190         list_add(&sbi->ll_list, &ll_super_list);
191         OBD_FREE(device, strlen(device) + 1);
192         if (version)
193                 OBD_FREE(version, strlen(version) + 1);
194         EXIT;  
195         return sb;
196
197 ERR:
198         MOD_DEC_USE_COUNT;
199         if (device)
200                 OBD_FREE(device, strlen(device) + 1);
201         if (version)
202                 OBD_FREE(version, strlen(version) + 1);
203         if (connected) 
204                 sbi->ll_ops->o_disconnect(&sbi->ll_conn);
205
206         if (sbi) {
207                 sbi->ll_super = NULL;
208         }
209         if (root) {
210                 iput(root);
211         }
212         sb->s_dev = 0;
213         return NULL;
214 } /* ll_read_super */
215
216
217 static void ll_put_super(struct super_block *sb)
218 {
219         struct ll_sb_info *sbi;
220
221         ENTRY;
222         sb->s_dev = 0;
223         
224         sbi = (struct ll_sb_info *) &sb->u.generic_sbp;
225         //ll_flush_reqs(&sbi->ll_inodes, ~0UL);
226
227         OPS(sb,disconnect)(ID(sb));
228         list_del(&sbi->ll_list);
229         
230         printk(KERN_INFO "OBDFS: Bye bye.\n");
231
232         MOD_DEC_USE_COUNT;
233         EXIT;
234 } /* ll_put_super */
235
236
237 void ll_do_change_inode(struct inode *inode, int valid)
238 {
239         struct obdo *oa;
240         int err;
241         
242         ENTRY;
243         if (IOPS(inode, setattr) == NULL) {
244                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
245                 EXIT;
246                 return;
247         }
248         oa = obdo_alloc();
249         if ( !oa ) {
250                 printk(__FUNCTION__ ": obdo_alloc failed\n");
251                 EXIT;
252                 return;
253         }
254
255         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
256         ll_from_inode(oa, inode);
257         oa->o_mode = inode->i_mode;
258         err = IOPS(inode, setattr)(IID(inode), oa);
259
260         if ( err )
261                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
262
263         EXIT;
264         obdo_free(oa);
265 } /* ll_write_inode */
266
267 void ll_change_inode(struct inode *inode, int mask)
268 {
269         return ll_do_change_inode(inode, OBD_MD_FLNLINK); 
270 }
271
272
273 extern void write_inode_pages(struct inode *);
274 /* This routine is called from iput() (for each unlink on the inode).
275  * We can't put this call into delete_inode() since that is called only
276  * when i_count == 0, and we need to keep a reference on the inode while
277  * it is in the page cache, which means i_count > 0.  Catch 22.
278  */
279 static void ll_put_inode(struct inode *inode)
280 {
281         ENTRY;
282         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
283                 write_inode_pages(inode);
284                 EXIT;
285                 return;
286         }
287
288         //ll_dequeue_pages(inode);
289         EXIT;
290 } /* ll_put_inode */
291
292
293 static void ll_delete_inode(struct inode *inode)
294 {
295         ll_do_change_inode(inode, ~0);
296         clear_inode(inode); 
297 }
298 #if 0
299 {
300         struct obdo *oa;
301         int err;
302
303         ENTRY;
304         if (IOPS(inode, destroy) == NULL) {
305                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
306                 EXIT;
307                 return;
308         }
309
310         oa = obdo_alloc();
311         if ( !oa ) {
312                 printk(__FUNCTION__ ": obdo_alloc failed\n");
313                 EXIT;
314                 return;
315         }
316         oa->o_valid = OBD_MD_FLNOTOBD;
317         ll_from_inode(oa, inode);
318
319         /* XXX how do we know that this inode is now clean? */
320         printk("delete_inode ------> link %d\n", inode->i_nlink);
321         ODEBUG(oa);
322         err = IOPS(inode, destroy)(IID(inode), oa);
323         obdo_free(oa);
324         clear_inode(inode);
325         if (err) {
326                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
327                 EXIT;
328                 return;
329         }
330
331         EXIT;
332 } /* ll_delete_inode */
333 #endif
334
335
336 static int ll_attr2inode(struct inode * inode, struct iattr * attr)
337 {
338         unsigned int ia_valid = attr->ia_valid;
339         int error = 0;
340
341         if (ia_valid & ATTR_SIZE) {
342                 error = vmtruncate(inode, attr->ia_size);
343                 if (error)
344                         goto out;
345         }
346
347         if (ia_valid & ATTR_UID)
348                 inode->i_uid = attr->ia_uid;
349         if (ia_valid & ATTR_GID)
350                 inode->i_gid = attr->ia_gid;
351         if (ia_valid & ATTR_ATIME)
352                 inode->i_atime = attr->ia_atime;
353         if (ia_valid & ATTR_MTIME)
354                 inode->i_mtime = attr->ia_mtime;
355         if (ia_valid & ATTR_CTIME)
356                 inode->i_ctime = attr->ia_ctime;
357         if (ia_valid & ATTR_MODE) {
358                 inode->i_mode = attr->ia_mode;
359                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
360                         inode->i_mode &= ~S_ISGID;
361         }
362 out:
363         return error;
364 }
365
366 int ll_setattr(struct dentry *de, struct iattr *attr)
367 {
368         struct inode *inode = de->d_inode;
369         struct obdo *oa;
370         int err;
371
372         ENTRY;
373         if (IOPS(inode, setattr) == NULL) {
374                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
375                 EXIT;
376                 return -EIO;
377         }
378         oa = obdo_alloc();
379         if ( !oa ) {
380                 printk(__FUNCTION__ ": obdo_alloc failed\n");
381                 return -ENOMEM;
382         }
383
384         ll_attr2inode(inode, attr);
385         oa->o_id = inode->i_ino;
386         oa->o_mode = inode->i_mode;
387         obdo_from_iattr(oa, attr);
388         err = IOPS(inode, setattr)(IID(inode), oa);
389
390         if ( err )
391                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
392
393         EXIT;
394         obdo_free(oa);
395         return err;
396 } /* ll_setattr */
397
398
399
400 static int ll_statfs(struct super_block *sb, struct statfs *buf)
401 {
402         struct statfs tmp;
403         int err;
404
405         ENTRY;
406
407         err = OPS(sb,statfs)(ID(sb), &tmp);
408         if ( err ) { 
409                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
410                 return err;
411         }
412         memcpy(buf, &tmp, sizeof(*buf));
413         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
414         EXIT;
415
416         return err; 
417 }
418
419 static inline void ll_read_inode2(struct inode *inode, void *opaque)
420 {
421         struct obdo *oa = opaque; 
422         
423         ENTRY;
424         ll_to_inode(inode, oa); 
425
426         INIT_LIST_HEAD(ll_iplist(inode)); /* list of dirty pages on inode */
427         INIT_LIST_HEAD(ll_islist(inode)); /* list of inodes in superblock */
428
429         /* OIDEBUG(inode); */
430
431         if (S_ISREG(inode->i_mode)) {
432                 inode->i_op = &ll_file_inode_operations;
433                 inode->i_fop = &ll_file_operations;
434                 inode->i_mapping->a_ops = &ll_aops;
435                 EXIT;
436         } else if (S_ISDIR(inode->i_mode)) {
437                 inode->i_op = &ll_dir_inode_operations;
438                 inode->i_fop = &ll_dir_operations; 
439                 inode->i_mapping->a_ops = &ll_aops;
440                 EXIT;
441         } else if (S_ISLNK(inode->i_mode)) {
442                 if (inode->i_blocks) { 
443                         inode->i_op = &ll_symlink_inode_operations;
444                         inode->i_mapping->a_ops = &ll_aops;
445                 }else {
446                         inode->i_op = &ll_fast_symlink_inode_operations;
447                 }
448                 EXIT;
449         } else {
450                 init_special_inode(inode, inode->i_mode,
451                                    ((int *)ll_i2info(inode)->oi_inline)[0]);
452         }
453
454         EXIT;
455         return;
456 }
457
458 /* exported operations */
459 struct super_operations ll_super_operations =
460 {
461         read_inode2: ll_read_inode2,
462         // put_inode: ll_put_inode,
463         // delete_inode: ll_delete_inode,
464         // put_super: ll_put_super,
465         // statfs: ll_statfs
466 };
467
468
469
470 struct file_system_type lustre_light_fs_type = {
471    "lustre_light", 0, ll_read_super, NULL
472 };
473
474 static int __init init_lustre_light(void)
475 {
476         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
477
478         return register_filesystem(&lustre_light_fs_type);
479 }
480
481 static void __exit exit_lustre_light(void)
482 {
483         unregister_filesystem(&lustre_light_fs_type);
484 }
485
486 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
487 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
488 MODULE_LICENSE("GPL");
489
490 module_init(init_lustre_light);
491 module_exit(exit_lustre_light);