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