Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[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, __u64 lockpart)
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         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
348         int flags;
349         ENTRY;
350
351         LASSERT(inode);
352
353         obddev = sbi->ll_mdc_exp->exp_obd;
354         res_id.name[0] = lli->lli_st_ino;
355         res_id.name[1] = lli->lli_st_generation;
356
357         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
358
359         /* FIXME use LDLM_FL_TEST_LOCK instead */
360         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
361         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
362                             &policy, LCK_PR, &lockh)) {
363                 ldlm_lock_decref(&lockh, LCK_PR);
364                 RETURN(1);
365         }
366
367         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
368                             &policy, LCK_PW, &lockh)) {
369                 ldlm_lock_decref(&lockh, LCK_PW);
370                 RETURN(1);
371         }
372         RETURN(0);
373 }
374
375 static int llu_inode_revalidate(struct inode *inode)
376 {
377         struct llu_inode_info *lli = llu_i2info(inode);
378         struct lov_stripe_md *lsm = NULL;
379         ENTRY;
380
381         if (!inode) {
382                 CERROR("REPORT THIS LINE TO PETER\n");
383                 RETURN(0);
384         }
385
386         if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
387                 struct lustre_md md;
388                 struct ptlrpc_request *req = NULL;
389                 struct llu_sb_info *sbi = llu_i2sbi(inode);
390                 struct ll_fid fid;
391                 unsigned long valid = 0;
392                 int rc, ealen = 0;
393
394                 /* Why don't we update all valid MDS fields here, if we're
395                  * doing an RPC anyways?  -phil */
396                 if (S_ISREG(lli->lli_st_mode)) {
397                         ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
398                         valid |= OBD_MD_FLEASIZE;
399                 }
400                 ll_inode2fid(&fid, inode);
401                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
402                 if (rc) {
403                         CERROR("failure %d inode %lu\n", rc, lli->lli_st_ino);
404                         RETURN(-abs(rc));
405                 }
406                 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, req, 0, 
407                                        sbi->ll_osc_exp, &md);
408
409                 /* XXX Too paranoid? */
410                 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
411                     !((md.body->valid & OBD_MD_FLNLINK) &&
412                       (md.body->nlink == 0))) {
413                         CERROR("Asked for %s eadata but got %s (%d)\n",
414                                (valid & OBD_MD_FLEASIZE) ? "some" : "no",
415                                (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
416                                 md.body->eadatasize);
417                 }
418                 if (rc) {
419                         ptlrpc_req_finished(req);
420                         RETURN(rc);
421                 }
422
423
424                 llu_update_inode(inode, md.body, md.lsm);
425                 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
426                         obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
427
428                 if (md.body->valid & OBD_MD_FLSIZE)
429                         set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
430                                 &llu_i2info(inode)->lli_flags);
431                 ptlrpc_req_finished(req);
432         }
433
434         lsm = llu_i2info(inode)->lli_smd;
435         if (!lsm)       /* object not yet allocated, don't validate size */
436                 RETURN(0);
437
438         /* ll_glimpse_size will prefer locally cached writes if they extend
439          * the file */
440         {
441                 struct ost_lvb lvb;
442                 ldlm_error_t err;
443
444                 err = llu_glimpse_size(inode, &lvb);
445                 lli->lli_st_size = lvb.lvb_size;
446         }
447         RETURN(0);
448 }
449
450 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
451 {
452         struct llu_inode_info *lli = llu_i2info(ino);
453
454         b->st_dev = lli->lli_st_dev;
455         b->st_ino = lli->lli_st_ino;
456         b->st_mode = lli->lli_st_mode;
457         b->st_nlink = lli->lli_st_nlink;
458         b->st_uid = lli->lli_st_uid;
459         b->st_gid = lli->lli_st_gid;
460         b->st_rdev = lli->lli_st_rdev;
461         b->st_size = lli->lli_st_size;
462         b->st_blksize = lli->lli_st_blksize;
463         b->st_blocks = lli->lli_st_blocks;
464         b->st_atime = lli->lli_st_atime;
465         b->st_mtime = lli->lli_st_mtime;
466         b->st_ctime = lli->lli_st_ctime;
467 }
468
469 static int llu_iop_getattr(struct pnode *pno,
470                            struct inode *ino,
471                            struct intnl_stat *b)
472 {
473         int rc;
474         ENTRY;
475
476         if (!ino) {
477                 LASSERT(pno);
478                 LASSERT(pno->p_base->pb_ino);
479                 ino = pno->p_base->pb_ino;
480         } else {
481                 LASSERT(!pno || pno->p_base->pb_ino == ino);
482         }
483
484         /* libsysio might call us directly without intent lock,
485          * we must re-fetch the attrs here
486          */
487         rc = llu_inode_revalidate(ino);
488         if (!rc) {
489                 copy_stat_buf(ino, b);
490                 LASSERT(!llu_i2info(ino)->lli_it);
491         }
492
493         RETURN(rc);
494 }
495
496 static int null_if_equal(struct ldlm_lock *lock, void *data)
497 {
498         if (data == lock->l_ast_data)
499                 lock->l_ast_data = NULL;
500
501         if (lock->l_req_mode != lock->l_granted_mode)
502                 return LDLM_ITER_STOP;
503
504         return LDLM_ITER_CONTINUE;
505 }
506
507 void llu_clear_inode(struct inode *inode)
508 {
509         struct ll_fid fid;
510         struct llu_inode_info *lli = llu_i2info(inode);
511         struct llu_sb_info *sbi = llu_i2sbi(inode);
512         ENTRY;
513
514         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu(%p)\n", lli->lli_st_ino,
515                lli->lli_st_generation, inode);
516
517         ll_inode2fid(&fid, inode);
518         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
519         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
520
521         if (lli->lli_smd)
522                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
523                                   null_if_equal, inode);
524
525         if (lli->lli_smd) {
526                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
527                 lli->lli_smd = NULL;
528         }
529
530         if (lli->lli_symlink_name) {
531                 OBD_FREE(lli->lli_symlink_name,
532                          strlen(lli->lli_symlink_name) + 1);
533                 lli->lli_symlink_name = NULL;
534         }
535
536         EXIT;
537 }
538
539 void llu_iop_gone(struct inode *inode)
540 {
541         struct llu_inode_info *lli = llu_i2info(inode);
542         ENTRY;
543
544         llu_clear_inode(inode);
545
546         OBD_FREE(lli, sizeof(*lli));
547         EXIT;
548 }
549
550 static int inode_setattr(struct inode * inode, struct iattr * attr)
551 {
552         unsigned int ia_valid = attr->ia_valid;
553         struct llu_inode_info *lli = llu_i2info(inode);
554         int error = 0;
555
556         if (ia_valid & ATTR_SIZE) {
557                 error = llu_vmtruncate(inode, attr->ia_size);
558                 if (error)
559                         goto out;
560         }
561
562         if (ia_valid & ATTR_UID)
563                 lli->lli_st_uid = attr->ia_uid;
564         if (ia_valid & ATTR_GID)
565                 lli->lli_st_gid = attr->ia_gid;
566         if (ia_valid & ATTR_ATIME)
567                 lli->lli_st_atime = attr->ia_atime;
568         if (ia_valid & ATTR_MTIME)
569                 lli->lli_st_mtime = attr->ia_mtime;
570         if (ia_valid & ATTR_CTIME)
571                 lli->lli_st_ctime = attr->ia_ctime;
572         if (ia_valid & ATTR_MODE) {
573                 lli->lli_st_mode = attr->ia_mode;
574                 if (!in_group_p(lli->lli_st_gid) && !capable(CAP_FSETID))
575                         lli->lli_st_mode &= ~S_ISGID;
576         }
577         /* mark_inode_dirty(inode); */
578 out:
579         return error;
580 }
581
582 /* If this inode has objects allocated to it (lsm != NULL), then the OST
583  * object(s) determine the file size and mtime.  Otherwise, the MDS will
584  * keep these values until such a time that objects are allocated for it.
585  * We do the MDS operations first, as it is checking permissions for us.
586  * We don't to the MDS RPC if there is nothing that we want to store there,
587  * otherwise there is no harm in updating mtime/atime on the MDS if we are
588  * going to do an RPC anyways.
589  *
590  * If we are doing a truncate, we will send the mtime and ctime updates
591  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
592  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
593  * at the same time.
594  */
595 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
596 {
597         struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
598         struct llu_sb_info *sbi = llu_i2sbi(inode);
599         struct llu_inode_info *lli = llu_i2info(inode);
600         struct ptlrpc_request *request = NULL;
601         struct mdc_op_data op_data;
602         int ia_valid = attr->ia_valid;
603         int rc = 0;
604         ENTRY;
605
606         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
607
608         if (ia_valid & ATTR_SIZE) {
609                 if (attr->ia_size > ll_file_maxbytes(inode)) {
610                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
611                                attr->ia_size, ll_file_maxbytes(inode));
612                         RETURN(-EFBIG);
613                 }
614
615                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
616         }
617
618         /* We mark all of the fields "set" so MDS/OST does not re-set them */
619         if (attr->ia_valid & ATTR_CTIME) {
620                 attr->ia_ctime = CURRENT_TIME;
621                 attr->ia_valid |= ATTR_CTIME_SET;
622         }
623         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
624                 attr->ia_atime = CURRENT_TIME;
625                 attr->ia_valid |= ATTR_ATIME_SET;
626         }
627         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
628                 attr->ia_mtime = CURRENT_TIME;
629                 attr->ia_valid |= ATTR_MTIME_SET;
630         }
631
632         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
633                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
634                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
635                        LTIME_S(CURRENT_TIME));
636         if (lsm)
637                 attr->ia_valid &= ~ATTR_SIZE;
638
639         /* If only OST attributes being set on objects, don't do MDS RPC.
640          * In that case, we need to check permissions and update the local
641          * inode ourselves so we can call obdo_from_inode() always. */
642         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
643                 struct lustre_md md;
644                 llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
645
646                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
647                                   attr, NULL, 0, NULL, 0, &request);
648
649                 if (rc) {
650                         ptlrpc_req_finished(request);
651                         if (rc != -EPERM && rc != -EACCES)
652                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
653                         RETURN(rc);
654                 }
655
656                 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
657                                        sbi->ll_osc_exp, &md);
658                 if (rc) {
659                         ptlrpc_req_finished(request);
660                         RETURN(rc);
661                 }
662                 llu_update_inode(inode, md.body, md.lsm);
663                 ptlrpc_req_finished(request);
664
665                 if (!md.lsm || !S_ISREG(lli->lli_st_mode)) {
666                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
667                         RETURN(0);
668                 }
669         } else {
670                 /* The OST doesn't check permissions, but the alternative is
671                  * a gratuitous RPC to the MDS.  We already rely on the client
672                  * to do read/write/truncate permission checks, so is mtime OK?
673                  */
674                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
675                         /* from sys_utime() */
676                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
677                                 if (current->fsuid != lli->lli_st_uid &&
678                                     (rc = ll_permission(inode, 0/*MAY_WRITE*/, NULL)) != 0)
679                                         RETURN(rc);
680                         } else {
681                                 /* from inode_change_ok() */
682                                 if (current->fsuid != lli->lli_st_uid &&
683                                     !capable(CAP_FOWNER))
684                                         RETURN(-EPERM);
685                         }
686                 }
687
688                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
689                 inode_setattr(inode, attr);
690         }
691
692         if (ia_valid & ATTR_SIZE) {
693                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
694                                                            OBD_OBJECT_EOF} };
695                 struct lustre_handle lockh = { 0 };
696                 int err, ast_flags = 0;
697                 /* XXX when we fix the AST intents to pass the discard-range
698                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
699                  * XXX here. */
700                 if (attr->ia_size == 0)
701                         ast_flags = LDLM_AST_DISCARD_DATA;
702
703                 rc = llu_extent_lock(NULL, inode, lsm, LCK_PW, &policy,
704                                      &lockh, ast_flags);
705                 if (rc != ELDLM_OK) {
706                         if (rc > 0)
707                                 RETURN(-ENOLCK);
708                         RETURN(rc);
709                 }
710
711                 rc = llu_vmtruncate(inode, attr->ia_size);
712                 if (rc == 0)
713                         set_bit(LLI_F_HAVE_OST_SIZE_LOCK,
714                                 &llu_i2info(inode)->lli_flags);
715
716                 /* unlock now as we don't mind others file lockers racing with
717                  * the mds updates below? */
718                 err = llu_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
719                 if (err) {
720                         CERROR("llu_extent_unlock failed: %d\n", err);
721                         if (!rc)
722                                 rc = err;
723                 }
724         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
725                 struct obdo oa;
726
727                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
728                        lli->lli_st_ino, LTIME_S(attr->ia_mtime));
729                 oa.o_id = lsm->lsm_object_id;
730                 oa.o_valid = OBD_MD_FLID;
731                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
732                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
733                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
734                 if (rc)
735                         CERROR("obd_setattr fails: rc=%d\n", rc);
736         }
737         RETURN(rc);
738 }
739
740 /* here we simply act as a thin layer to glue it with
741  * llu_setattr_raw(), which is copy from kernel
742  */
743 static int llu_iop_setattr(struct pnode *pno,
744                            struct inode *ino,
745                            unsigned mask,
746                            struct intnl_stat *stbuf)
747 {
748         struct iattr iattr;
749         ENTRY;
750
751         memset(&iattr, 0, sizeof(iattr));
752
753         if (mask & SETATTR_MODE) {
754                 iattr.ia_mode = stbuf->st_mode;
755                 iattr.ia_valid |= ATTR_MODE;
756         }
757         if (mask & SETATTR_MTIME) {
758                 iattr.ia_mtime = stbuf->st_mtime;
759                 iattr.ia_valid |= ATTR_MTIME;
760         }
761         if (mask & SETATTR_ATIME) {
762                 iattr.ia_atime = stbuf->st_atime;
763                 iattr.ia_valid |= ATTR_ATIME;
764         }
765         if (mask & SETATTR_UID) {
766                 iattr.ia_uid = stbuf->st_uid;
767                 iattr.ia_valid |= ATTR_UID;
768         }
769         if (mask & SETATTR_GID) {
770                 iattr.ia_gid = stbuf->st_gid;
771                 iattr.ia_valid |= ATTR_GID;
772         }
773         if (mask & SETATTR_LEN) {
774                 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
775                 iattr.ia_valid |= ATTR_SIZE;
776         }
777
778         iattr.ia_valid |= ATTR_RAW;
779
780         RETURN(llu_setattr_raw(ino, &iattr));
781 }
782
783 #define EXT2_LINK_MAX           32000
784
785 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
786 {
787         struct inode *dir = pno->p_base->pb_parent->pb_ino;
788         struct qstr *qstr = &pno->p_base->pb_name;
789         const char *name = qstr->name;
790         int len = qstr->len;
791         struct ptlrpc_request *request = NULL;
792         struct llu_sb_info *sbi = llu_i2sbi(dir);
793         struct mdc_op_data op_data;
794         int err = -EMLINK;
795         ENTRY;
796
797         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
798                 RETURN(err);
799
800         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
801         err = mdc_create(sbi->ll_mdc_exp, &op_data,
802                          tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
803                          current->fsuid, current->fsgid, 0, &request);
804         ptlrpc_req_finished(request);
805         RETURN(err);
806 }
807
808 static int llu_readlink_internal(struct inode *inode,
809                                  struct ptlrpc_request **request,
810                                  char **symname)
811 {
812         struct llu_inode_info *lli = llu_i2info(inode);
813         struct llu_sb_info *sbi = llu_i2sbi(inode);
814         struct ll_fid fid;
815         struct mds_body *body;
816         int rc, symlen = lli->lli_st_size + 1;
817         ENTRY;
818
819         *request = NULL;
820
821         if (lli->lli_symlink_name) {
822                 *symname = lli->lli_symlink_name;
823                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
824                 RETURN(0);
825         }
826
827         ll_inode2fid(&fid, inode);
828         rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
829                          OBD_MD_LINKNAME, symlen, request);
830         if (rc) {
831                 CERROR("inode %lu: rc = %d\n", lli->lli_st_ino, rc);
832                 RETURN(rc);
833         }
834
835         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
836         LASSERT (body != NULL);
837         LASSERT_REPSWABBED (*request, 0);
838
839         if ((body->valid & OBD_MD_LINKNAME) == 0) {
840                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
841                 GOTO (failed, rc = -EPROTO);
842         }
843         
844         LASSERT (symlen != 0);
845         if (body->eadatasize != symlen) {
846                 CERROR ("inode %lu: symlink length %d not expected %d\n",
847                         lli->lli_st_ino, body->eadatasize - 1, symlen - 1);
848                 GOTO (failed, rc = -EPROTO);
849         }
850
851         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
852         if (*symname == NULL ||
853             strnlen (*symname, symlen) != symlen - 1) {
854                 /* not full/NULL terminated */
855                 CERROR ("inode %lu: symlink not NULL terminated string"
856                         "of length %d\n", lli->lli_st_ino, symlen - 1);
857                 GOTO (failed, rc = -EPROTO);
858         }
859
860         OBD_ALLOC(lli->lli_symlink_name, symlen);
861         /* do not return an error if we cannot cache the symlink locally */
862         if (lli->lli_symlink_name)
863                 memcpy(lli->lli_symlink_name, *symname, symlen);
864
865         RETURN(0);
866
867  failed:
868         ptlrpc_req_finished (*request);
869         RETURN (-EPROTO);
870 }
871
872 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
873 {
874         struct inode *inode = pno->p_base->pb_ino;
875         struct ptlrpc_request *request;
876         char *symname;
877         int rc;
878         ENTRY;
879
880         rc = llu_readlink_internal(inode, &request, &symname);
881         if (rc)
882                 GOTO(out, rc);
883
884         LASSERT(symname);
885         strncpy(data, symname, bufsize);
886
887         ptlrpc_req_finished(request);
888  out:
889         RETURN(rc);
890 }
891
892 static int llu_iop_mknod_raw(struct pnode *pno,
893                              mode_t mode,
894                              dev_t dev)
895 {
896         struct ptlrpc_request *request = NULL;
897         struct inode *dir = pno->p_parent->p_base->pb_ino;
898         struct llu_sb_info *sbi = llu_i2sbi(dir);
899         struct mdc_op_data op_data;
900         int err = -EMLINK;
901         ENTRY;
902
903         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu\n",
904                pno->p_base->pb_name.name, llu_i2info(dir)->lli_st_ino);
905
906         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
907                 RETURN(err);
908
909         mode &= ~current->fs->umask;
910
911         switch (mode & S_IFMT) {
912         case 0:
913         case S_IFREG:
914                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
915         case S_IFCHR:
916         case S_IFBLK:
917         case S_IFIFO:
918         case S_IFSOCK:
919                 llu_prepare_mdc_op_data(&op_data, dir, NULL,
920                                         pno->p_base->pb_name.name,
921                                         pno->p_base->pb_name.len,
922                                         0);
923                 err = mdc_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
924                                  current->fsuid, current->fsgid, dev, &request);
925                 ptlrpc_req_finished(request);
926                 break;
927         case S_IFDIR:
928                 err = -EPERM;
929                 break;
930         default:
931                 err = -EINVAL;
932         }
933         RETURN(err);
934 }
935
936 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
937 {
938         struct inode *src = old->p_base->pb_ino;
939         struct inode *dir = new->p_parent->p_base->pb_ino;
940         const char *name = new->p_base->pb_name.name;
941         int namelen = new->p_base->pb_name.len;
942         struct ptlrpc_request *request = NULL;
943         struct mdc_op_data op_data;
944         int rc;
945         ENTRY;
946
947         LASSERT(src);
948         LASSERT(dir);
949
950         llu_prepare_mdc_op_data(&op_data, src, dir, name, namelen, 0);
951         rc = mdc_link(llu_i2sbi(src)->ll_mdc_exp, &op_data, &request);
952         ptlrpc_req_finished(request);
953
954         RETURN(rc);
955 }
956
957 static int llu_iop_unlink_raw(struct pnode *pno)
958 {
959         struct inode *dir = pno->p_base->pb_parent->pb_ino;
960         struct qstr *qstr = &pno->p_base->pb_name;
961         const char *name = qstr->name;
962         int len = qstr->len;
963         struct inode *target = pno->p_base->pb_ino;
964         struct ptlrpc_request *request = NULL;
965         struct mdc_op_data op_data;
966         int rc;
967         ENTRY;
968
969         LASSERT(target);
970
971         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
972         rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
973         if (!rc) {
974                 rc = llu_objects_destroy(request, dir);
975
976                 llu_i2info(target)->lli_stale_flag = 1;
977                 unhook_stale_inode(pno);
978         }
979
980         ptlrpc_req_finished(request);
981         RETURN(rc);
982 }
983
984 /* FIXME
985  * following cases need to be considered later:
986  * - rename an opened file/dir
987  * - an opened file be removed in rename
988  * - rename to remove and hardlink (?opened)
989  */
990 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
991 {
992         struct inode *src = old->p_parent->p_base->pb_ino;
993         struct inode *tgt = new->p_parent->p_base->pb_ino;
994         struct inode *tgtinode = new->p_base->pb_ino;
995         const char *oldname = old->p_base->pb_name.name;
996         int oldnamelen = old->p_base->pb_name.len;
997         const char *newname = new->p_base->pb_name.name;
998         int newnamelen = new->p_base->pb_name.len;
999         struct ptlrpc_request *request = NULL;
1000         struct mdc_op_data op_data;
1001         int rc;
1002         ENTRY;
1003
1004         LASSERT(src);
1005         LASSERT(tgt);
1006
1007         llu_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
1008         rc = mdc_rename(llu_i2sbi(src)->ll_mdc_exp, &op_data,
1009                         oldname, oldnamelen, newname, newnamelen,
1010                         &request);
1011         if (!rc) {
1012                 rc = llu_objects_destroy(request, src);
1013
1014                 if (tgtinode) {
1015                         llu_i2info(tgtinode)->lli_stale_flag = 1;
1016                         unhook_stale_inode(new);
1017                 }
1018         }
1019
1020         ptlrpc_req_finished(request);
1021
1022         RETURN(rc);
1023 }
1024
1025 #ifdef _HAVE_STATVFS
1026 static int llu_statfs_internal(struct llu_sb_info *sbi,
1027                                struct obd_statfs *osfs,
1028                                unsigned long max_age)
1029 {
1030         struct obd_statfs obd_osfs;
1031         int rc;
1032         ENTRY;
1033
1034         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1035         if (rc) {
1036                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1037                 RETURN(rc);
1038         }
1039
1040         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1041                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1042
1043         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1044         if (rc) {
1045                 CERROR("obd_statfs fails: rc = %d\n", rc);
1046                 RETURN(rc);
1047         }
1048
1049         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1050                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1051                obd_osfs.os_files);
1052
1053         osfs->os_blocks = obd_osfs.os_blocks;
1054         osfs->os_bfree = obd_osfs.os_bfree;
1055         osfs->os_bavail = obd_osfs.os_bavail;
1056
1057         /* If we don't have as many objects free on the OST as inodes
1058          * on the MDS, we reduce the total number of inodes to
1059          * compensate, so that the "inodes in use" number is correct.
1060          */
1061         if (obd_osfs.os_ffree < osfs->os_ffree) {
1062                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1063                         obd_osfs.os_ffree;
1064                 osfs->os_ffree = obd_osfs.os_ffree;
1065         }
1066
1067         RETURN(rc);
1068 }
1069
1070 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1071 {
1072         struct obd_statfs osfs;
1073         int rc;
1074
1075         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1076
1077         /* For now we will always get up-to-date statfs values, but in the
1078          * future we may allow some amount of caching on the client (e.g.
1079          * from QOS or lprocfs updates). */
1080         rc = llu_statfs_internal(sbi, &osfs, jiffies - 1);
1081         if (rc)
1082                 return rc;
1083
1084         statfs_unpack(sfs, &osfs);
1085
1086         if (sizeof(sfs->f_blocks) == 4) {
1087                 while (osfs.os_blocks > ~0UL) {
1088                         sfs->f_bsize <<= 1;
1089
1090                         osfs.os_blocks >>= 1;
1091                         osfs.os_bfree >>= 1;
1092                         osfs.os_bavail >>= 1;
1093                 }
1094         }
1095
1096         sfs->f_blocks = osfs.os_blocks;
1097         sfs->f_bfree = osfs.os_bfree;
1098         sfs->f_bavail = osfs.os_bavail;
1099
1100         return 0;
1101 }
1102
1103 static int llu_iop_statvfs(struct pnode *pno,
1104                            struct inode *ino,
1105                            struct intnl_statvfs *buf)
1106 {
1107         struct statfs fs;
1108         int rc;
1109         ENTRY;
1110
1111 #ifndef __CYGWIN__
1112         LASSERT(pno->p_base->pb_ino);
1113         rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1114         if (rc)
1115                 RETURN(rc);
1116
1117         /* from native driver */
1118         buf->f_bsize = fs.f_bsize;  /* file system block size */
1119         buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1120         buf->f_blocks = fs.f_blocks;
1121         buf->f_bfree = fs.f_bfree;
1122         buf->f_bavail = fs.f_bavail;
1123         buf->f_files = fs.f_files;  /* Total number serial numbers */
1124         buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */
1125         buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1126         buf->f_fsid = fs.f_fsid.__val[1];
1127         buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */
1128         buf->f_namemax = fs.f_namelen;
1129 #endif
1130
1131         RETURN(0);
1132 }
1133 #endif /* _HAVE_STATVFS */
1134
1135 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1136 {
1137         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1138         struct qstr *qstr = &pno->p_base->pb_name;
1139         const char *name = qstr->name;
1140         int len = qstr->len;
1141         struct ptlrpc_request *request = NULL;
1142         struct llu_inode_info *lli = llu_i2info(dir);
1143         struct mdc_op_data op_data;
1144         int err = -EMLINK;
1145         ENTRY;
1146         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%lu(%p)\n",
1147                name, lli->lli_st_ino, lli->lli_st_generation, dir);
1148
1149         if (lli->lli_st_nlink >= EXT2_LINK_MAX)
1150                 RETURN(err);
1151
1152         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
1153         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1154         err = mdc_create(llu_i2sbi(dir)->ll_mdc_exp, &op_data, NULL, 0, mode,
1155                          current->fsuid, current->fsgid, 0, &request);
1156         ptlrpc_req_finished(request);
1157         RETURN(err);
1158 }
1159
1160 static int llu_iop_rmdir_raw(struct pnode *pno)
1161 {
1162         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1163         struct qstr *qstr = &pno->p_base->pb_name;
1164         const char *name = qstr->name;
1165         int len = qstr->len;
1166         struct ptlrpc_request *request = NULL;
1167         struct mdc_op_data op_data;
1168         struct llu_inode_info *lli = llu_i2info(dir);
1169         int rc;
1170         ENTRY;
1171         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%lu(%p)\n",
1172                name, lli->lli_st_ino, lli->lli_st_generation, dir);
1173
1174         llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1175         rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1176         ptlrpc_req_finished(request);
1177
1178         /* libsysio: remove the pnode right away */
1179         if (!rc) {
1180                 llu_i2info(pno->p_base->pb_ino)->lli_stale_flag = 1;
1181                 unhook_stale_inode(pno);
1182         }
1183
1184         RETURN(rc);
1185 }
1186
1187 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap)
1188 {
1189         CERROR("liblustre did not support fcntl\n");
1190         return -ENOSYS;
1191 }
1192
1193 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1194 {
1195         struct llu_inode_info *lli = llu_i2info(inode);
1196         struct ll_file_data *fd = lli->lli_file_data;
1197         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1198                                                     .end = OBD_OBJECT_EOF}};
1199         struct lustre_handle lockh = { 0 };
1200         struct lov_stripe_md *lsm = lli->lli_smd;
1201         ldlm_error_t err;
1202         int flags = 0;
1203         ENTRY;
1204
1205         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1206                 RETURN(-EINVAL);
1207         }
1208
1209         policy.l_extent.gid = arg;
1210         if (lli->lli_open_flags & O_NONBLOCK)
1211                 flags = LDLM_FL_BLOCK_NOWAIT;
1212
1213         err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1214                               flags);
1215         if (err)
1216                 RETURN(err);
1217
1218         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1219         fd->fd_gid = arg;
1220         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1221
1222         RETURN(0);
1223 }
1224
1225 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1226 {
1227         struct llu_inode_info *lli = llu_i2info(inode);
1228         struct ll_file_data *fd = lli->lli_file_data;
1229         struct lov_stripe_md *lsm = lli->lli_smd;
1230         ldlm_error_t err;
1231         ENTRY;
1232
1233         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1234                 RETURN(-EINVAL);
1235
1236         if (fd->fd_gid != arg)
1237                 RETURN(-EINVAL);
1238
1239         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1240
1241         err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1242         if (err)
1243                 RETURN(err);
1244
1245         fd->fd_gid = 0;
1246         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1247
1248         RETURN(0);
1249 }       
1250
1251 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1252                          va_list ap)
1253 {
1254         unsigned long arg;
1255
1256         switch (request) {
1257         case LL_IOC_GROUP_LOCK:
1258                 arg = va_arg(ap, unsigned long);
1259                 return llu_get_grouplock(ino, arg);
1260         case LL_IOC_GROUP_UNLOCK:
1261                 arg = va_arg(ap, unsigned long);
1262                 return llu_put_grouplock(ino, arg);
1263         }
1264
1265         CERROR("did not support ioctl cmd %lx\n", request);
1266         return -ENOSYS;
1267 }
1268
1269 /*
1270  * we already do syncronous read/write
1271  */
1272 static int llu_iop_sync(struct inode *inode)
1273 {
1274         return 0;
1275 }
1276
1277 static int llu_iop_datasync(struct inode *inode)
1278 {
1279         return 0;
1280 }
1281
1282 struct filesys_ops llu_filesys_ops =
1283 {
1284         fsop_gone: llu_fsop_gone,
1285 };
1286
1287 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1288 {
1289         struct inode *inode;
1290         struct ll_fid fid;
1291         struct file_identifier fileid = {&fid, sizeof(fid)};
1292
1293         if ((md->body->valid &
1294              (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1295             (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1296                 /* FIXME this is workaround for for open(O_CREAT),
1297                  * see lookup_it_finish(). */
1298                 return ERR_PTR(-EPERM);
1299         }
1300
1301         /* try to find existing inode */
1302         fid.id = md->body->ino;
1303         fid.generation = md->body->generation;
1304         fid.f_type = md->body->mode & S_IFMT;
1305
1306         inode = _sysio_i_find(fs, &fileid);
1307         if (inode) {
1308                 struct llu_inode_info *lli = llu_i2info(inode);
1309
1310                 if (lli->lli_stale_flag ||
1311                     lli->lli_st_generation != md->body->generation)
1312                         I_RELE(inode);
1313                 else {
1314                         llu_update_inode(inode, md->body, md->lsm);
1315                         return inode;
1316                 }
1317         }
1318
1319         inode = llu_new_inode(fs, &fid);
1320         if (inode)
1321                 llu_update_inode(inode, md->body, md->lsm);
1322         
1323         return inode;
1324 }
1325
1326 extern struct list_head lustre_profile_list;
1327
1328 static int
1329 llu_fsswop_mount(const char *source,
1330                  unsigned flags,
1331                  const void *data __IS_UNUSED,
1332                  struct pnode *tocover,
1333                  struct mount **mntp)
1334 {
1335         struct filesys *fs;
1336         struct inode *root;
1337         struct pnode_base *rootpb;
1338         struct obd_device *obd;
1339         struct ll_fid rootfid;
1340         struct llu_sb_info *sbi;
1341         struct obd_statfs osfs;
1342         static struct qstr noname = { NULL, 0, 0 };
1343         struct ptlrpc_request *request = NULL;
1344         struct lustre_handle mdc_conn = {0, };
1345         struct lustre_handle osc_conn = {0, };
1346         struct lustre_md md;
1347         class_uuid_t uuid;
1348         struct lustre_profile *lprof;
1349         char *osc = NULL, *mdc = NULL;
1350         int err = -EINVAL;
1351
1352         ENTRY;
1353
1354         /* allocate & initialize sbi */
1355         OBD_ALLOC(sbi, sizeof(*sbi));
1356         if (!sbi)
1357                 RETURN(-ENOMEM);
1358
1359         INIT_LIST_HEAD(&sbi->ll_conn_chain);
1360         generate_random_uuid(uuid);
1361         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1362
1363         /* zeroconf */
1364         if (g_zconf) {
1365                 struct config_llog_instance cfg;
1366                 int len;
1367
1368                 if (!g_zconf_mdsname) {
1369                         CERROR("no mds name\n");
1370                         GOTO(out_free, err = -EINVAL);
1371                 }
1372
1373                 /* generate a string unique to this super, let's try
1374                  the address of the super itself.*/
1375                 len = (sizeof(sbi) * 2) + 1; 
1376                 OBD_ALLOC(sbi->ll_instance, len);
1377                 if (sbi->ll_instance == NULL) 
1378                         GOTO(out_free, err = -ENOMEM);
1379                 sprintf(sbi->ll_instance, "%p", sbi);
1380
1381                 cfg.cfg_instance = sbi->ll_instance;
1382                 cfg.cfg_uuid = sbi->ll_sb_uuid;
1383                 err = liblustre_process_log(&cfg, 1);
1384                 if (err < 0) {
1385                         CERROR("Unable to process log: %s\n", g_zconf_profile);
1386
1387                         GOTO(out_free, err);
1388                 }
1389
1390                 lprof = class_get_profile(g_zconf_profile);
1391                 if (lprof == NULL) {
1392                         CERROR("No profile found: %s\n", g_zconf_profile);
1393                         GOTO(out_free, err = -EINVAL);
1394                 }
1395                 if (osc)
1396                         OBD_FREE(osc, strlen(osc) + 1);
1397                 OBD_ALLOC(osc, strlen(lprof->lp_osc) + 
1398                           strlen(sbi->ll_instance) + 2);
1399                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
1400
1401                 if (mdc)
1402                         OBD_FREE(mdc, strlen(mdc) + 1);
1403                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) + 
1404                           strlen(sbi->ll_instance) + 2);
1405                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
1406         } else {
1407                 /* setup from dump_file */
1408                 if (list_empty(&lustre_profile_list)) {
1409                         CERROR("no profile\n");
1410                         GOTO(out_free, err = -EINVAL);
1411                 }
1412
1413                 lprof = list_entry(lustre_profile_list.next,
1414                                    struct lustre_profile, lp_list);
1415                 osc = lprof->lp_osc;
1416                 mdc = lprof->lp_mdc;
1417         }
1418
1419         if (!osc) {
1420                 CERROR("no osc\n");
1421                 GOTO(out_free, err = -EINVAL);
1422         }
1423         if (!mdc) {
1424                 CERROR("no mdc\n");
1425                 GOTO(out_free, err = -EINVAL);
1426         }
1427
1428         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1429         if (!fs) {
1430                 err = -ENOMEM;
1431                 goto out_free;
1432         }
1433
1434         obd = class_name2obd(mdc);
1435         if (!obd) {
1436                 CERROR("MDC %s: not setup or attached\n", mdc);
1437                 GOTO(out_free, err = -EINVAL);
1438         }
1439
1440 #warning "FIXME ASAP!"
1441 #if 0
1442         if (mdc_init_ea_size(obd, osc))
1443                 GOTO(out_free, err = -EINVAL);
1444 #endif
1445
1446         /* setup mdc */
1447         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
1448         if (err) {
1449                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1450                 GOTO(out_free, err);
1451         }
1452         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
1453
1454         err = obd_statfs(obd, &osfs, 100000000);
1455         if (err)
1456                 GOTO(out_mdc, err);
1457
1458         /*
1459          * FIXME fill fs stat data into sbi here!!! FIXME
1460          */
1461
1462         /* setup osc */
1463         obd = class_name2obd(osc);
1464         if (!obd) {
1465                 CERROR("OSC %s: not setup or attached\n", osc);
1466                 GOTO(out_mdc, err = -EINVAL);
1467         }
1468
1469         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
1470         if (err) {
1471                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1472                 GOTO(out_mdc, err);
1473         }
1474         sbi->ll_osc_exp = class_conn2export(&osc_conn);
1475
1476         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
1477         if (err) {
1478                 CERROR("cannot mds_connect: rc = %d\n", err);
1479                 GOTO(out_osc, err);
1480         }
1481         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
1482         sbi->ll_rootino = rootfid.id;
1483
1484         /* fetch attr of root inode */
1485         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
1486                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
1487         if (err) {
1488                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
1489                 GOTO(out_osc, err);
1490         }
1491
1492         err = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
1493                                 sbi->ll_osc_exp, &md);
1494         if (err) {
1495                 CERROR("failed to understand root inode md: rc = %d\n",err);
1496                 GOTO(out_request, err);
1497         }
1498
1499         LASSERT(sbi->ll_rootino != 0);
1500
1501         root = llu_iget(fs, &md);
1502         if (!root || IS_ERR(root)) {
1503                 CERROR("fail to generate root inode\n");
1504                 GOTO(out_request, err = -EBADF);
1505         }
1506
1507         /*
1508          * Generate base path-node for root.
1509          */
1510         rootpb = _sysio_pb_new(&noname, NULL, root);
1511         if (!rootpb) {
1512                 err = -ENOMEM;
1513                 goto out_inode;
1514         }
1515
1516         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
1517         if (err) {
1518                 _sysio_pb_gone(rootpb);
1519                 goto out_inode;
1520         }
1521
1522         ptlrpc_req_finished(request);
1523
1524         printf("LibLustre: namespace mounted successfully!\n");
1525
1526         return 0;
1527
1528 out_inode:
1529         _sysio_i_gone(root);
1530 out_request:
1531         ptlrpc_req_finished(request);
1532 out_osc:
1533         obd_disconnect(sbi->ll_osc_exp, 0);
1534 out_mdc:
1535         obd_disconnect(sbi->ll_mdc_exp, 0);
1536 out_free:
1537         OBD_FREE(sbi, sizeof(*sbi));
1538         return err;
1539 }
1540
1541 struct fssw_ops llu_fssw_ops = {
1542         llu_fsswop_mount
1543 };
1544
1545 static struct inode_ops llu_inode_ops = {
1546         inop_lookup:    llu_iop_lookup,
1547         inop_getattr:   llu_iop_getattr,
1548         inop_setattr:   llu_iop_setattr,
1549         inop_getdirentries:     llu_iop_getdirentries,
1550         inop_mkdir:     llu_iop_mkdir_raw,
1551         inop_rmdir:     llu_iop_rmdir_raw,
1552         inop_symlink:   llu_iop_symlink_raw,
1553         inop_readlink:  llu_iop_readlink,
1554         inop_open:      llu_iop_open,
1555         inop_close:     llu_iop_close,
1556         inop_link:      llu_iop_link_raw,
1557         inop_unlink:    llu_iop_unlink_raw,
1558         inop_rename:    llu_iop_rename_raw,
1559         inop_ipreadv:   llu_iop_ipreadv,
1560         inop_ipwritev:  llu_iop_ipwritev,
1561         inop_iodone:    llu_iop_iodone,
1562         inop_fcntl:     llu_iop_fcntl,
1563         inop_sync:      llu_iop_sync,
1564         inop_datasync:  llu_iop_datasync,
1565         inop_ioctl:     llu_iop_ioctl,
1566         inop_mknod:     llu_iop_mknod_raw,
1567 #ifdef _HAVE_STATVFS
1568         inop_statvfs:   llu_iop_statvfs,
1569 #endif
1570         inop_gone:      llu_iop_gone,
1571 };