Whamcloud - gitweb
680c47f96fe8206f732883b177a526cc91b0596e
[fs/lustre-release.git] / lustre / llite / super25.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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #include <linux/version.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_ha.h>
31 #include <linux/lustre_dlm.h>
32 #include <linux/init.h>
33 #include <linux/fs.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
36
37 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
38 #include <asm/statfs.h>
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 /* /proc/lustre/llite root that tracks llite mount points */
45 struct proc_dir_entry *proc_lustre_fs_root = NULL;
46 /* lproc_llite.c */
47 extern int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
48                                        struct super_block *sb,
49                                        char *osc, char *mdc);
50
51 extern int ll_init_inodecache(void);
52 extern void ll_destroy_inodecache(void);
53 extern int ll_recover(struct recovd_data *, int);
54 extern int ll_commitcbd_setup(struct ll_sb_info *);
55 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
56 int ll_read_inode2(struct inode *inode, void *opaque);
57
58 extern int ll_proc_namespace(struct super_block* sb, char* osc, char* mdc);
59
60 static char *ll_read_opt(const char *opt, char *data)
61 {
62         char *value;
63         char *retval;
64         ENTRY;
65
66         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
67         if (strncmp(opt, data, strlen(opt)))
68                 RETURN(NULL);
69         if ((value = strchr(data, '=')) == NULL)
70                 RETURN(NULL);
71
72         value++;
73         OBD_ALLOC(retval, strlen(value) + 1);
74         if (!retval) {
75                 CERROR("out of memory!\n");
76                 RETURN(NULL);
77         }
78
79         memcpy(retval, value, strlen(value)+1);
80         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
81         RETURN(retval);
82 }
83
84 static int ll_set_opt(const char *opt, char *data, int fl)
85 {
86         ENTRY;
87
88         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
89         if (strncmp(opt, data, strlen(opt)))
90                 RETURN(0);
91         else
92                 RETURN(fl);
93 }
94
95 static void ll_options(char *options, char **ost, char **mds, int *flags)
96 {
97         char *opt_ptr = options;
98         char *this_char;
99         ENTRY;
100
101         if (!options) {
102                 EXIT;
103                 return;
104         }
105
106         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
107                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
108                 if ((!*ost && (*ost = ll_read_opt("osc", this_char)))||
109                     (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
110                     (!(*flags & LL_SBI_NOLCK) &&
111                      ((*flags) = (*flags) |
112                       ll_set_opt("nolock", this_char, LL_SBI_NOLCK))))
113                         continue;
114         }
115         EXIT;
116 }
117
118 #ifndef log2
119 #define log2(n) ffz(~(n))
120 #endif
121
122
123 static int ll_fill_super(struct super_block *sb, void *data, int silent)
124 {
125         struct inode *root = 0;
126         struct obd_device *obd;
127         struct ll_sb_info *sbi;
128         char *osc = NULL;
129         char *mdc = NULL;
130         int err;
131         struct ll_fid rootfid;
132         struct obd_statfs osfs;
133         struct ptlrpc_request *request = NULL;
134         struct ptlrpc_connection *mdc_conn;
135         struct ll_read_inode2_cookie lic;
136         class_uuid_t uuid;
137         struct obd_uuid param_uuid;
138
139         ENTRY;
140         CDEBUG(D_VFSTRACE, "VFS Op:\n");
141
142         OBD_ALLOC(sbi, sizeof(*sbi));
143         if (!sbi)
144                 RETURN(-ENOMEM);
145
146         INIT_LIST_HEAD(&sbi->ll_conn_chain);
147         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
148         generate_random_uuid(uuid);
149         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
150
151         sb->s_fs_info = sbi;
152
153         ll_options(data, &osc, &mdc, &sbi->ll_flags);
154
155         if (!osc) {
156                 CERROR("no osc\n");
157                 GOTO(out_free, sb = NULL);
158         }
159
160         if (!mdc) {
161                 CERROR("no mdc\n");
162                 GOTO(out_free, sb = NULL);
163         }
164
165         strncpy(param_uuid.uuid, mdc, sizeof(param_uuid.uuid));
166         obd = class_uuid2obd(&param_uuid);
167         if (!obd) {
168                 CERROR("MDC %s: not setup or attached\n", mdc);
169                 GOTO(out_free, sb = NULL);
170         }
171
172         err = obd_connect(&sbi->ll_mdc_conn, obd, &sbi->ll_sb_uuid);
173         if (err) {
174                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
175                 GOTO(out_free, sb = NULL);
176         }
177
178         mdc_conn = sbi2mdc(sbi)->cl_import->imp_connection;
179         strncpy(param_uuid.uuid, osc, sizeof(param_uuid.uuid));
180
181         obd = class_uuid2obd(&param_uuid);
182         if (!obd) {
183                 CERROR("OSC %s: not setup or attached\n", osc);
184                 GOTO(out_mdc, sb = NULL);
185         }
186
187         err = obd_connect(&sbi->ll_osc_conn, obd, &sbi->ll_sb_uuid);
188         if (err) {
189                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
190                 GOTO(out_mdc, sb = NULL);
191         }
192
193         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
194         if (err) {
195                 CERROR("cannot mds_connect: rc = %d\n", err);
196                 GOTO(out_osc, sb = NULL);
197         }
198         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
199         sbi->ll_rootino = rootfid.id;
200
201         memset(&osfs, 0, sizeof(osfs));
202         err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
203         sb->s_blocksize = osfs.os_bsize;
204         sb->s_blocksize_bits = log2(osfs.os_bsize);
205         sb->s_magic = LL_SUPER_MAGIC;
206         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
207
208         sb->s_op = &ll_super_operations;
209
210         /* make root inode 
211          * XXX: move this to after cbd setup? */
212         err = mdc_getattr(&sbi->ll_mdc_conn, &rootfid,
213                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
214         if (err) {
215                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
216                 GOTO(out_osc, sb = NULL);
217         }
218
219         /* initialize committed transaction callback daemon */
220         spin_lock_init(&sbi->ll_commitcbd_lock);
221         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
222         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
223         sbi->ll_commitcbd_flags = 0;
224         err = ll_commitcbd_setup(sbi);
225         if (err) {
226                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
227                 ptlrpc_req_finished (request);
228                 GOTO(out_osc, sb = NULL);
229         }
230
231         lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*lic.lic_body));
232         LASSERT (lic.lic_body != NULL);         /* checked by mdc_getattr() */
233         LASSERT_REPSWABBED (request, 0);        /* swabbed by mdc_getattr() */
234
235         lic.lic_lsm = NULL;
236
237         root = iget5_locked(sb, sbi->ll_rootino, NULL,
238                             ll_read_inode2, &lic);
239
240         ptlrpc_req_finished(request);
241
242         if (root == NULL || is_bad_inode(root)) {
243                 /* XXX might need iput() for bad inode */
244                 CERROR("lustre_lite: bad iget5 for root\n");
245                 GOTO(out_cbd, sb = NULL);
246         }
247
248         sb->s_root = d_alloc_root(root);
249         root->i_state &= ~(I_LOCK | I_NEW);
250         printk("AMRUT 1\n");
251         if (proc_lustre_fs_root) {
252                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
253                                                   osc, mdc);
254                 if (err < 0)
255                         CERROR("could not register mount in /proc/lustre");
256         }
257
258 out_dev:
259         if (mdc)
260                 OBD_FREE(mdc, strlen(mdc) + 1);
261         if (osc)
262                 OBD_FREE(osc, strlen(osc) + 1);
263         printk("AMRUT 2\n");
264
265         RETURN(0);
266
267 out_cbd:
268         ll_commitcbd_cleanup(sbi);
269 out_osc:
270         obd_disconnect(&sbi->ll_osc_conn, 0);
271 out_mdc:
272         obd_disconnect(&sbi->ll_mdc_conn, 0);
273 out_free:
274         OBD_FREE(sbi, sizeof(*sbi));
275
276         goto out_dev;
277 } /* ll_fill_super */
278
279
280 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
281 {
282         struct ptlrpc_request *request = NULL;
283         struct ll_sb_info *sbi = ll_i2sbi(inode);
284         struct mdc_op_data op_data;
285         int err = 0;
286         ENTRY;
287         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
288
289         if ((attr->ia_valid & ATTR_SIZE)) {
290                 /* writeback uses inode->i_size to determine how far out
291                  * its cached pages go.  ll_truncate gets a PW lock, canceling
292                  * our lock, _after_ it has updated i_size.  this can confuse
293                  * us into zero extending the file to the newly truncated
294                  * size, and this has bad implications for a racing o_append.
295                  * if we're extending our size we need to flush the pages
296                  * with the correct i_size before vmtruncate stomps on
297                  * the new i_size.  again, this can only find pages to
298                  * purge if the PW lock that generated them is still held.
299                  */
300                 if ( attr->ia_size > inode->i_size ) {
301                         filemap_fdatasync(inode->i_mapping);
302                         filemap_fdatawait(inode->i_mapping);
303                 }
304                 err = vmtruncate(inode, attr->ia_size);
305                 if (err)
306                         RETURN(err);
307         }
308
309         /* Don't send size changes to MDS to avoid "fast EA" problems, and
310          * also avoid a pointless RPC (we get file size from OST anyways).
311          */
312         attr->ia_valid &= ~ATTR_SIZE;
313         if (!attr->ia_valid)
314                 RETURN(0);
315
316         ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
317
318         err = mdc_setattr(&sbi->ll_mdc_conn, &op_data,
319                           attr, NULL, 0, &request);
320         if (err)
321                 CERROR("mdc_setattr fails: err = %d\n", err);
322
323         ptlrpc_req_finished(request);
324
325         if (S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_MTIME_SET) {
326                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
327                 struct obdo oa;
328                 int err2;
329
330                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
331                        inode->i_ino, attr->ia_mtime);
332                 oa.o_id = lsm->lsm_object_id;
333                 oa.o_mode = S_IFREG;
334                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMTIME;
335                 oa.o_mtime = LTIME_S(attr->ia_mtime);
336                 err2 = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
337                 if (err2) {
338                         CERROR("obd_setattr fails: rc=%d\n", err);
339                         if (!err)
340                                 err = err2;
341                 }
342         }
343         RETURN(err);
344 }
345 struct super_block * ll_get_sb(struct file_system_type *fs_type,
346                                int flags, char *devname, void * data)
347 {
348         return get_sb_nodev(fs_type, flags, data, ll_fill_super);
349 }
350
351 static void ll_put_super(struct super_block *sb)
352 {
353         struct ll_sb_info *sbi = ll_s2sbi(sb);
354         struct list_head *tmp, *next;
355         struct ll_fid rootfid;
356         ENTRY;
357         CDEBUG(D_VFSTRACE, "VFS Op:\n");
358
359         list_del(&sbi->ll_conn_chain);
360         ll_commitcbd_cleanup(sbi);
361         obd_disconnect(&sbi->ll_osc_conn, 0);
362
363         /* NULL request to force sync on the MDS, and get the last_committed
364          * value to flush remaining RPCs from the pending queue on client.
365          *
366          * XXX This should be an mdc_sync() call to sync the whole MDS fs,
367          *     which we can call for other reasons as well.
368          */
369         mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
370
371         if (sbi->ll_proc_root) {
372                 lprocfs_remove(sbi->ll_proc_root);
373         sbi->ll_proc_root = NULL;
374         }
375
376         obd_disconnect(&sbi->ll_mdc_conn, 0);
377
378         spin_lock(&dcache_lock);
379         list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list){
380                 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
381                 shrink_dcache_parent(dentry);
382         }
383         spin_unlock(&dcache_lock);
384
385         OBD_FREE(sbi, sizeof(*sbi));
386
387         EXIT;
388 } /* ll_put_super */
389
390 static void ll_clear_inode(struct inode *inode)
391 {
392         struct ll_sb_info *sbi = ll_i2sbi(inode);
393         struct ll_inode_info *lli = ll_i2info(inode);
394         int rc;
395         ENTRY;
396         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
397
398 #warning "Is there a reason we don't do this in 2.5, but we do in 2.4?"
399 #if 0
400         rc = ll_mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
401         if (rc < 0) {
402                 CERROR("ll_mdc_cancel_unused: %d\n", rc);
403                 /* XXX FIXME do something dramatic */
404         }
405
406         if (lli->lli_smd) {
407                 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd, 0);
408                 if (rc < 0) {
409                         CERROR("obd_cancel_unused: %d\n", rc);
410                         /* XXX FIXME do something dramatic */
411                 }
412         }
413 #endif
414
415         if (atomic_read(&inode->i_count) != 0)
416                 CERROR("clearing in-use inode %lu: count = %d\n",
417                        inode->i_ino, atomic_read(&inode->i_count));
418
419         if (lli->lli_smd) {
420                 obd_free_memmd(&sbi->ll_osc_conn, &lli->lli_smd);
421                 lli->lli_smd = NULL;
422         }
423
424         if (lli->lli_symlink_name) {
425                 OBD_FREE(lli->lli_symlink_name,strlen(lli->lli_symlink_name)+1);
426                 lli->lli_symlink_name = NULL;
427         }
428
429         EXIT;
430 }
431
432 #if 0
433 static void ll_delete_inode(struct inode *inode)
434 {
435         ENTRY;
436         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
437         if (S_ISREG(inode->i_mode)) {
438                 int err;
439                 struct obdo *oa;
440                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
441
442                 /* mcreate with no open */
443                 if (!lsm)
444                         GOTO(out, 0);
445
446                 if (lsm->lsm_object_id == 0) {
447                         CERROR("This really happens\n");
448                         /* No obdo was ever created */
449                         GOTO(out, 0);
450                 }
451
452                 oa = obdo_alloc();
453                 if (oa == NULL)
454                         GOTO(out, -ENOMEM);
455
456                 oa->o_id = lsm->lsm_object_id;
457                 oa->o_mode = inode->i_mode;
458                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
459
460                 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
461                 obdo_free(oa);
462                 if (err)
463                         CDEBUG(D_SUPER, "obd destroy objid "LPX64" error %d\n",
464                                lsm->lsm_object_id, err);
465         }
466 out:
467         clear_inode(inode);
468         EXIT;
469 }
470 #endif
471
472 /* like inode_setattr, but doesn't mark the inode dirty */
473 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
474 {
475         unsigned int ia_valid = attr->ia_valid;
476         int error = 0;
477
478         if ((ia_valid & ATTR_SIZE) && trunc) {
479                 if (attr->ia_size > ll_file_maxbytes(inode)) {
480                         error = -EFBIG;
481                         goto out;
482                 }
483                 error = vmtruncate(inode, attr->ia_size);
484                 if (error)
485                         goto out;
486         } else if (ia_valid & ATTR_SIZE)
487                 inode->i_size = attr->ia_size;
488
489         if (ia_valid & ATTR_UID)
490                 inode->i_uid = attr->ia_uid;
491         if (ia_valid & ATTR_GID)
492                 inode->i_gid = attr->ia_gid;
493         if (ia_valid & ATTR_ATIME)
494                 inode->i_atime = attr->ia_atime;
495         if (ia_valid & ATTR_MTIME)
496                 inode->i_mtime = attr->ia_mtime;
497         if (ia_valid & ATTR_CTIME)
498                 inode->i_ctime = attr->ia_ctime;
499         if (ia_valid & ATTR_MODE) {
500                 inode->i_mode = attr->ia_mode;
501                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
502                         inode->i_mode &= ~S_ISGID;
503         }
504 out:
505         return error;
506 }
507
508 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
509 {
510         struct ptlrpc_request *request = NULL;
511         struct ll_sb_info *sbi = ll_i2sbi(inode);
512         int err = 0;
513
514         ENTRY;
515
516         /* change incore inode */
517         err = ll_attr2inode(inode, attr, do_trunc);
518         if (err)
519                 RETURN(err);
520
521         /* Don't send size changes to MDS to avoid "fast EA" problems, and
522          * also avoid a pointless RPC (we get file size from OST anyways).
523          */
524         attr->ia_valid &= ~ATTR_SIZE;
525         if (attr->ia_valid) {
526                 struct mdc_op_data op_data;
527
528                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
529
530                 err = mdc_setattr(&sbi->ll_mdc_conn, &op_data,
531                                   attr, NULL, 0, &request);
532                 if (err)
533                         CERROR("mdc_setattr fails: err = %d\n", err);
534
535                 ptlrpc_req_finished(request);
536                 if (S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_MTIME_SET) {
537                         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
538                         struct obdo oa;
539                         int err2;
540
541                         CDEBUG(D_ERROR, "setting mtime on OST\n");
542                         oa.o_id = lsm->lsm_object_id;
543                         oa.o_mode = S_IFREG;
544                         oa.o_valid = OBD_MD_FLID |OBD_MD_FLTYPE |OBD_MD_FLMTIME;
545                         oa.o_mtime = LTIME_S(attr->ia_mtime);
546                         err2 = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
547                         if (err2) {
548                                 CERROR("obd_setattr fails: rc=%d\n", err);
549                                 if (!err)
550                                         err = err2;
551                         }
552                 }
553         }
554
555         RETURN(err);
556 }
557
558 int ll_setattr(struct dentry *de, struct iattr *attr)
559 {
560         int rc = inode_change_ok(de->d_inode, attr);
561         CDEBUG(D_VFSTRACE, "VFS Op:name=%s\n", de->d_name.name);
562         if (rc)
563                 return rc;
564
565         return ll_inode_setattr(de->d_inode, attr, 1);
566 }
567
568 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
569 {
570         struct ll_sb_info *sbi = ll_s2sbi(sb);
571         struct obd_statfs osfs;
572         int rc;
573         ENTRY;
574         CDEBUG(D_VFSTRACE, "VFS Op:\n");
575
576         memset(sfs, 0, sizeof(*sfs));
577         rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
578         statfs_unpack(sfs, &osfs);
579         if (rc)
580                 CERROR("mdc_statfs fails: rc = %d\n", rc);
581         else
582                 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
583                        " objects "LPU64"/"LPU64"\n",
584                        osfs.os_bavail, osfs.os_blocks,
585                        osfs.os_ffree, osfs.os_files);
586
587         /* temporary until mds_statfs returns statfs info for all OSTs */
588         if (!rc) {
589                 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
590                 if (rc) {
591                         CERROR("obd_statfs fails: rc = %d\n", rc);
592                         GOTO(out, rc);
593                 }
594                 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
595                        " objects "LPU64"/"LPU64"\n",
596                        osfs.os_bavail, osfs.os_blocks,
597                        osfs.os_ffree, osfs.os_files);
598
599                 while (osfs.os_blocks > ~0UL) {
600                         sfs->f_bsize <<= 1;
601
602                         osfs.os_blocks >>= 1;
603                         osfs.os_bfree >>= 1;
604                         osfs.os_bavail >>= 1;
605                 }
606                 sfs->f_blocks = osfs.os_blocks;
607                 sfs->f_bfree = osfs.os_bfree;
608                 sfs->f_bavail = osfs.os_bavail;
609                 if (osfs.os_ffree < (__u64)sfs->f_ffree) {
610                         sfs->f_files = (sfs->f_files - sfs->f_ffree) +
611                                        osfs.os_ffree;
612                         sfs->f_ffree = osfs.os_ffree;
613                 }
614         }
615
616 out:
617         RETURN(rc);
618 }
619
620 void ll_update_inode(struct inode *inode, struct mds_body *body,
621                      struct lov_stripe_md *lsm)
622 {
623         struct ll_inode_info *lli = ll_i2info(inode);
624
625         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
626         if (lsm != NULL) {
627                 if (lli->lli_smd == NULL) {
628                         lli->lli_smd = lsm;
629                         lli->lli_maxbytes = lsm->lsm_maxbytes;
630                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
631                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
632                 } else {
633                         LASSERT (!memcmp (lli->lli_smd, lsm, sizeof (*lsm)));
634                 }
635         }
636
637         if (body->valid & OBD_MD_FLID)
638                 inode->i_ino = body->ino;
639         if (body->valid & OBD_MD_FLATIME)
640                 LTIME_S(inode->i_atime) = body->atime;
641         if (body->valid & OBD_MD_FLMTIME)
642                 LTIME_S(inode->i_mtime) = body->mtime;
643         if (body->valid & OBD_MD_FLCTIME)
644                 LTIME_S(inode->i_ctime) = body->ctime;
645         if (body->valid & OBD_MD_FLMODE)
646                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
647         if (body->valid & OBD_MD_FLTYPE)
648                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
649         if (body->valid & OBD_MD_FLUID)
650                 inode->i_uid = body->uid;
651         if (body->valid & OBD_MD_FLGID)
652                 inode->i_gid = body->gid;
653         if (body->valid & OBD_MD_FLFLAGS)
654                 inode->i_flags = body->flags;
655         if (body->valid & OBD_MD_FLNLINK)
656                 inode->i_nlink = body->nlink;
657         if (body->valid & OBD_MD_FLGENER)
658                 inode->i_generation = body->generation;
659         if (body->valid & OBD_MD_FLRDEV)
660                 inode->i_rdev = to_kdev_t(body->rdev);
661         if (body->valid & OBD_MD_FLSIZE)
662                 inode->i_size = body->size;
663         if (body->valid & OBD_MD_FLBLOCKS)
664                 inode->i_blocks = body->blocks;
665 }
666
667 int ll_read_inode2(struct inode *inode, void *opaque)
668 {
669         struct ll_read_inode2_cookie *lic = opaque;
670         struct mds_body *body = lic->lic_body;
671         struct ll_inode_info *lli = ll_i2info(inode);
672         int rc = 0;
673         ENTRY;
674         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
675
676         sema_init(&lli->lli_open_sem, 1);
677         /* these are 2.4 only, but putting them here for consistency.. */
678         spin_lock_init(&lli->lli_read_extent_lock);
679         INIT_LIST_HEAD(&lli->lli_read_extents);
680         ll_lldo_init(&lli->lli_dirty);
681         lli->lli_flags = 0;
682         lli->lli_maxbytes = LUSTRE_STRIPE_MAXBYTES;
683
684         LASSERT(!lli->lli_smd);
685
686         /* core attributes first */
687         ll_update_inode(inode, body, lic ? lic->lic_lsm : NULL);
688
689         /* OIDEBUG(inode); */
690
691         if (S_ISREG(inode->i_mode)) {
692                 inode->i_op = &ll_file_inode_operations;
693                 inode->i_fop = &ll_file_operations;
694                 inode->i_mapping->a_ops = &ll_aops;
695                 EXIT;
696         } else if (S_ISDIR(inode->i_mode)) {
697                 inode->i_op = &ll_dir_inode_operations;
698                 inode->i_fop = &ll_dir_operations;
699                 inode->i_mapping->a_ops = &ll_dir_aops;
700                 EXIT;
701         } else if (S_ISLNK(inode->i_mode)) {
702                 inode->i_op = &ll_fast_symlink_inode_operations;
703                 EXIT;
704         } else {
705                 inode->i_op = &ll_special_inode_operations;
706                 init_special_inode(inode, inode->i_mode,
707                                    kdev_t_to_nr(inode->i_rdev));
708                 EXIT;
709         }
710
711         return rc;
712 }
713
714
715 void ll_umount_begin(struct super_block *sb)
716 {
717         struct ll_sb_info *sbi = ll_s2sbi(sb);
718         struct obd_device *obd;
719         struct obd_ioctl_data ioc_data = { 0 };
720
721         ENTRY;
722         CDEBUG(D_VFSTRACE, "VFS Op:\n");
723
724         obd = class_conn2obd(&sbi->ll_mdc_conn);
725         obd->obd_no_recov = 1;
726         obd_iocontrol(IOC_OSC_SET_ACTIVE, &sbi->ll_mdc_conn, sizeof ioc_data,
727                       &ioc_data, NULL);
728
729         obd = class_conn2obd(&sbi->ll_osc_conn);
730         obd->obd_no_recov = 1;
731         obd_iocontrol(IOC_OSC_SET_ACTIVE, &sbi->ll_osc_conn, sizeof ioc_data,
732                       &ioc_data, NULL);
733         
734         /* Really, we'd like to wait until there are no requests outstanding,
735          * and then continue.  For now, we just invalidate the requests,
736          * schedule, and hope.
737          */
738         schedule();
739
740         EXIT;
741 }
742
743 static kmem_cache_t *ll_inode_cachep;
744
745 static struct inode *ll_alloc_inode(struct super_block *sb)
746 {
747         struct ll_inode_info *lli;
748         OBD_SLAB_ALLOC(lli, ll_inode_cachep, SLAB_KERNEL, sizeof *lli);
749         if (lli == NULL)
750                 return NULL;
751
752         memset(lli, 0, (char *)&lli->lli_vfs_inode - (char *)lli);
753         sema_init(&lli->lli_open_sem, 1);
754         init_MUTEX(&lli->lli_size_valid_sem);
755         lli->lli_maxbytes = LUSTRE_STRIPE_MAXBYTES;
756
757         return &lli->lli_vfs_inode;
758 }
759
760 static void ll_destroy_inode(struct inode *inode)
761 {
762         OBD_SLAB_FREE(ll_inode_cachep, ll_i2info(inode),
763                       sizeof(struct ll_inode_info));
764 }
765
766 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
767 {
768         struct ll_inode_info *lli = foo;
769
770         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
771             SLAB_CTOR_CONSTRUCTOR)
772                 inode_init_once(&lli->lli_vfs_inode);
773 }
774
775 int ll_init_inodecache(void)
776 {
777         ll_inode_cachep = kmem_cache_create("lustre_inode_cache",
778                                             sizeof(struct ll_inode_info),
779                                             0, SLAB_HWCACHE_ALIGN,
780                                             init_once, NULL);
781         if (ll_inode_cachep == NULL)
782                 return -ENOMEM;
783         return 0;
784 }
785
786 void ll_destroy_inodecache(void)
787 {
788         if (kmem_cache_destroy(ll_inode_cachep))
789                 CERROR("ll_inode_cache: not all structures were freed\n");
790 }
791
792
793
794 /* exported operations */
795 struct super_operations ll_super_operations =
796 {
797         alloc_inode: ll_alloc_inode,
798         destroy_inode: ll_destroy_inode,
799         clear_inode: ll_clear_inode,
800 //        delete_inode: ll_delete_inode,
801         put_super: ll_put_super,
802         statfs: ll_statfs,
803         umount_begin: ll_umount_begin
804 };
805
806
807 struct file_system_type lustre_lite_fs_type = {
808         .owner  = THIS_MODULE,
809         .name =   "lustre_lite",
810         .get_sb = ll_get_sb,
811         .kill_sb = kill_anon_super,
812 };
813
814 static int __init init_lustre_lite(void)
815 {
816         int rc;
817         printk(KERN_INFO "Lustre Lite Client File System; "
818                "info@clusterfs.com\n");
819         rc = ll_init_inodecache();
820         if (rc)
821                 return -ENOMEM;
822         ll_file_data_slab = kmem_cache_create("ll_file_data",
823                                               sizeof(struct ll_file_data), 0,
824                                               SLAB_HWCACHE_ALIGN, NULL, NULL);
825         if (ll_file_data_slab == NULL) {
826                 ll_destroy_inodecache();
827                 return -ENOMEM;
828         }
829
830         proc_lustre_fs_root = proc_lustre_root ?
831                               proc_mkdir("llite", proc_lustre_root) : NULL;
832
833         return register_filesystem(&lustre_lite_fs_type);
834 }
835
836 static void __exit exit_lustre_lite(void)
837 {
838         unregister_filesystem(&lustre_lite_fs_type);
839         ll_destroy_inodecache();
840         kmem_cache_destroy(ll_file_data_slab);
841         if (proc_lustre_fs_root) {
842                 lprocfs_remove(proc_lustre_fs_root);
843                 proc_lustre_fs_root = NULL;
844         }
845 }
846
847 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
848 MODULE_DESCRIPTION("Lustre Lite Client File System");
849 MODULE_LICENSE("GPL");
850
851 module_init(init_lustre_lite);
852 module_exit(exit_lustre_lite);
853 #endif