Whamcloud - gitweb
64-bit warning fixes. Someone should take a closer look at ext2_obd.c
[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
20 #define DEBUG_SUBSYSTEM S_LLITE
21
22 #include <linux/lustre_lite.h>
23 #include <linux/lustre_ha.h>
24
25 kmem_cache_t *ll_file_data_slab;
26 extern struct address_space_operations ll_aops;
27 extern struct address_space_operations ll_dir_aops;
28 struct super_operations ll_super_operations;
29 extern int ll_commitcbd_setup(struct ll_sb_info *);
30 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
31
32 static char *ll_read_opt(const char *opt, char *data)
33 {
34         char *value;
35         char *retval;
36         ENTRY;
37
38         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
39         if ( strncmp(opt, data, strlen(opt)) )
40                 RETURN(NULL);
41         if ( (value = strchr(data, '=')) == NULL )
42                 RETURN(NULL);
43
44         value++;
45         OBD_ALLOC(retval, strlen(value) + 1);
46         if ( !retval ) {
47                 CERROR("out of memory!\n");
48                 RETURN(NULL);
49         }
50         
51         memcpy(retval, value, strlen(value)+1);
52         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
53         RETURN(retval);
54 }
55
56 static void ll_options(char *options, char **dev, char **vers)
57 {
58         char *this_char;
59         ENTRY;
60
61         if (!options) {
62                 EXIT;
63                 return;
64         }
65
66         for (this_char = strtok (options, ",");
67              this_char != NULL;
68              this_char = strtok (NULL, ",")) {
69                 CDEBUG(D_INFO, "this_char %s\n", this_char);
70                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
71                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
72                         continue;
73         }
74         EXIT;
75 }
76
77 static struct super_block * ll_read_super(struct super_block *sb,
78                                           void *data, int silent)
79 {
80         struct inode *root = 0;
81         struct ll_sb_info *sbi;
82         char *device = NULL;
83         char *version = NULL;
84         int devno;
85         int err;
86         struct ll_fid rootfid;
87         struct ptlrpc_request *request = NULL;
88
89         ENTRY;
90         MOD_INC_USE_COUNT;
91
92         OBD_ALLOC(sbi, sizeof(*sbi));
93         if (!sbi) {
94                 MOD_DEC_USE_COUNT;
95                 RETURN(NULL);
96         }
97
98         sb->u.generic_sbp = sbi;
99
100         ll_options(data, &device, &version);
101
102         if ( !device ) {
103                 CERROR("no device\n");
104                 GOTO(out_free, sb = NULL);
105         }
106
107         devno = simple_strtoul(device, NULL, 0);
108         if ( devno >= MAX_OBD_DEVICES ) {
109                 CERROR("device of %s too high\n", device);
110                 GOTO(out_free, sb = NULL);
111         }
112
113         sbi->ll_conn.oc_dev = &obd_dev[devno];
114         err = obd_connect(&sbi->ll_conn);
115         if ( err ) {
116                 CERROR("cannot connect to %s\n", device);
117                 GOTO(out_free, sb = NULL);
118         }
119
120         ptlrpc_init_client(ptlrpc_connmgr, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
121                            &sbi->ll_mds_client);
122
123         sbi->ll_mds_conn = ptlrpc_uuid_to_connection("mds");
124         if (!sbi->ll_mds_conn) {
125                 CERROR("cannot find MDS\n");
126                 GOTO(out_disc, sb = NULL);
127         }
128
129         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
130         if (err) {
131                 CERROR("cannot connect to MDS\n");
132                 GOTO(out_disc, sb = NULL);
133         }
134
135         sbi->ll_mds_conn->c_level = LUSTRE_CONN_FULL;
136
137         err= mdc_connect(&sbi->ll_mds_client, sbi->ll_mds_conn, 
138                     &rootfid, &request); 
139         CERROR("rootfid %Ld\n", rootfid.id);
140         if (err) { 
141                 CERROR("cannot mds_connect %d\n", err);
142                 GOTO(out_disc, sb = NULL);
143         }
144         sbi->ll_rootino = rootfid.id;
145
146         sb->s_maxbytes = 1ULL << 36;
147         sb->s_blocksize = PAGE_SIZE;
148         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
149         sb->s_magic = LL_SUPER_MAGIC;
150         sb->s_op = &ll_super_operations;
151
152         /* make root inode */
153         err = mdc_getattr(&sbi->ll_mds_client, sbi->ll_mds_conn,
154                           sbi->ll_rootino, S_IFDIR,
155                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, &request);
156         if (err) {
157                 CERROR("mdc_getattr failed for root %d\n", err);
158                 GOTO(out_req, sb = NULL);
159         }
160
161         /* initialize committed transaction callback daemon */
162         INIT_LIST_HEAD(&sbi->ll_commitcbd_not_committed);
163         spin_lock_init(&sbi->ll_commitcbd_lock); 
164         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
165         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
166         sbi->ll_commitcbd_flags = 0;
167         err = ll_commitcbd_setup(sbi);
168         if (err) { 
169                 CERROR("failed to start commit callback daemon\n");
170                 GOTO(out_req, sb = NULL); 
171         }
172
173         root = iget4(sb, sbi->ll_rootino, NULL,
174                      lustre_msg_buf(request->rq_repmsg, 0));
175         if (root) {
176                 sb->s_root = d_alloc_root(root);
177         } else {
178                 CERROR("lustre_lite: bad iget4 for root\n");
179                 GOTO(out_req, sb = NULL);
180         }
181
182 out_req:
183         ptlrpc_free_req(request);
184         if (!sb) {
185 out_disc:
186                 obd_disconnect(&sbi->ll_conn);
187 out_free:
188                 MOD_DEC_USE_COUNT;
189                 OBD_FREE(sbi, sizeof(*sbi));
190         }
191         if (device) 
192                 OBD_FREE(device, strlen(device) + 1);
193         if (version)
194                 OBD_FREE(version, strlen(version) + 1);
195
196         RETURN(sb);
197 } /* ll_read_super */
198
199 static void ll_put_super(struct super_block *sb)
200 {
201         struct ll_sb_info *sbi = sb->u.generic_sbp;
202         ENTRY;
203         ll_commitcbd_cleanup(sbi);
204         obd_disconnect(&sbi->ll_conn);
205         ptlrpc_put_connection(sbi->ll_mds_conn);
206         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
207         MOD_DEC_USE_COUNT;
208         EXIT;
209 } /* ll_put_super */
210
211
212 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
213 static void ll_delete_inode(struct inode *inode)
214 {
215         if (S_ISREG(inode->i_mode)) { 
216                 int err; 
217                 struct obdo *oa; 
218                 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
219                 if (!oa) { 
220                         CERROR("no memory\n"); 
221                 }
222
223                 err = obd_destroy(ll_i2obdconn(inode), oa); 
224                 CDEBUG(D_INODE, "obd destroy of %Ld error %d\n",
225                        (unsigned long long)oa->o_id, err);
226                 obdo_free(oa);
227         }
228
229         clear_inode(inode); 
230 }
231
232 /* like inode_setattr, but doesn't mark the inode dirty */ 
233 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
234 {
235         unsigned int ia_valid = attr->ia_valid;
236         int error = 0;
237
238         if ((ia_valid & ATTR_SIZE) && trunc ) {
239                 error = vmtruncate(inode, attr->ia_size);
240                 if (error)
241                         goto out;
242         } else if (ia_valid & ATTR_SIZE) { 
243                 inode->i_size = attr->ia_size;
244         }               
245
246         if (ia_valid & ATTR_UID)
247                 inode->i_uid = attr->ia_uid;
248         if (ia_valid & ATTR_GID)
249                 inode->i_gid = attr->ia_gid;
250         if (ia_valid & ATTR_ATIME)
251                 inode->i_atime = attr->ia_atime;
252         if (ia_valid & ATTR_MTIME)
253                 inode->i_mtime = attr->ia_mtime;
254         if (ia_valid & ATTR_CTIME)
255                 inode->i_ctime = attr->ia_ctime;
256         if (ia_valid & ATTR_MODE) {
257                 inode->i_mode = attr->ia_mode;
258                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
259                         inode->i_mode &= ~S_ISGID;
260         }
261 out:
262         return error;
263 }
264
265 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
266 {
267         struct ptlrpc_request *request = NULL;
268         struct ll_sb_info *sbi = ll_i2sbi(inode);
269         int err;
270
271         ENTRY;
272
273         /* change incore inode */
274         ll_attr2inode(inode, attr, do_trunc);
275
276         err = mdc_setattr(&sbi->ll_mds_client, sbi->ll_mds_conn, inode, attr,
277                           &request);
278         if (err)
279                 CERROR("mdc_setattr fails (%d)\n", err);
280
281         ptlrpc_free_req(request);
282
283         RETURN(err);
284 }
285
286 int ll_setattr(struct dentry *de, struct iattr *attr)
287 {
288         return ll_inode_setattr(de->d_inode, attr, 1);
289 }
290
291 static int ll_statfs(struct super_block *sb, struct statfs *buf)
292 {
293         struct statfs tmp;
294         int err;
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
305         RETURN(err);
306 }
307
308 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
309 {
310         struct ll_inode_info *ii = 
311                 (struct ll_inode_info *) &dst->u.generic_ip;
312
313         /* core attributes first */
314         if ( body->valid & OBD_MD_FLID )
315                 dst->i_ino = body->ino;
316         if ( body->valid & OBD_MD_FLATIME ) 
317                 dst->i_atime = body->atime;
318         if ( body->valid & OBD_MD_FLMTIME ) 
319                 dst->i_mtime = body->mtime;
320         if ( body->valid & OBD_MD_FLCTIME ) 
321                 dst->i_ctime = body->ctime;
322         if ( body->valid & OBD_MD_FLSIZE ) 
323                 dst->i_size = body->size;
324         if ( body->valid & OBD_MD_FLMODE ) 
325                 dst->i_mode = body->mode;
326         if ( body->valid & OBD_MD_FLUID ) 
327                 dst->i_uid = body->uid;
328         if ( body->valid & OBD_MD_FLGID ) 
329                 dst->i_gid = body->gid;
330         if ( body->valid & OBD_MD_FLFLAGS ) 
331                 dst->i_flags = body->flags;
332         if ( body->valid & OBD_MD_FLNLINK )
333                 dst->i_nlink = body->nlink;
334         if ( body->valid & OBD_MD_FLGENER )
335                 dst->i_generation = body->generation;
336
337         /* this will become more elaborate for striping etc */ 
338         if (body->valid & OBD_MD_FLOBJID) 
339                 ii->lli_objid = body->objid;
340 #if 0
341
342         if (obdo_has_inline(oa)) {
343                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
344                     S_ISFIFO(inode->i_mode)) {
345                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
346                         CDEBUG(D_INODE,
347                                "copying device %x from obdo to inode\n", rdev);
348                         init_special_inode(inode, inode->i_mode, rdev);
349                 } else {
350                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
351                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
352                 }
353                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
354         }
355 #endif 
356 } /* ll_to_inode */
357
358 static inline void ll_read_inode2(struct inode *inode, void *opaque)
359 {
360         struct mds_body *body = opaque; 
361         
362         ENTRY;
363         ll_to_inode(inode, body); 
364
365         /* OIDEBUG(inode); */
366
367         if (S_ISREG(inode->i_mode)) {
368                 inode->i_op = &ll_file_inode_operations;
369                 inode->i_fop = &ll_file_operations;
370                 inode->i_mapping->a_ops = &ll_aops;
371                 EXIT;
372         } else if (S_ISDIR(inode->i_mode)) {
373                 inode->i_op = &ll_dir_inode_operations;
374                 inode->i_fop = &ll_dir_operations; 
375                 inode->i_mapping->a_ops = &ll_dir_aops;
376                 EXIT;
377         } else if (S_ISLNK(inode->i_mode)) {
378                 inode->i_op = &ll_fast_symlink_inode_operations;
379                 EXIT;
380         } else {
381                 init_special_inode(inode, inode->i_mode,
382                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
383                 EXIT;
384         }
385
386         return;
387 }
388
389 /* exported operations */
390 struct super_operations ll_super_operations =
391 {
392         read_inode2: ll_read_inode2,
393         delete_inode: ll_delete_inode,
394         put_super: ll_put_super,
395         // statfs: ll_statfs
396 };
397
398 struct file_system_type lustre_lite_fs_type = {
399         "lustre_lite", 0, ll_read_super, NULL
400 };
401
402 static int __init init_lustre_lite(void)
403 {
404         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
405         ll_file_data_slab = kmem_cache_create("ll_file_data",
406                                               sizeof(struct ll_file_data), 0,
407                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
408         if (ll_file_data_slab == NULL)
409                 return -ENOMEM;
410         return register_filesystem(&lustre_lite_fs_type);
411 }
412
413 static void __exit exit_lustre_lite(void)
414 {
415         unregister_filesystem(&lustre_lite_fs_type);
416         kmem_cache_destroy(ll_file_data_slab);
417 }
418
419 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
420 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
421 MODULE_LICENSE("GPL");
422
423 module_init(init_lustre_lite);
424 module_exit(exit_lustre_lite);