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