Whamcloud - gitweb
- changed back to using no optimization for UML modules, and no -g2
[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         ENTRY;
51
52         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
53         if ( strncmp(opt, data, strlen(opt)) ) {
54                 EXIT;
55                 return NULL;
56         }
57         if ( (value = strchr(data, '=')) == NULL ) {
58                 EXIT;
59                 return NULL;
60         }
61
62         value++;
63         OBD_ALLOC(retval, char *, strlen(value) + 1);
64         if ( !retval ) {
65                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
66                 return NULL;
67         }
68         
69         memcpy(retval, value, strlen(value)+1);
70         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
71         EXIT;
72         return retval;
73 }
74
75 static void ll_options(char *options, char **dev, char **vers)
76 {
77         char *this_char;
78         ENTRY; 
79
80         if (!options) { 
81                 EXIT;
82                 return;
83         }
84
85         for (this_char = strtok (options, ",");
86              this_char != NULL;
87              this_char = strtok (NULL, ",")) {
88                 CDEBUG(D_INFO, "this_char %s\n", this_char);
89                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
90                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
91                         continue;
92                 
93         }
94         EXIT;
95 }
96
97 static struct super_block * ll_read_super(struct super_block *sb, 
98                                             void *data, int silent)
99 {
100         struct inode *root = 0; 
101         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
102         char *device = NULL;
103         char *version = NULL;
104         int connected = 0;
105         int devno;
106         int err;
107         struct mds_rep *rep; 
108         struct ptlrep_hdr *hdr = NULL; 
109
110         ENTRY;
111         MOD_INC_USE_COUNT; 
112
113         memset(sbi, 0, sizeof(*sbi));
114
115         printk(__FUNCTION__ "line %d\n", __LINE__); 
116
117         ll_options(data, &device, &version);
118         printk(__FUNCTION__ "line %d\n", __LINE__); 
119         if ( !device ) {
120                 printk(__FUNCTION__ ": no device\n");
121                 sb = NULL; 
122                 goto ERR;
123         }
124         printk(__FUNCTION__ "line %d\n", __LINE__); 
125
126         devno = simple_strtoul(device, NULL, 0);
127         if ( devno >= MAX_OBD_DEVICES ) {
128                 printk(__FUNCTION__ ": device of %s too high\n", device);
129                 sb = NULL; 
130                 goto ERR;
131         } 
132         printk(__FUNCTION__ "line %d\n", __LINE__); 
133
134         sbi->ll_conn.oc_dev = &obd_dev[devno];
135         err = obd_connect(&sbi->ll_conn);
136         if ( err ) {
137                 printk(__FUNCTION__ "cannot connect to %s\n", device);
138                 sb = NULL; 
139                 goto ERR;
140         }
141         printk(__FUNCTION__ "line %d\n", __LINE__); 
142         connected = 1;
143
144         printk(__FUNCTION__ "line %d\n", __LINE__); 
145         err = kportal_uuid_to_peer("mds", &sbi->ll_peer);
146         if (err == 0)
147                 sbi->ll_peer_ptr = &sbi->ll_peer;
148         printk(__FUNCTION__ "line %d\n", __LINE__); 
149
150         sbi->ll_super = sb;
151         sbi->ll_rootino = 2;
152
153         sb->s_maxbytes = 1LL << 36;
154         sb->s_blocksize = PAGE_SIZE;
155         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
156         sb->s_magic = LL_SUPER_MAGIC;
157         sb->s_op = &ll_super_operations;
158         printk(__FUNCTION__ "line %d\n", __LINE__); 
159
160         /* make root inode */
161         err = mdc_getattr(sbi->ll_peer_ptr, sbi->ll_rootino, S_IFDIR, 
162                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 
163                           &rep, &hdr);
164         printk(__FUNCTION__ "line %d\n", __LINE__); 
165         if (err) {
166                 printk(__FUNCTION__ ": mds_getattr failed for root %d\n", err);
167                 sb = NULL; 
168                 goto ERR;
169         }
170         printk(__FUNCTION__ "line %d\n", __LINE__); 
171                          
172         root = iget4(sb, sbi->ll_rootino, NULL, rep);
173         if (root) {
174                 sb->s_root = d_alloc_root(root);
175         } else {
176             printk("lustre_light: bad iget4 for root\n");
177             sb = NULL; 
178             goto ERR;
179         } 
180         printk(__FUNCTION__ "line %d\n", __LINE__); 
181         
182 ERR:
183         if (hdr)
184                 kfree(hdr);
185         if (device)
186                 OBD_FREE(device, strlen(device) + 1);
187         if (version)
188                 OBD_FREE(version, strlen(version) + 1);
189         if (!sb && connected) 
190                 obd_disconnect(&sbi->ll_conn);
191
192         if (!sb && root) {
193                 iput(root);
194         }
195         if (!sb) 
196                 MOD_DEC_USE_COUNT;
197
198         EXIT;
199         return sb;
200 } /* ll_read_super */
201
202 static void ll_put_super(struct super_block *sb)
203 {
204         ENTRY;
205
206         obd_disconnect(ID(sb));
207
208         MOD_DEC_USE_COUNT;
209         EXIT;
210 } /* ll_put_super */
211
212
213 extern void write_inode_pages(struct inode *);
214 /* This routine is called from iput() (for each unlink on the inode).
215  * We can't put this call into delete_inode() since that is called only
216  * when i_count == 0, and we need to keep a reference on the inode while
217  * it is in the page cache, which means i_count > 0.  Catch 22.
218  */
219 static void ll_put_inode(struct inode *inode)
220 {
221         ENTRY;
222         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
223                 write_inode_pages(inode);
224                 EXIT;
225                 return;
226         }
227
228         //ll_dequeue_pages(inode);
229         EXIT;
230 } /* ll_put_inode */
231
232 /* like inode_setattr, but doesn't mark the inode dirty */ 
233 static int ll_attr2inode(struct inode * inode, struct iattr * attr)
234 {
235         unsigned int ia_valid = attr->ia_valid;
236         int error = 0;
237
238         if (ia_valid & ATTR_SIZE) {
239                 error = vmtruncate(inode, attr->ia_size);
240                 if (error)
241                         goto out;
242         }
243         if (ia_valid & ATTR_UID)
244                 inode->i_uid = attr->ia_uid;
245         if (ia_valid & ATTR_GID)
246                 inode->i_gid = attr->ia_gid;
247         if (ia_valid & ATTR_ATIME)
248                 inode->i_atime = attr->ia_atime;
249         if (ia_valid & ATTR_MTIME)
250                 inode->i_mtime = attr->ia_mtime;
251         if (ia_valid & ATTR_CTIME)
252                 inode->i_ctime = attr->ia_ctime;
253         if (ia_valid & ATTR_MODE) {
254                 inode->i_mode = attr->ia_mode;
255                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
256                         inode->i_mode &= ~S_ISGID;
257         }
258 out:
259         return error;
260 }
261
262 int ll_inode_setattr(struct inode *inode, struct iattr *attr)
263 {
264         struct ptlrep_hdr *hdr = NULL;
265         struct ll_sb_info *sbi =
266                 (struct ll_sb_info *)(&inode->i_sb->u.generic_sbp);
267         int err;
268
269         ENTRY;
270
271         /* change incore inode */
272         ll_attr2inode(inode, attr);
273
274         err = mdc_setattr(sbi->ll_peer_ptr, inode, attr, NULL, &hdr); 
275         if ( err )
276                 printk(__FUNCTION__ ": ll_setattr fails (%d)\n", err);
277
278         EXIT;
279         return err;
280 }
281
282 int ll_setattr(struct dentry *de, struct iattr *attr)
283 {
284         return ll_inode_setattr(de->d_inode, attr);
285 }
286
287 static int ll_statfs(struct super_block *sb, struct statfs *buf)
288 {
289         struct statfs tmp;
290         int err;
291
292         ENTRY;
293
294         err = obd_statfs(ID(sb), &tmp);
295         if ( err ) { 
296                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
297                 return err;
298         }
299         memcpy(buf, &tmp, sizeof(*buf));
300         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
301         EXIT;
302
303         return err; 
304 }
305
306 static void inline ll_to_inode(struct inode *dst, struct mds_rep *rep)
307 {
308         struct ll_inode_info *ii = 
309                 (struct ll_inode_info *) &dst->u.generic_ip;
310
311         /* core attributes first */
312         if ( rep->valid & OBD_MD_FLID )
313                 dst->i_ino = rep->ino;
314         if ( rep->valid & OBD_MD_FLATIME ) 
315                 dst->i_atime = rep->atime;
316         if ( rep->valid & OBD_MD_FLMTIME ) 
317                 dst->i_mtime = rep->mtime;
318         if ( rep->valid & OBD_MD_FLCTIME ) 
319                 dst->i_ctime = rep->ctime;
320         if ( rep->valid & OBD_MD_FLSIZE ) 
321                 dst->i_size = rep->size;
322         if ( rep->valid & OBD_MD_FLMODE ) 
323                 dst->i_mode = rep->mode;
324         if ( rep->valid & OBD_MD_FLUID ) 
325                 dst->i_uid = rep->uid;
326         if ( rep->valid & OBD_MD_FLGID ) 
327                 dst->i_gid = rep->gid;
328         if ( rep->valid & OBD_MD_FLFLAGS ) 
329                 dst->i_flags = rep->flags;
330         if ( rep->valid & OBD_MD_FLNLINK )
331                 dst->i_nlink = rep->nlink;
332         if ( rep->valid & OBD_MD_FLGENER )
333                 dst->i_generation = rep->generation;
334
335         /* this will become more elaborate for striping etc */ 
336         if (rep->valid & OBD_MD_FLOBJID) 
337                 ii->lli_objid = rep->objid;
338 #if 0
339
340         if (obdo_has_inline(oa)) {
341                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
342                     S_ISFIFO(inode->i_mode)) {
343                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
344                         CDEBUG(D_INODE,
345                                "copying device %x from obdo to inode\n", rdev);
346                         init_special_inode(inode, inode->i_mode, rdev);
347                 } else {
348                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
349                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
350                 }
351                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
352         }
353 #endif 
354 } /* ll_to_inode */
355
356 static inline void ll_read_inode2(struct inode *inode, void *opaque)
357 {
358         struct mds_rep *rep = opaque; 
359         
360         ENTRY;
361         ll_to_inode(inode, rep); 
362
363         /* OIDEBUG(inode); */
364
365         if (S_ISREG(inode->i_mode)) {
366                 inode->i_op = &ll_file_inode_operations;
367                 inode->i_fop = &ll_file_operations;
368                 inode->i_mapping->a_ops = &ll_aops;
369                 EXIT;
370         } else if (S_ISDIR(inode->i_mode)) {
371                 inode->i_op = &ll_dir_inode_operations;
372                 inode->i_fop = &ll_dir_operations; 
373                 inode->i_mapping->a_ops = &ll_dir_aops;
374                 EXIT;
375         } else if (S_ISLNK(inode->i_mode)) {
376                 if (inode->i_blocks) { 
377                         inode->i_op = &ll_symlink_inode_operations;
378                         inode->i_mapping->a_ops = &ll_aops;
379                 }else {
380                         inode->i_op = &ll_fast_symlink_inode_operations;
381                 }
382                 EXIT;
383         } else {
384                 init_special_inode(inode, inode->i_mode,
385                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
386         }
387
388         EXIT;
389         return;
390 }
391
392 /* exported operations */
393 struct super_operations ll_super_operations =
394 {
395         read_inode2: ll_read_inode2,
396         // put_inode: ll_put_inode,
397         // delete_inode: ll_delete_inode,
398         put_super: ll_put_super,
399         // statfs: ll_statfs
400 };
401
402 struct file_system_type lustre_light_fs_type = {
403    "lustre_light", 0, ll_read_super, NULL
404 };
405
406 static int __init init_lustre_light(void)
407 {
408         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
409
410         return register_filesystem(&lustre_light_fs_type);
411 }
412
413 static void __exit exit_lustre_light(void)
414 {
415         unregister_filesystem(&lustre_light_fs_type);
416 }
417
418 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
419 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
420 MODULE_LICENSE("GPL");
421
422 module_init(init_lustre_light);
423 module_exit(exit_lustre_light);