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