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