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