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