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