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