Whamcloud - gitweb
Landing b_bug974 onto HEAD (20040213_1538).
[fs/lustre-release.git] / lustre / liblustre / 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 <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/fcntl.h>
33 #include <sys/queue.h>
34 #ifndef __CYGWIN__
35 # include <sys/statvfs.h>
36 #else
37 # include <sys/statfs.h>
38 #endif
39
40 #include <sysio.h>
41 #include <fs.h>
42 #include <mount.h>
43 #include <inode.h>
44 #include <file.h>
45
46 #include "llite_lib.h"
47
48 static void llu_fsop_gone(struct filesys *fs)
49 {
50         struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
51         struct obd_device *obd = class_exp2obd(sbi->ll_mdc_exp);
52         struct ll_fid rootfid;
53         ENTRY;
54
55         list_del(&sbi->ll_conn_chain);
56         obd_disconnect(sbi->ll_osc_exp, 0);
57
58         /* NULL request to force sync on the MDS, and get the last_committed
59          * value to flush remaining RPCs from the sending queue on client.
60          *
61          * XXX This should be an mdc_sync() call to sync the whole MDS fs,
62          *     which we can call for other reasons as well.
63          */
64         if (!obd->obd_no_recov)
65                 mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
66
67         obd_disconnect(sbi->ll_mdc_exp, 0);
68
69         OBD_FREE(sbi, sizeof(*sbi));
70
71         EXIT;
72 }
73
74 static struct inode_ops llu_inode_ops;
75
76 void llu_update_inode(struct inode *inode, struct mds_body *body,
77                       struct lov_stripe_md *lsm)
78 {
79         struct llu_inode_info *lli = llu_i2info(inode);
80
81         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
82         if (lsm != NULL) {
83                 if (lli->lli_smd == NULL) {
84                         lli->lli_smd = lsm;
85                         lli->lli_maxbytes = lsm->lsm_maxbytes;
86                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
87                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
88                 } else {
89                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
90                                 CERROR("lsm mismatch for inode %ld\n",
91                                        lli->lli_st_ino);
92                                 LBUG();
93                         }
94                 }
95         }
96
97         if (body->valid & OBD_MD_FLID)
98                 lli->lli_st_ino = body->ino;
99         if (body->valid & OBD_MD_FLATIME)
100                 LTIME_S(lli->lli_st_atime) = body->atime;
101         if (body->valid & OBD_MD_FLMTIME)
102                 LTIME_S(lli->lli_st_mtime) = body->mtime;
103         if (body->valid & OBD_MD_FLCTIME)
104                 LTIME_S(lli->lli_st_ctime) = body->ctime;
105         if (body->valid & OBD_MD_FLMODE)
106                 lli->lli_st_mode = (lli->lli_st_mode & S_IFMT)|(body->mode & ~S_IFMT);
107         if (body->valid & OBD_MD_FLTYPE)
108                 lli->lli_st_mode = (lli->lli_st_mode & ~S_IFMT)|(body->mode & S_IFMT);
109         if (body->valid & OBD_MD_FLUID)
110                 lli->lli_st_uid = body->uid;
111         if (body->valid & OBD_MD_FLGID)
112                 lli->lli_st_gid = body->gid;
113         if (body->valid & OBD_MD_FLFLAGS)
114                 lli->lli_st_flags = body->flags;
115         if (body->valid & OBD_MD_FLNLINK)
116                 lli->lli_st_nlink = body->nlink;
117         if (body->valid & OBD_MD_FLGENER)
118                 lli->lli_st_generation = body->generation;
119         if (body->valid & OBD_MD_FLRDEV)
120                 lli->lli_st_rdev = body->rdev;
121         if (body->valid & OBD_MD_FLSIZE)
122                 lli->lli_st_size = body->size;
123         if (body->valid & OBD_MD_FLBLOCKS)
124                 lli->lli_st_blocks = body->blocks;
125
126         /* fillin fid */
127         if (body->valid & OBD_MD_FLID)
128                 lli->lli_fid.id = body->ino;
129         if (body->valid & OBD_MD_FLGENER)
130                 lli->lli_fid.generation = body->generation;
131         if (body->valid & OBD_MD_FLTYPE)
132                 lli->lli_fid.f_type = body->mode & S_IFMT;
133 }
134
135 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
136 {
137         struct llu_inode_info *lli = llu_i2info(dst);
138
139         valid &= src->o_valid;
140
141         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
142                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
143                        src->o_valid, 
144                        LTIME_S(lli->lli_st_mtime), LTIME_S(lli->lli_st_ctime),
145                        (long)src->o_mtime, (long)src->o_ctime);
146
147         if (valid & OBD_MD_FLATIME)
148                 LTIME_S(lli->lli_st_atime) = src->o_atime;
149         if (valid & OBD_MD_FLMTIME)
150                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
151         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
152                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
153         if (valid & OBD_MD_FLSIZE)
154                 lli->lli_st_size = src->o_size;
155         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
156                 lli->lli_st_blocks = src->o_blocks;
157         if (valid & OBD_MD_FLBLKSZ)
158                 lli->lli_st_blksize = src->o_blksize;
159         if (valid & OBD_MD_FLTYPE)
160                 lli->lli_st_mode = (lli->lli_st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
161         if (valid & OBD_MD_FLMODE)
162                 lli->lli_st_mode = (lli->lli_st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
163         if (valid & OBD_MD_FLUID)
164                 lli->lli_st_uid = src->o_uid;
165         if (valid & OBD_MD_FLGID)
166                 lli->lli_st_gid = src->o_gid;
167         if (valid & OBD_MD_FLFLAGS)
168                 lli->lli_st_flags = src->o_flags;
169         if (valid & OBD_MD_FLGENER)
170                 lli->lli_st_generation = src->o_generation;
171 }
172
173 #define S_IRWXUGO       (S_IRWXU|S_IRWXG|S_IRWXO)
174 #define S_IALLUGO       (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
175
176 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
177 {
178         struct llu_inode_info *lli = llu_i2info(src);
179         obd_flag newvalid = 0;
180
181         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
182                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
183                        valid, LTIME_S(lli->lli_st_mtime), 
184                        LTIME_S(lli->lli_st_ctime));
185
186         if (valid & OBD_MD_FLATIME) {
187                 dst->o_atime = LTIME_S(lli->lli_st_atime);
188                 newvalid |= OBD_MD_FLATIME;
189         }
190         if (valid & OBD_MD_FLMTIME) {
191                 dst->o_mtime = LTIME_S(lli->lli_st_mtime);
192                 newvalid |= OBD_MD_FLMTIME;
193         }
194         if (valid & OBD_MD_FLCTIME) {
195                 dst->o_ctime = LTIME_S(lli->lli_st_ctime);
196                 newvalid |= OBD_MD_FLCTIME;
197         }
198         if (valid & OBD_MD_FLSIZE) {
199                 dst->o_size = lli->lli_st_size;
200                 newvalid |= OBD_MD_FLSIZE;
201         }
202         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
203                 dst->o_blocks = lli->lli_st_blocks;
204                 newvalid |= OBD_MD_FLBLOCKS;
205         }
206         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
207                 dst->o_blksize = lli->lli_st_blksize;
208                 newvalid |= OBD_MD_FLBLKSZ;
209         }
210         if (valid & OBD_MD_FLTYPE) {
211                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(lli->lli_st_mode & S_IFMT);
212                 newvalid |= OBD_MD_FLTYPE;
213         }
214         if (valid & OBD_MD_FLMODE) {
215                 dst->o_mode = (dst->o_mode & S_IFMT)|(lli->lli_st_mode & S_IALLUGO);
216                 newvalid |= OBD_MD_FLMODE;
217         }
218         if (valid & OBD_MD_FLUID) {
219                 dst->o_uid = lli->lli_st_uid;
220                 newvalid |= OBD_MD_FLUID;
221         }
222         if (valid & OBD_MD_FLGID) {
223                 dst->o_gid = lli->lli_st_gid;
224                 newvalid |= OBD_MD_FLGID;
225         }
226         if (valid & OBD_MD_FLFLAGS) {
227                 dst->o_flags = lli->lli_st_flags;
228                 newvalid |= OBD_MD_FLFLAGS;
229         }
230         if (valid & OBD_MD_FLGENER) {
231                 dst->o_generation = lli->lli_st_generation;
232                 newvalid |= OBD_MD_FLGENER;
233         }
234
235         dst->o_valid |= newvalid;
236 }
237
238 /*
239  * really does the getattr on the inode and updates its fields
240  */
241 int llu_inode_getattr(struct inode *inode, struct lov_stripe_md *lsm)
242 {
243         struct llu_inode_info *lli = llu_i2info(inode);
244         struct obd_export *exp = llu_i2obdexp(inode);
245         struct ptlrpc_request_set *set;
246         struct obdo oa;
247         obd_flag refresh_valid;
248         int rc;
249         ENTRY;
250
251         LASSERT(lsm);
252         LASSERT(lli);
253
254         memset(&oa, 0, sizeof oa);
255         oa.o_id = lsm->lsm_object_id;
256         oa.o_mode = S_IFREG;
257         oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
258                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
259                 OBD_MD_FLCTIME;
260
261         set = ptlrpc_prep_set();
262         if (set == NULL) {
263                 CERROR ("ENOMEM allocing request set\n");
264                 rc = -ENOMEM;
265         } else {
266                 rc = obd_getattr_async(exp, &oa, lsm, set);
267                 if (rc == 0)
268                         rc = ptlrpc_set_wait(set);
269                 ptlrpc_set_destroy(set);
270         }
271         if (rc)
272                 RETURN(rc);
273
274         refresh_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
275                         OBD_MD_FLCTIME | OBD_MD_FLSIZE;
276
277         /* We set this flag in commit write as we extend the file size.  When
278          * the bit is set and the lock is canceled that covers the file size,
279          * we clear the bit.  This is enough to protect the window where our
280          * local size extension is needed for writeback.  However, it relies on
281          * behaviour that won't be true in the near future.  This assumes that
282          * all getattr callers get extent locks, which they currnetly do.  It
283          * also assumes that we only send discarding asts for {0,eof} truncates
284          * as is currently the case.  This will have to be replaced by the
285          * proper eoc communication between clients and the ost, which is on
286          * its way. */
287         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
288                 if (oa.o_size < lli->lli_st_size)
289                         refresh_valid &= ~OBD_MD_FLSIZE;
290                 else 
291                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
292         }
293
294         obdo_refresh_inode(inode, &oa, refresh_valid);
295
296 /*
297         if (inode->i_blksize < PAGE_CACHE_SIZE)
298                 inode->i_blksize = PAGE_CACHE_SIZE;
299
300         CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
301                lsm->lsm_object_id, inode->i_size, inode->i_blocks,
302                inode->i_blksize);
303 */
304         RETURN(0);
305 }
306
307 static struct inode* llu_new_inode(struct filesys *fs,
308                                    struct ll_fid *fid)
309 {
310         struct inode *inode;
311         struct llu_inode_info *lli;
312
313         OBD_ALLOC(lli, sizeof(*lli));
314         if (!lli)
315                 return NULL;
316
317         /* initialize lli here */
318         lli->lli_sbi = llu_fs2sbi(fs);
319         lli->lli_smd = NULL;
320         lli->lli_symlink_name = NULL;
321         lli->lli_flags = 0;
322         lli->lli_maxbytes = (__u64)(~0UL);
323         lli->lli_file_data = NULL;
324
325         lli->lli_sysio_fid.fid_data = &lli->lli_fid;
326         lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
327
328         memcpy(&lli->lli_fid, fid, sizeof(*fid));
329
330         /* file identifier is needed by functions like _sysio_i_find() */
331         inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
332 #ifndef AUTOMOUNT_FILE_NAME
333                              fid->f_type & S_IFMT,
334 #else
335                              fid->f_type, /* all of the bits! */
336 #endif
337                              0, 0,
338                              &llu_inode_ops, lli);
339
340         if (!inode)
341                 OBD_FREE(lli, sizeof(*lli));
342
343         return inode;
344 }
345
346 #if 0
347 static int ll_intent_to_lock_mode(struct lookup_intent *it)
348 {
349         /* CREAT needs to be tested before open (both could be set) */
350         if (it->it_op & IT_CREAT)
351                 return LCK_PW;
352         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
353                 return LCK_PR;
354
355         LBUG();
356         RETURN(-EINVAL);
357 }
358 #endif
359
360 #if 0
361 int ll_it_open_error(int phase, struct lookup_intent *it)
362 {
363         if (it_disposition(it, DISP_OPEN_OPEN)) {
364                 if (phase == DISP_OPEN_OPEN)
365                         return it->d.lustre.it_status;
366                 else
367                         return 0;
368         }
369
370         if (it_disposition(it, DISP_OPEN_CREATE)) {
371                 if (phase == DISP_OPEN_CREATE)
372                         return it->d.lustre.it_status;
373                 else
374                         return 0;
375         }
376
377         if (it_disposition(it, DISP_LOOKUP_EXECD)) {
378                 if (phase == DISP_LOOKUP_EXECD)
379                         return it->d.lustre.it_status;
380                 else
381                         return 0;
382         }
383         CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition, it->d.lustre.it_status);
384         LBUG();
385         return 0;
386 }
387 #endif
388
389 static int llu_have_md_lock(struct inode *inode)
390 {
391         struct llu_sb_info *sbi = llu_i2sbi(inode);
392         struct llu_inode_info *lli = llu_i2info(inode);
393         struct lustre_handle lockh;
394         struct ldlm_res_id res_id = { .name = {0} };
395         struct obd_device *obddev;
396         int flags;
397         ENTRY;
398
399         LASSERT(inode);
400
401         obddev = sbi->ll_mdc_exp->exp_obd;
402         res_id.name[0] = lli->lli_st_ino;
403         res_id.name[1] = lli->lli_st_generation;
404
405         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
406
407         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
408         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
409                             NULL, 0, LCK_PR, &lockh)) {
410                 ldlm_lock_decref(&lockh, LCK_PR);
411                 RETURN(1);
412         }
413
414         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
415                             NULL, 0, LCK_PW, &lockh)) {
416                 ldlm_lock_decref(&lockh, LCK_PW);
417                 RETURN(1);
418         }
419         RETURN(0);
420 }
421
422 static int llu_inode_revalidate(struct inode *inode)
423 {
424         struct llu_inode_info *lli = llu_i2info(inode);
425         struct lov_stripe_md *lsm = NULL;
426         ENTRY;
427
428         if (!inode) {
429                 CERROR("REPORT THIS LINE TO PETER\n");
430                 RETURN(0);
431         }
432
433         if (!llu_have_md_lock(inode)) {
434                 struct lustre_md md;
435                 struct ptlrpc_request *req = NULL;
436                 struct llu_sb_info *sbi = llu_i2sbi(inode);
437                 struct ll_fid fid;
438                 unsigned long valid = 0;
439                 int rc, ealen = 0;
440
441                 /* Why don't we update all valid MDS fields here, if we're
442                  * doing an RPC anyways?  -phil */
443                 if (S_ISREG(lli->lli_st_mode)) {
444                         ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
445                         valid |= OBD_MD_FLEASIZE;
446                 }
447                 ll_inode2fid(&fid, inode);
448                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
449                 if (rc) {
450                         CERROR("failure %d inode %lu\n", rc, lli->lli_st_ino);
451                         RETURN(-abs(rc));
452                 }
453                 rc = mdc_req2lustre_md(req, 0, sbi->ll_osc_exp, &md);
454
455                 /* XXX Too paranoid? */
456                 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
457                     !((md.body->valid & OBD_MD_FLNLINK) &&
458                       (md.body->nlink == 0))) {
459                         CERROR("Asked for %s eadata but got %s (%d)\n",
460                                (valid & OBD_MD_FLEASIZE) ? "some" : "no",
461                                (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
462                                 md.body->eadatasize);
463                 }
464                 if (rc) {
465                         ptlrpc_req_finished(req);
466                         RETURN(rc);
467                 }
468
469
470                 llu_update_inode(inode, md.body, md.lsm);
471                 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
472                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
473
474                 if (md.body->valid & OBD_MD_FLSIZE)
475                         set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
476                                 &llu_i2info(inode)->lli_flags);
477                 ptlrpc_req_finished(req);
478         }
479
480         lsm = llu_i2info(inode)->lli_smd;
481         if (!lsm)       /* object not yet allocated, don't validate size */
482                 RETURN(0);
483
484         /*
485          * unfortunately stat comes in through revalidate and we don't
486          * differentiate this use from initial instantiation.  we're
487          * also being wildly conservative and flushing write caches
488          * so that stat really returns the proper size.
489          */
490         {
491                 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
492                 struct lustre_handle lockh = {0};
493                 ldlm_error_t err;
494
495                 err = llu_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
496                 if (err != ELDLM_OK)
497                         RETURN(err);
498
499                 llu_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
500         }
501         RETURN(0);
502 }
503
504 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
505 {
506         struct llu_inode_info *lli = llu_i2info(ino);
507
508         b->st_dev = lli->lli_st_dev;
509         b->st_ino = lli->lli_st_ino;
510         b->st_mode = lli->lli_st_mode;
511         b->st_nlink = lli->lli_st_nlink;
512         b->st_uid = lli->lli_st_uid;
513         b->st_gid = lli->lli_st_gid;
514         b->st_rdev = lli->lli_st_rdev;
515         b->st_size = lli->lli_st_size;
516         b->st_blksize = lli->lli_st_blksize;
517         b->st_blocks = lli->lli_st_blocks;
518         b->st_atime = lli->lli_st_atime;
519         b->st_mtime = lli->lli_st_mtime;
520         b->st_ctime = lli->lli_st_ctime;
521 }
522
523 static int llu_iop_getattr(struct pnode *pno,
524                            struct inode *ino,
525                            struct intnl_stat *b)
526 {
527         int rc;
528         ENTRY;
529
530         if (!ino) {
531                 LASSERT(pno);
532                 LASSERT(pno->p_base->pb_ino);
533                 ino = pno->p_base->pb_ino;
534         } else {
535                 LASSERT(!pno || pno->p_base->pb_ino == ino);
536         }
537
538         /* libsysio might call us directly without intent lock,
539          * we must re-fetch the attrs here
540          */
541         rc = llu_inode_revalidate(ino);
542         if (!rc) {
543                 copy_stat_buf(ino, b);
544
545                 if (llu_i2info(ino)->lli_it) {
546                         struct lookup_intent *it;
547
548                         LL_GET_INTENT(ino, it);
549                         it->it_op_release(it);
550                         OBD_FREE(it, sizeof(*it));
551                 }
552         }
553
554         RETURN(rc);
555 }
556
557 static int null_if_equal(struct ldlm_lock *lock, void *data)
558 {
559         if (data == lock->l_ast_data)
560                 lock->l_ast_data = NULL;
561
562         if (lock->l_req_mode != lock->l_granted_mode)
563                 return LDLM_ITER_STOP;
564
565         return LDLM_ITER_CONTINUE;
566 }
567
568 void llu_clear_inode(struct inode *inode)
569 {
570         struct ll_fid fid;
571         struct llu_inode_info *lli = llu_i2info(inode);
572         struct llu_sb_info *sbi = llu_i2sbi(inode);
573         ENTRY;
574
575         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu(%p)\n", lli->lli_st_ino,
576                lli->lli_st_generation, inode);
577
578         ll_inode2fid(&fid, inode);
579         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
580         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
581
582         if (lli->lli_smd)
583                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
584                                   null_if_equal, inode);
585
586         if (lli->lli_smd) {
587                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
588                 lli->lli_smd = NULL;
589         }
590
591         if (lli->lli_symlink_name) {
592                 OBD_FREE(lli->lli_symlink_name,
593                          strlen(lli->lli_symlink_name) + 1);
594                 lli->lli_symlink_name = NULL;
595         }
596
597         EXIT;
598 }
599
600 void llu_iop_gone(struct inode *inode)
601 {
602         struct llu_inode_info *lli = llu_i2info(inode);
603         ENTRY;
604
605         llu_clear_inode(inode);
606
607         OBD_FREE(lli, sizeof(*lli));
608         EXIT;
609 }
610
611 static int inode_setattr(struct inode * inode, struct iattr * attr)
612 {
613         unsigned int ia_valid = attr->ia_valid;
614         struct llu_inode_info *lli = llu_i2info(inode);
615         int error = 0;
616
617         if (ia_valid & ATTR_SIZE) {
618                 error = llu_vmtruncate(inode, attr->ia_size);
619                 if (error)
620                         goto out;
621         }
622
623         if (ia_valid & ATTR_UID)
624                 lli->lli_st_uid = attr->ia_uid;
625         if (ia_valid & ATTR_GID)
626                 lli->lli_st_gid = attr->ia_gid;
627         if (ia_valid & ATTR_ATIME)
628                 lli->lli_st_atime = attr->ia_atime;
629         if (ia_valid & ATTR_MTIME)
630                 lli->lli_st_mtime = attr->ia_mtime;
631         if (ia_valid & ATTR_CTIME)
632                 lli->lli_st_ctime = attr->ia_ctime;
633         if (ia_valid & ATTR_MODE) {
634                 lli->lli_st_mode = attr->ia_mode;
635                 if (!in_group_p(lli->lli_st_gid) && !capable(CAP_FSETID))
636                         lli->lli_st_mode &= ~S_ISGID;
637         }
638         /* mark_inode_dirty(inode); */
639 out:
640         return error;
641 }
642
643 /* If this inode has objects allocated to it (lsm != NULL), then the OST
644  * object(s) determine the file size and mtime.  Otherwise, the MDS will
645  * keep these values until such a time that objects are allocated for it.
646  * We do the MDS operations first, as it is checking permissions for us.
647  * We don't to the MDS RPC if there is nothing that we want to store there,
648  * otherwise there is no harm in updating mtime/atime on the MDS if we are
649  * going to do an RPC anyways.
650  *
651  * If we are doing a truncate, we will send the mtime and ctime updates
652  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
653  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
654  * at the same time.
655  */
656 #define OST_ATTR (ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME | \
657                   ATTR_ATIME | ATTR_ATIME_SET | ATTR_SIZE)
658 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
659 {
660         struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
661         struct llu_sb_info *sbi = llu_i2sbi(inode);
662         struct llu_inode_info *lli = llu_i2info(inode);
663         struct ptlrpc_request *request = NULL;
664         struct mdc_op_data op_data;
665         int ia_valid = attr->ia_valid;
666         int rc = 0;
667         ENTRY;
668
669         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
670
671         if (ia_valid & ATTR_SIZE) {
672                 if (attr->ia_size > ll_file_maxbytes(inode)) {
673                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
674                                attr->ia_size, ll_file_maxbytes(inode));
675                         RETURN(-EFBIG);
676                 }
677
678                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
679         }
680
681         /* We mark all of the fields "set" so MDS/OST does not re-set them */
682         if (attr->ia_valid & ATTR_CTIME) {
683                 attr->ia_ctime = CURRENT_TIME;
684                 attr->ia_valid |= ATTR_CTIME_SET;
685         }
686         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
687                 attr->ia_atime = CURRENT_TIME;
688                 attr->ia_valid |= ATTR_ATIME_SET;
689         }
690         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
691                 attr->ia_mtime = CURRENT_TIME;
692                 attr->ia_valid |= ATTR_MTIME_SET;
693         }
694
695         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
696                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
697                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
698                        LTIME_S(CURRENT_TIME));
699         if (lsm)
700                 attr->ia_valid &= ~ATTR_SIZE;
701
702         /* If only OST attributes being set on objects, don't do MDS RPC.
703          * In that case, we need to check permissions and update the local
704          * inode ourselves so we can call obdo_from_inode() always. */
705         if (ia_valid & (lsm ? ~(OST_ATTR | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
706                 struct lustre_md md;
707                 llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
708
709                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
710                                   attr, NULL, 0, NULL, 0, &request);
711
712                 if (rc) {
713                         ptlrpc_req_finished(request);
714                         if (rc != -EPERM && rc != -EACCES)
715                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
716                         RETURN(rc);
717                 }
718
719                 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
720                 if (rc) {
721                         ptlrpc_req_finished(request);
722                         RETURN(rc);
723                 }
724                 llu_update_inode(inode, md.body, md.lsm);
725                 ptlrpc_req_finished(request);
726
727                 if (!md.lsm || !S_ISREG(lli->lli_st_mode)) {
728                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
729                         RETURN(0);
730                 }
731         } else {
732                 /* The OST doesn't check permissions, but the alternative is
733                  * a gratuitous RPC to the MDS.  We already rely on the client
734                  * to do read/write/truncate permission checks, so is mtime OK?
735                  */
736                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
737                         /* from sys_utime() */
738                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
739                                 if (current->fsuid != lli->lli_st_uid &&
740                                     (rc = ll_permission(inode, 0/*MAY_WRITE*/, NULL)) != 0)
741                                         RETURN(rc);
742                         } else {
743                                 /* from inode_change_ok() */
744                                 if (current->fsuid != lli->lli_st_uid &&
745                                     !capable(CAP_FOWNER))
746                                         RETURN(-EPERM);
747                         }
748                 }
749
750                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
751                 inode_setattr(inode, attr);
752         }
753
754         if (ia_valid & ATTR_SIZE) {
755                 struct ldlm_extent extent = { .start = attr->ia_size,
756                                               .end = OBD_OBJECT_EOF };
757                 struct lustre_handle lockh = { 0 };
758                 int err, ast_flags = 0;
759                 /* XXX when we fix the AST intents to pass the discard-range
760                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
761                  * XXX here. */
762
763                 /* Writeback uses inode->i_size to determine how far out
764                  * its cached pages go.  ll_truncate gets a PW lock, canceling
765                  * our lock, _after_ it has updated i_size.  this can confuse
766                  *
767                  * We really need to get our PW lock before we change
768                  * inode->i_size.  If we don't we can race with other
769                  * i_size updaters on our node, like ll_file_read.  We
770                  * can also race with i_size propogation to other
771                  * nodes through dirtying and writeback of final cached
772                  * pages.  This last one is especially bad for racing
773                  * o_append users on other nodes. */
774                 if (extent.start == 0)
775                         ast_flags = LDLM_AST_DISCARD_DATA;
776                 rc = llu_extent_lock_no_validate(NULL, inode, lsm, LCK_PW,
777                                                  &extent, &lockh, ast_flags);
778                 if (rc != ELDLM_OK) {
779                         if (rc > 0)
780                                 RETURN(-ENOLCK);
781                         RETURN(rc);
782                 }
783
784                 rc = llu_vmtruncate(inode, attr->ia_size);
785                 if (rc == 0)
786                         set_bit(LLI_F_HAVE_OST_SIZE_LOCK,
787                                 &llu_i2info(inode)->lli_flags);
788
789                 /* unlock now as we don't mind others file lockers racing with
790                  * the mds updates below? */
791                 err = llu_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
792                 if (err) {
793                         CERROR("llu_extent_unlock failed: %d\n", err);
794                         if (!rc)
795                                 rc = err;
796                 }
797         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
798                 struct obdo oa;
799
800                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
801                        lli->lli_st_ino, LTIME_S(attr->ia_mtime));
802                 oa.o_id = lsm->lsm_object_id;
803                 oa.o_valid = OBD_MD_FLID;
804                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
805                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
806                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
807                 if (rc)
808                         CERROR("obd_setattr fails: rc=%d\n", rc);
809         }
810         RETURN(rc);
811 }
812
813 /* FIXME here we simply act as a thin layer to glue it with
814  * llu_setattr_raw(), which is copy from kernel
815  */
816 static int llu_iop_setattr(struct pnode *pno,
817                            struct inode *ino,
818                            unsigned mask,
819                            struct intnl_stat *stbuf)
820 {
821         struct iattr iattr;
822         ENTRY;
823
824         memset(&iattr, 0, sizeof(iattr));
825
826         if (mask & SETATTR_MODE) {
827                 iattr.ia_mode = stbuf->st_mode;
828                 iattr.ia_valid |= ATTR_MODE;
829         }
830         if (mask & SETATTR_MTIME) {
831                 iattr.ia_mtime = stbuf->st_mtime;
832                 iattr.ia_valid |= ATTR_MTIME;
833         }
834         if (mask & SETATTR_ATIME) {
835                 iattr.ia_atime = stbuf->st_atime;
836                 iattr.ia_valid |= ATTR_ATIME;
837         }
838         if (mask & SETATTR_UID) {
839                 iattr.ia_uid = stbuf->st_uid;
840                 iattr.ia_valid |= ATTR_UID;
841         }
842         if (mask & SETATTR_GID) {
843                 iattr.ia_gid = stbuf->st_gid;
844                 iattr.ia_valid |= ATTR_GID;
845         }
846         if (mask & SETATTR_LEN) {
847                 iattr.ia_size = stbuf->st_size; /* FIXME signed expansion problem */
848                 iattr.ia_valid |= ATTR_SIZE;
849         }
850
851         iattr.ia_valid |= ATTR_RAW;
852
853         RETURN(llu_setattr_raw(ino, &iattr));
854 }
855
856 #define EXT2_LINK_MAX           32000
857
858 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
859 {
860         struct inode *dir = pno->p_base->pb_parent->pb_ino;
861         struct qstr *qstr = &pno->p_base->pb_name;
862         const char *name = qstr->name;
863         int len = qstr->len;
864         struct ptlrpc_request *request = NULL;
865         struct llu_sb_info *sbi = llu_i2sbi(dir);
866         struct mdc_op_data op_data;
867         int err = -EMLINK;
868         ENTRY;
869
870         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
871                 RETURN(err);
872
873         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
874         err = mdc_create(sbi->ll_mdc_exp, &op_data,
875                          tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
876                          current->fsuid, current->fsgid, 0, &request);
877         ptlrpc_req_finished(request);
878         RETURN(err);
879 }
880
881 static int llu_readlink_internal(struct inode *inode,
882                                  struct ptlrpc_request **request,
883                                  char **symname)
884 {
885         struct llu_inode_info *lli = llu_i2info(inode);
886         struct llu_sb_info *sbi = llu_i2sbi(inode);
887         struct ll_fid fid;
888         struct mds_body *body;
889         int rc, symlen = lli->lli_st_size + 1;
890         ENTRY;
891
892         *request = NULL;
893
894         if (lli->lli_symlink_name) {
895                 *symname = lli->lli_symlink_name;
896                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
897                 RETURN(0);
898         }
899
900         ll_inode2fid(&fid, inode);
901         rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
902                          OBD_MD_LINKNAME, symlen, request);
903         if (rc) {
904                 CERROR("inode %lu: rc = %d\n", lli->lli_st_ino, rc);
905                 RETURN(rc);
906         }
907
908         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
909         LASSERT (body != NULL);
910         LASSERT_REPSWABBED (*request, 0);
911
912         if ((body->valid & OBD_MD_LINKNAME) == 0) {
913                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
914                 GOTO (failed, rc = -EPROTO);
915         }
916         
917         LASSERT (symlen != 0);
918         if (body->eadatasize != symlen) {
919                 CERROR ("inode %lu: symlink length %d not expected %d\n",
920                         lli->lli_st_ino, body->eadatasize - 1, symlen - 1);
921                 GOTO (failed, rc = -EPROTO);
922         }
923
924         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
925         if (*symname == NULL ||
926             strnlen (*symname, symlen) != symlen - 1) {
927                 /* not full/NULL terminated */
928                 CERROR ("inode %lu: symlink not NULL terminated string"
929                         "of length %d\n", lli->lli_st_ino, symlen - 1);
930                 GOTO (failed, rc = -EPROTO);
931         }
932
933         OBD_ALLOC(lli->lli_symlink_name, symlen);
934         /* do not return an error if we cannot cache the symlink locally */
935         if (lli->lli_symlink_name)
936                 memcpy(lli->lli_symlink_name, *symname, symlen);
937
938         RETURN(0);
939
940  failed:
941         ptlrpc_req_finished (*request);
942         RETURN (-EPROTO);
943 }
944
945 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
946 {
947         struct inode *inode = pno->p_base->pb_ino;
948         struct ptlrpc_request *request;
949         char *symname;
950         int rc;
951         ENTRY;
952
953         /* on symlinks lli_open_sem protects lli_symlink_name allocation/data */
954 /*
955         down(&lli->lli_open_sem);
956 */
957         rc = llu_readlink_internal(inode, &request, &symname);
958         if (rc)
959                 GOTO(out, rc);
960
961         LASSERT(symname);
962         strncpy(data, symname, bufsize);
963
964         ptlrpc_req_finished(request);
965  out:
966 /*
967         up(&lli->lli_open_sem);
968 */
969         RETURN(rc);
970 }
971
972 static int llu_iop_mknod_raw(struct pnode *pno,
973                              mode_t mode,
974                              dev_t dev)
975 {
976         struct ptlrpc_request *request = NULL;
977         struct inode *dir = pno->p_parent->p_base->pb_ino;
978         struct llu_sb_info *sbi = llu_i2sbi(dir);
979         struct mdc_op_data op_data;
980         int err = -EMLINK;
981         ENTRY;
982
983         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu\n",
984                pno->p_base->pb_name.name, llu_i2info(dir)->lli_st_ino);
985
986         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
987                 RETURN(err);
988
989         mode &= ~current->fs->umask;
990
991         switch (mode & S_IFMT) {
992         case 0:
993         case S_IFREG:
994                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
995         case S_IFCHR:
996         case S_IFBLK:
997         case S_IFIFO:
998         case S_IFSOCK:
999                 llu_prepare_mdc_op_data(&op_data, dir, NULL,
1000                                         pno->p_base->pb_name.name,
1001                                         pno->p_base->pb_name.len,
1002                                         0);
1003                 err = mdc_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
1004                                  current->fsuid, current->fsgid, dev, &request);
1005                 ptlrpc_req_finished(request);
1006                 break;
1007         case S_IFDIR:
1008                 err = -EPERM;
1009                 break;
1010         default:
1011                 err = -EINVAL;
1012         }
1013         RETURN(err);
1014 }
1015
1016 #if 0
1017 static int llu_mdc_unlink(struct inode *dir, struct inode *child, __u32 mode,
1018                          const char *name, int len)
1019 {
1020         struct ptlrpc_request *request = NULL;
1021         struct mds_body *body;
1022         struct lov_mds_md *eadata;
1023         struct lov_stripe_md *lsm = NULL;
1024         struct obd_trans_info oti = { 0 };
1025         struct mdc_op_data op_data;
1026         struct obdo *oa;
1027         int rc;
1028         ENTRY;
1029
1030         llu_prepare_mdc_op_data(&op_data, dir, child, name, len, mode);
1031         rc = mdc_unlink(&llu_i2sbi(dir)->ll_mdc_conn, &op_data, &request);
1032         if (rc)
1033                 GOTO(out, rc);
1034         /* req is swabbed so this is safe */
1035         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1036
1037         if (!(body->valid & OBD_MD_FLEASIZE))
1038                 GOTO(out, rc = 0);
1039
1040         if (body->eadatasize == 0) {
1041                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1042                 GOTO(out, rc = -EPROTO);
1043         }
1044
1045         /* The MDS sent back the EA because we unlinked the last reference
1046          * to this file. Use this EA to unlink the objects on the OST.
1047          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1048          * check it is complete and sensible. */
1049         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
1050         LASSERT(eadata != NULL);
1051         if (eadata == NULL) {
1052                 CERROR("Can't unpack MDS EA data\n");
1053                 GOTO(out, rc = -EPROTO);
1054         }
1055
1056         rc = obd_unpackmd(llu_i2obdconn(dir), &lsm, eadata, body->eadatasize);
1057         if (rc < 0) {
1058                 CERROR("obd_unpackmd: %d\n", rc);
1059                 GOTO(out, rc);
1060         }
1061         LASSERT(rc >= sizeof(*lsm));
1062
1063         oa = obdo_alloc();
1064         if (oa == NULL)
1065                 GOTO(out_free_memmd, rc = -ENOMEM);
1066
1067         oa->o_id = lsm->lsm_object_id;
1068         oa->o_mode = body->mode & S_IFMT;
1069         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
1070
1071         if (body->valid & OBD_MD_FLCOOKIE) {
1072                 oa->o_valid |= OBD_MD_FLCOOKIE;
1073                 oti.oti_logcookies = lustre_msg_buf(request->rq_repmsg, 3,
1074                                                     body->eadatasize);
1075         }
1076
1077         rc = obd_destroy(llu_i2obdconn(dir), oa, lsm, &oti);
1078         obdo_free(oa);
1079         if (rc)
1080                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
1081                        lsm->lsm_object_id, rc);
1082  out_free_memmd:
1083         obd_free_memmd(llu_i2obdconn(dir), &lsm);
1084  out:
1085         ptlrpc_req_finished(request);
1086         return rc;
1087 }
1088 #endif
1089
1090 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1091 {
1092         struct inode *src = old->p_base->pb_ino;
1093         struct inode *dir = new->p_parent->p_base->pb_ino;
1094         const char *name = new->p_base->pb_name.name;
1095         int namelen = new->p_base->pb_name.len;
1096         struct ptlrpc_request *request = NULL;
1097         struct mdc_op_data op_data;
1098         int rc;
1099         ENTRY;
1100
1101         LASSERT(src);
1102         LASSERT(dir);
1103
1104         llu_prepare_mdc_op_data(&op_data, src, dir, name, namelen, 0);
1105         rc = mdc_link(llu_i2sbi(src)->ll_mdc_exp, &op_data, &request);
1106         ptlrpc_req_finished(request);
1107
1108         RETURN(rc);
1109 }
1110
1111 static int llu_iop_unlink_raw(struct pnode *pno)
1112 {
1113         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1114         struct qstr *qstr = &pno->p_base->pb_name;
1115         const char *name = qstr->name;
1116         int len = qstr->len;
1117         struct inode *target = pno->p_base->pb_ino;
1118         struct ptlrpc_request *request = NULL;
1119         struct mdc_op_data op_data;
1120         int rc;
1121         ENTRY;
1122
1123         LASSERT(target);
1124
1125         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1126         rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1127         if (!rc) {
1128                 rc = llu_objects_destroy(request, dir);
1129
1130                 llu_i2info(target)->lli_stale_flag = 1;
1131                 unhook_stale_inode(pno);
1132         }
1133
1134         ptlrpc_req_finished(request);
1135         RETURN(rc);
1136 }
1137
1138 /* FIXME
1139  * following cases need to be considered later:
1140  * - rename an opened file/dir
1141  * - an opened file be removed in rename
1142  * - rename to remove and hardlink (?opened)
1143  */
1144 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1145 {
1146         struct inode *src = old->p_parent->p_base->pb_ino;
1147         struct inode *tgt = new->p_parent->p_base->pb_ino;
1148         struct inode *tgtinode = new->p_base->pb_ino;
1149         const char *oldname = old->p_base->pb_name.name;
1150         int oldnamelen = old->p_base->pb_name.len;
1151         const char *newname = new->p_base->pb_name.name;
1152         int newnamelen = new->p_base->pb_name.len;
1153         struct ptlrpc_request *request = NULL;
1154         struct mdc_op_data op_data;
1155         int rc;
1156         ENTRY;
1157
1158         LASSERT(src);
1159         LASSERT(tgt);
1160
1161         llu_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
1162         rc = mdc_rename(llu_i2sbi(src)->ll_mdc_exp, &op_data,
1163                         oldname, oldnamelen, newname, newnamelen,
1164                         &request);
1165         if (!rc) {
1166                 rc = llu_objects_destroy(request, src);
1167
1168                 if (tgtinode) {
1169                         llu_i2info(tgtinode)->lli_stale_flag = 1;
1170                         unhook_stale_inode(new);
1171                 }
1172         }
1173
1174         ptlrpc_req_finished(request);
1175
1176         RETURN(rc);
1177 }
1178
1179 #if 0
1180 static int llu_statfs_internal(struct llu_sb_info *sbi,
1181                                struct obd_statfs *osfs,
1182                                unsigned long max_age)
1183 {
1184         struct obd_statfs obd_osfs;
1185         int rc;
1186         ENTRY;
1187
1188         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1189         if (rc) {
1190                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1191                 RETURN(rc);
1192         }
1193
1194         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1195                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1196
1197         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1198         if (rc) {
1199                 CERROR("obd_statfs fails: rc = %d\n", rc);
1200                 RETURN(rc);
1201         }
1202
1203         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1204                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1205                obd_osfs.os_files);
1206
1207         osfs->os_blocks = obd_osfs.os_blocks;
1208         osfs->os_bfree = obd_osfs.os_bfree;
1209         osfs->os_bavail = obd_osfs.os_bavail;
1210
1211         /* If we don't have as many objects free on the OST as inodes
1212          * on the MDS, we reduce the total number of inodes to
1213          * compensate, so that the "inodes in use" number is correct.
1214          */
1215         if (obd_osfs.os_ffree < osfs->os_ffree) {
1216                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1217                         obd_osfs.os_ffree;
1218                 osfs->os_ffree = obd_osfs.os_ffree;
1219         }
1220
1221         RETURN(rc);
1222 }
1223
1224 static int llu_statfs(struct llu_sb_info *sbi, struct kstatfs *sfs)
1225 {
1226         struct obd_statfs osfs;
1227         int rc;
1228
1229         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1230
1231         /* For now we will always get up-to-date statfs values, but in the
1232          * future we may allow some amount of caching on the client (e.g.
1233          * from QOS or lprocfs updates). */
1234         rc = llu_statfs_internal(sbi, &osfs, jiffies - 1);
1235         if (rc)
1236                 return rc;
1237
1238         statfs_unpack(sfs, &osfs);
1239
1240         if (sizeof(sfs->f_blocks) == 4) {
1241                 while (osfs.os_blocks > ~0UL) {
1242                         sfs->f_bsize <<= 1;
1243
1244                         osfs.os_blocks >>= 1;
1245                         osfs.os_bfree >>= 1;
1246                         osfs.os_bavail >>= 1;
1247                 }
1248         }
1249
1250         sfs->f_blocks = osfs.os_blocks;
1251         sfs->f_bfree = osfs.os_bfree;
1252         sfs->f_bavail = osfs.os_bavail;
1253
1254         return 0;
1255 }
1256
1257 static int llu_iop_statvfs(struct pnode *pno,
1258                            struct inode *ino,
1259                            struct intnl_statvfs *buf)
1260 {
1261         struct statfs fs;
1262         int rc;
1263         ENTRY;
1264
1265 #ifndef __CYGWIN__
1266         LASSERT(pno->p_base->pb_ino);
1267         rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1268         if (rc)
1269                 RETURN(rc);
1270
1271         /* from native driver */
1272         buf->f_bsize = fs.f_bsize;  /* file system block size */
1273         buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1274         buf->f_blocks = fs.f_blocks;
1275         buf->f_bfree = fs.f_bfree;
1276         buf->f_bavail = fs.f_bavail;
1277         buf->f_files = fs.f_files;  /* Total number serial numbers */
1278         buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */
1279         buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1280         buf->f_fsid = fs.f_fsid.__val[1];
1281         buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */
1282         buf->f_namemax = fs.f_namelen;
1283 #endif
1284
1285         RETURN(0);
1286 }
1287 #endif
1288
1289 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1290 {
1291         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1292         struct qstr *qstr = &pno->p_base->pb_name;
1293         const char *name = qstr->name;
1294         int len = qstr->len;
1295         struct ptlrpc_request *request = NULL;
1296         struct llu_inode_info *lli = llu_i2info(dir);
1297         struct mdc_op_data op_data;
1298         int err = -EMLINK;
1299         ENTRY;
1300         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%lu(%p)\n",
1301                name, lli->lli_st_ino, lli->lli_st_generation, dir);
1302
1303         if (lli->lli_st_nlink >= EXT2_LINK_MAX)
1304                 RETURN(err);
1305
1306         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
1307         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1308         err = mdc_create(llu_i2sbi(dir)->ll_mdc_exp, &op_data, NULL, 0, mode,
1309                          current->fsuid, current->fsgid, 0, &request);
1310         ptlrpc_req_finished(request);
1311         RETURN(err);
1312 }
1313
1314 static int llu_iop_rmdir_raw(struct pnode *pno)
1315 {
1316         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1317         struct qstr *qstr = &pno->p_base->pb_name;
1318         const char *name = qstr->name;
1319         int len = qstr->len;
1320         struct ptlrpc_request *request = NULL;
1321         struct mdc_op_data op_data;
1322         struct llu_inode_info *lli = llu_i2info(dir);
1323         int rc;
1324         ENTRY;
1325         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%lu(%p)\n",
1326                name, lli->lli_st_ino, lli->lli_st_generation, dir);
1327
1328         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1329         rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1330         ptlrpc_req_finished(request);
1331
1332         /* libsysio: remove the pnode right away */
1333         if (!rc) {
1334                 llu_i2info(pno->p_base->pb_ino)->lli_stale_flag = 1;
1335                 unhook_stale_inode(pno);
1336         }
1337
1338         RETURN(rc);
1339 }
1340
1341 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap)
1342 {
1343         CERROR("liblustre did not support fcntl\n");
1344         return -ENOSYS;
1345 }
1346
1347 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1348                          va_list ap)
1349 {
1350         CERROR("liblustre did not support ioctl\n");
1351         return -ENOSYS;
1352 }
1353
1354 /*
1355  * we already do syncronous read/write
1356  */
1357 static int llu_iop_sync(struct inode *inode)
1358 {
1359         return 0;
1360 }
1361
1362 static int llu_iop_datasync(struct inode *inode)
1363 {
1364         return 0;
1365 }
1366
1367 struct filesys_ops llu_filesys_ops =
1368 {
1369         fsop_gone: llu_fsop_gone,
1370 };
1371
1372 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1373 {
1374         struct inode *inode;
1375         struct ll_fid fid;
1376         struct file_identifier fileid = {&fid, sizeof(fid)};
1377
1378         if ((md->body->valid &
1379              (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1380             (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE))
1381                 CERROR("invalide fields!\n");
1382
1383         /* try to find existing inode */
1384         fid.id = md->body->ino;
1385         fid.generation = md->body->generation;
1386         fid.f_type = md->body->mode & S_IFMT;
1387
1388         inode = _sysio_i_find(fs, &fileid);
1389         if (inode) {
1390                 if (llu_i2info(inode)->lli_st_generation ==
1391                     md->body->generation) {
1392                         llu_update_inode(inode, md->body, md->lsm);
1393                         return inode;
1394                 } else
1395                         I_RELE(inode);
1396         }
1397
1398         inode = llu_new_inode(fs, &fid);
1399         if (inode)
1400                 llu_update_inode(inode, md->body, md->lsm);
1401         
1402         return inode;
1403 }
1404
1405 extern struct list_head lustre_profile_list;
1406
1407 static int
1408 llu_fsswop_mount(const char *source,
1409                  unsigned flags,
1410                  const void *data __IS_UNUSED,
1411                  struct pnode *tocover,
1412                  struct mount **mntp)
1413 {
1414         struct filesys *fs;
1415         struct inode *root;
1416         struct pnode_base *rootpb;
1417         struct obd_device *obd;
1418         struct ll_fid rootfid;
1419         struct llu_sb_info *sbi;
1420         struct obd_statfs osfs;
1421         static struct qstr noname = { NULL, 0, 0 };
1422         struct ptlrpc_request *request = NULL;
1423         struct lustre_handle mdc_conn = {0, };
1424         struct lustre_handle osc_conn = {0, };
1425         struct lustre_md md;
1426         class_uuid_t uuid;
1427         struct lustre_profile *lprof;
1428         char *osc = NULL, *mdc = NULL;
1429         int err = -EINVAL;
1430
1431         ENTRY;
1432
1433         /* allocate & initialize sbi */
1434         OBD_ALLOC(sbi, sizeof(*sbi));
1435         if (!sbi)
1436                 RETURN(-ENOMEM);
1437
1438         INIT_LIST_HEAD(&sbi->ll_conn_chain);
1439         generate_random_uuid(uuid);
1440         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1441
1442         /* zeroconf */
1443         if (g_zconf) {
1444                 struct config_llog_instance cfg;
1445                 int len;
1446
1447                 if (!g_zconf_mdsname) {
1448                         CERROR("no mds name\n");
1449                         GOTO(out_free, err = -EINVAL);
1450                 }
1451
1452                 /* XXX */
1453                 /* generate a string unique to this super, let's try
1454                  the address of the super itself.*/
1455                 len = (sizeof(sbi) * 2) + 1; 
1456                 OBD_ALLOC(sbi->ll_instance, len);
1457                 if (sbi->ll_instance == NULL) 
1458                         GOTO(out_free, err = -ENOMEM);
1459                 sprintf(sbi->ll_instance, "%p", sbi);
1460
1461                 cfg.cfg_instance = sbi->ll_instance;
1462                 cfg.cfg_uuid = sbi->ll_sb_uuid;
1463                 err = liblustre_process_log(&cfg);
1464                 if (err < 0) {
1465                         CERROR("Unable to process log: %s\n", g_zconf_profile);
1466
1467                         GOTO(out_free, err);
1468                 }
1469
1470                 lprof = class_get_profile(g_zconf_profile);
1471                 if (lprof == NULL) {
1472                         CERROR("No profile found: %s\n", g_zconf_profile);
1473                         GOTO(out_free, err = -EINVAL);
1474                 }
1475                 if (osc)
1476                         OBD_FREE(osc, strlen(osc) + 1);
1477                 OBD_ALLOC(osc, strlen(lprof->lp_osc) + 
1478                           strlen(sbi->ll_instance) + 2);
1479                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
1480
1481                 if (mdc)
1482                         OBD_FREE(mdc, strlen(mdc) + 1);
1483                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) + 
1484                           strlen(sbi->ll_instance) + 2);
1485                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
1486         } else {
1487                 /* setup from dump_file */
1488                 if (list_empty(&lustre_profile_list)) {
1489                         CERROR("no profile\n");
1490                         GOTO(out_free, err = -EINVAL);
1491                 }
1492
1493                 lprof = list_entry(lustre_profile_list.next,
1494                                    struct lustre_profile, lp_list);
1495                 osc = lprof->lp_osc;
1496                 mdc = lprof->lp_mdc;
1497         }
1498
1499         if (!osc) {
1500                 CERROR("no osc\n");
1501                 GOTO(out_free, err = -EINVAL);
1502         }
1503         if (!mdc) {
1504                 CERROR("no mdc\n");
1505                 GOTO(out_free, err = -EINVAL);
1506         }
1507
1508         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1509         if (!fs) {
1510                 err = -ENOMEM;
1511                 goto out_free;
1512         }
1513
1514         obd = class_name2obd(mdc);
1515         if (!obd) {
1516                 CERROR("MDC %s: not setup or attached\n", mdc);
1517                 GOTO(out_free, err = -EINVAL);
1518         }
1519
1520         if (mdc_init_ea_size(obd, osc))
1521                 GOTO(out_free, err = -EINVAL);
1522
1523         /* setup mdc */
1524         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
1525         if (err) {
1526                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1527                 GOTO(out_free, err);
1528         }
1529         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
1530
1531         err = obd_statfs(obd, &osfs, 100000000);
1532         if (err)
1533                 GOTO(out_mdc, err);
1534
1535         /*
1536          * FIXME fill fs stat data into sbi here!!! FIXME
1537          */
1538
1539         /* setup osc */
1540         obd = class_name2obd(osc);
1541         if (!obd) {
1542                 CERROR("OSC %s: not setup or attached\n", osc);
1543                 GOTO(out_mdc, err = -EINVAL);
1544         }
1545
1546         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
1547         if (err) {
1548                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1549                 GOTO(out_mdc, err);
1550         }
1551         sbi->ll_osc_exp = class_conn2export(&osc_conn);
1552
1553         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
1554         if (err) {
1555                 CERROR("cannot mds_connect: rc = %d\n", err);
1556                 GOTO(out_osc, err);
1557         }
1558         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
1559         sbi->ll_rootino = rootfid.id;
1560
1561         /* fetch attr of root inode */
1562         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
1563                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
1564         if (err) {
1565                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
1566                 GOTO(out_osc, err);
1567         }
1568
1569         err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
1570         if (err) {
1571                 CERROR("failed to understand root inode md: rc = %d\n",err);
1572                 GOTO(out_request, err);
1573         }
1574
1575         LASSERT(sbi->ll_rootino != 0);
1576
1577         root = llu_iget(fs, &md);
1578         if (root == NULL) {
1579                 CERROR("fail to generate root inode\n");
1580                 GOTO(out_request, err = -EBADF);
1581         }
1582
1583         /*
1584          * Generate base path-node for root.
1585          */
1586         rootpb = _sysio_pb_new(&noname, NULL, root);
1587         if (!rootpb) {
1588                 err = -ENOMEM;
1589                 goto out_inode;
1590         }
1591
1592         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
1593         if (err) {
1594                 _sysio_pb_gone(rootpb);
1595                 goto out_inode;
1596         }
1597
1598         ptlrpc_req_finished(request);
1599
1600         printf("LibLustre: namespace mounted successfully!\n");
1601
1602         return 0;
1603
1604 out_inode:
1605         _sysio_i_gone(root);
1606 out_request:
1607         ptlrpc_req_finished(request);
1608 out_osc:
1609         obd_disconnect(sbi->ll_osc_exp, 0);
1610 out_mdc:
1611         obd_disconnect(sbi->ll_mdc_exp, 0);
1612 out_free:
1613         OBD_FREE(sbi, sizeof(*sbi));
1614         return err;
1615 }
1616
1617 struct fssw_ops llu_fssw_ops = {
1618         llu_fsswop_mount
1619 };
1620
1621 static struct inode_ops llu_inode_ops = {
1622         inop_lookup:    llu_iop_lookup,
1623         inop_getattr:   llu_iop_getattr,
1624         inop_setattr:   llu_iop_setattr,
1625         inop_getdirentries:     NULL,
1626         inop_mkdir:     llu_iop_mkdir_raw,
1627         inop_rmdir:     llu_iop_rmdir_raw,
1628         inop_symlink:   llu_iop_symlink_raw,
1629         inop_readlink:  llu_iop_readlink,
1630         inop_open:      llu_iop_open,
1631         inop_close:     llu_iop_close,
1632         inop_link:      llu_iop_link_raw,
1633         inop_unlink:    llu_iop_unlink_raw,
1634         inop_rename:    llu_iop_rename_raw,
1635         inop_ipreadv:   llu_iop_ipreadv,
1636         inop_ipwritev:  llu_iop_ipwritev,
1637         inop_iodone:    llu_iop_iodone,
1638         inop_fcntl:     llu_iop_fcntl,
1639         inop_sync:      llu_iop_sync,
1640         inop_datasync:  llu_iop_datasync,
1641         inop_ioctl:     llu_iop_ioctl,
1642         inop_mknod:     llu_iop_mknod_raw,
1643 #if 0
1644         inop_statvfs:   llu_iop_statvfs,
1645 #endif
1646         inop_gone:      llu_iop_gone,
1647 };
1648