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