Whamcloud - gitweb
pass lu_context to ->o_connect() method
[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-2004 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 <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 #ifdef HAVE_XTIO_H
41 #include <xtio.h>
42 #endif
43 #include <sysio.h>
44 #include <fs.h>
45 #include <mount.h>
46 #include <inode.h>
47 #ifdef HAVE_FILE_H
48 #include <file.h>
49 #endif
50
51 #undef LIST_HEAD
52
53 #include "llite_lib.h"
54
55 #ifndef MAY_EXEC
56 #define MAY_EXEC        1
57 #define MAY_WRITE       2
58 #define MAY_READ        4
59 #endif
60
61 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
62
63 static int ll_permission(struct inode *inode, int mask)
64 {
65         struct intnl_stat *st = llu_i2stat(inode);
66         mode_t mode = st->st_mode;
67
68         if (current->fsuid == st->st_uid)
69                 mode >>= 6;
70         else if (in_group_p(st->st_gid))
71                 mode >>= 3;
72
73         if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
74                 return 0;
75
76         if ((mask & (MAY_READ|MAY_WRITE)) ||
77             (st->st_mode & S_IXUGO))
78                 if (capable(CAP_DAC_OVERRIDE))
79                         return 0;
80
81         if (mask == MAY_READ ||
82             (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
83                 if (capable(CAP_DAC_READ_SEARCH))
84                         return 0;
85         }
86
87         return -EACCES;
88 }
89
90 static void llu_fsop_gone(struct filesys *fs)
91 {
92         struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
93         struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
94         int next = 0;
95         ENTRY;
96
97         list_del(&sbi->ll_conn_chain);
98         obd_disconnect(sbi->ll_dt_exp);
99         obd_disconnect(sbi->ll_md_exp);
100
101         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL){
102                 struct lustre_cfg_bufs bufs;
103                 struct lustre_cfg *lcfg;
104                 int err;
105
106                 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
107                 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
108                 err = class_process_config(lcfg);
109                 lustre_cfg_free(lcfg);
110                 if (err)
111                         CERROR("cleanup failed: %s\n", obd->obd_name);
112
113                 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
114                 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
115                 err = class_process_config(lcfg);
116                 if (err)
117                         CERROR("detach failed: %s\n", obd->obd_name);
118         }
119
120         OBD_FREE(sbi, sizeof(*sbi));
121
122         EXIT;
123 }
124
125 static struct inode_ops llu_inode_ops;
126
127 void llu_update_inode(struct inode *inode, struct mdt_body *body,
128                       struct lov_stripe_md *lsm)
129 {
130         struct llu_inode_info *lli = llu_i2info(inode);
131         struct intnl_stat *st = llu_i2stat(inode);
132
133         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
134         if (lsm != NULL) {
135                 if (lli->lli_smd == NULL) {
136                         lli->lli_smd = lsm;
137                         lli->lli_maxbytes = lsm->lsm_maxbytes;
138                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
139                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
140                 } else {
141                         if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
142                                 CERROR("lsm mismatch for inode %lld\n",
143                                        (long long)st->st_ino);
144                                 LBUG();
145                         }
146                 }
147         }
148
149         if (body->valid & OBD_MD_FLMTIME &&
150             body->mtime > LTIME_S(st->st_mtime))
151                 LTIME_S(st->st_mtime) = body->mtime;
152         if (body->valid & OBD_MD_FLATIME &&
153             body->atime > LTIME_S(st->st_atime))
154                 LTIME_S(st->st_atime) = body->atime;
155         if (body->valid & OBD_MD_FLCTIME &&
156             body->ctime > LTIME_S(st->st_ctime))
157                 LTIME_S(st->st_ctime) = body->ctime;
158         if (body->valid & OBD_MD_FLMODE)
159                 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
160         if (body->valid & OBD_MD_FLTYPE)
161                 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
162         if (S_ISREG(st->st_mode))
163                 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
164         else
165                 st->st_blksize = 4096;
166         if (body->valid & OBD_MD_FLUID)
167                 st->st_uid = body->uid;
168         if (body->valid & OBD_MD_FLGID)
169                 st->st_gid = body->gid;
170         if (body->valid & OBD_MD_FLNLINK)
171                 st->st_nlink = body->nlink;
172         if (body->valid & OBD_MD_FLRDEV)
173                 st->st_rdev = body->rdev;
174         if (body->valid & OBD_MD_FLSIZE)
175                 st->st_size = body->size;
176         if (body->valid & OBD_MD_FLBLOCKS)
177                 st->st_blocks = body->blocks;
178         if (body->valid & OBD_MD_FLFLAGS)
179                 lli->lli_st_flags = body->flags;
180 }
181
182 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
183 {
184         struct llu_inode_info *lli = llu_i2info(dst);
185         struct intnl_stat *st = llu_i2stat(dst);
186
187         valid &= src->o_valid;
188
189         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
190                 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
191                        src->o_valid,
192                        LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
193                        (long)src->o_mtime, (long)src->o_ctime);
194
195         if (valid & OBD_MD_FLATIME)
196                 LTIME_S(st->st_atime) = src->o_atime;
197         if (valid & OBD_MD_FLMTIME)
198                 LTIME_S(st->st_mtime) = src->o_mtime;
199         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
200                 LTIME_S(st->st_ctime) = src->o_ctime;
201         if (valid & OBD_MD_FLSIZE)
202                 st->st_size = src->o_size;
203         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
204                 st->st_blocks = src->o_blocks;
205         if (valid & OBD_MD_FLBLKSZ)
206                 st->st_blksize = src->o_blksize;
207         if (valid & OBD_MD_FLTYPE)
208                 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
209         if (valid & OBD_MD_FLMODE)
210                 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
211         if (valid & OBD_MD_FLUID)
212                 st->st_uid = src->o_uid;
213         if (valid & OBD_MD_FLGID)
214                 st->st_gid = src->o_gid;
215         if (valid & OBD_MD_FLFLAGS)
216                 lli->lli_st_flags = src->o_flags;
217 }
218
219 #define S_IRWXUGO       (S_IRWXU|S_IRWXG|S_IRWXO)
220 #define S_IALLUGO       (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
221
222 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
223 {
224         struct llu_inode_info *lli = llu_i2info(src);
225         struct intnl_stat *st = llu_i2stat(src);
226         obd_flag newvalid = 0;
227
228         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
229                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
230                        valid, LTIME_S(st->st_mtime),
231                        LTIME_S(st->st_ctime));
232
233         if (valid & OBD_MD_FLATIME) {
234                 dst->o_atime = LTIME_S(st->st_atime);
235                 newvalid |= OBD_MD_FLATIME;
236         }
237         if (valid & OBD_MD_FLMTIME) {
238                 dst->o_mtime = LTIME_S(st->st_mtime);
239                 newvalid |= OBD_MD_FLMTIME;
240         }
241         if (valid & OBD_MD_FLCTIME) {
242                 dst->o_ctime = LTIME_S(st->st_ctime);
243                 newvalid |= OBD_MD_FLCTIME;
244         }
245         if (valid & OBD_MD_FLSIZE) {
246                 dst->o_size = st->st_size;
247                 newvalid |= OBD_MD_FLSIZE;
248         }
249         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
250                 dst->o_blocks = st->st_blocks;
251                 newvalid |= OBD_MD_FLBLOCKS;
252         }
253         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
254                 dst->o_blksize = st->st_blksize;
255                 newvalid |= OBD_MD_FLBLKSZ;
256         }
257         if (valid & OBD_MD_FLTYPE) {
258                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
259                 newvalid |= OBD_MD_FLTYPE;
260         }
261         if (valid & OBD_MD_FLMODE) {
262                 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
263                 newvalid |= OBD_MD_FLMODE;
264         }
265         if (valid & OBD_MD_FLUID) {
266                 dst->o_uid = st->st_uid;
267                 newvalid |= OBD_MD_FLUID;
268         }
269         if (valid & OBD_MD_FLGID) {
270                 dst->o_gid = st->st_gid;
271                 newvalid |= OBD_MD_FLGID;
272         }
273         if (valid & OBD_MD_FLFLAGS) {
274                 dst->o_flags = lli->lli_st_flags;
275                 newvalid |= OBD_MD_FLFLAGS;
276         }
277         if (valid & OBD_MD_FLGENER) {
278                 dst->o_generation = lli->lli_st_generation;
279                 newvalid |= OBD_MD_FLGENER;
280         }
281         if (valid & OBD_MD_FLFID) {
282                 dst->o_fid = st->st_ino;
283                 newvalid |= OBD_MD_FLFID;
284         }
285
286         dst->o_valid |= newvalid;
287 }
288
289 /*
290  * really does the getattr on the inode and updates its fields
291  */
292 int llu_inode_getattr(struct inode *inode, struct lov_stripe_md *lsm)
293 {
294         struct llu_inode_info *lli = llu_i2info(inode);
295         struct obd_export *exp = llu_i2obdexp(inode);
296         struct ptlrpc_request_set *set;
297         struct obdo oa;
298         obd_flag refresh_valid;
299         int rc;
300         ENTRY;
301
302         LASSERT(lsm);
303         LASSERT(lli);
304
305         memset(&oa, 0, sizeof oa);
306         oa.o_id = lsm->lsm_object_id;
307         oa.o_mode = S_IFREG;
308         oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
309                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
310                 OBD_MD_FLCTIME;
311
312         set = ptlrpc_prep_set();
313         if (set == NULL) {
314                 CERROR ("ENOMEM allocing request set\n");
315                 rc = -ENOMEM;
316         } else {
317                 rc = obd_getattr_async(exp, &oa, lsm, set);
318                 if (rc == 0)
319                         rc = ptlrpc_set_wait(set);
320                 ptlrpc_set_destroy(set);
321         }
322         if (rc)
323                 RETURN(rc);
324
325         refresh_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
326                         OBD_MD_FLCTIME | OBD_MD_FLSIZE;
327
328         obdo_refresh_inode(inode, &oa, refresh_valid);
329
330         RETURN(0);
331 }
332
333 static struct inode* llu_new_inode(struct filesys *fs,
334                                    struct lu_fid *fid)
335 {
336         struct inode *inode;
337         struct llu_inode_info *lli;
338         struct intnl_stat st = {
339                 .st_dev  = 0,
340 #if 0
341 #ifndef AUTOMOUNT_FILE_NAME
342                 .st_mode = fid->f_type & S_IFMT,
343 #else
344                 .st_mode = fid->f_type /* all of the bits! */
345 #endif
346 #endif
347                 /* FIXME: fix this later */
348                 .st_mode = 0,
349
350                 .st_uid  = geteuid(),
351                 .st_gid  = getegid(),
352         };
353
354         OBD_ALLOC(lli, sizeof(*lli));
355         if (!lli)
356                 return NULL;
357
358         /* initialize lli here */
359         lli->lli_sbi = llu_fs2sbi(fs);
360         lli->lli_smd = NULL;
361         lli->lli_symlink_name = NULL;
362         lli->lli_flags = 0;
363         lli->lli_maxbytes = (__u64)(~0UL);
364         lli->lli_file_data = NULL;
365
366         lli->lli_sysio_fid.fid_data = &lli->lli_fid;
367         lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
368         lli->lli_fid = *fid;
369
370         /* file identifier is needed by functions like _sysio_i_find() */
371         inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
372                              &st, 0, &llu_inode_ops, lli);
373
374         if (!inode)
375                 OBD_FREE(lli, sizeof(*lli));
376
377         return inode;
378 }
379
380 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
381 {
382         struct llu_sb_info *sbi = llu_i2sbi(inode);
383         struct llu_inode_info *lli = llu_i2info(inode);
384         struct lustre_handle lockh;
385         struct ldlm_res_id res_id = { .name = {0} };
386         struct obd_device *obddev;
387         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
388         int flags;
389         ENTRY;
390
391         LASSERT(inode);
392
393         obddev = sbi->ll_md_exp->exp_obd;
394         res_id.name[0] = fid_seq(&lli->lli_fid);
395         res_id.name[1] = fid_oid(&lli->lli_fid);
396         res_id.name[2] = fid_ver(&lli->lli_fid);
397
398         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
399
400         /* FIXME use LDLM_FL_TEST_LOCK instead */
401         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
402         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
403                             &policy, LCK_PR, &lockh)) {
404                 ldlm_lock_decref(&lockh, LCK_PR);
405                 RETURN(1);
406         }
407
408         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
409                             &policy, LCK_PW, &lockh)) {
410                 ldlm_lock_decref(&lockh, LCK_PW);
411                 RETURN(1);
412         }
413         RETURN(0);
414 }
415
416 static int llu_inode_revalidate(struct inode *inode)
417 {
418         struct lov_stripe_md *lsm = NULL;
419         ENTRY;
420
421         if (!inode) {
422                 CERROR("REPORT THIS LINE TO PETER\n");
423                 RETURN(0);
424         }
425
426         if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
427                 struct lustre_md md;
428                 struct ptlrpc_request *req = NULL;
429                 struct llu_sb_info *sbi = llu_i2sbi(inode);
430                 unsigned long valid = OBD_MD_FLGETATTR;
431                 int rc, ealen = 0;
432
433                 /* Why don't we update all valid MDS fields here, if we're
434                  * doing an RPC anyways?  -phil */
435                 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
436                         ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
437                         valid |= OBD_MD_FLEASIZE;
438                 }
439                 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
440                                 valid, ealen, &req);
441                 if (rc) {
442                         CERROR("failure %d inode %llu\n", rc,
443                                (long long)llu_i2stat(inode)->st_ino);
444                         RETURN(-abs(rc));
445                 }
446                 rc = md_get_lustre_md(sbi->ll_md_exp, req, 0,
447                                       sbi->ll_dt_exp, &md);
448
449                 /* XXX Too paranoid? */
450                 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
451                     !((md.body->valid & OBD_MD_FLNLINK) &&
452                       (md.body->nlink == 0))) {
453                         CERROR("Asked for %s eadata but got %s (%d)\n",
454                                (valid & OBD_MD_FLEASIZE) ? "some" : "no",
455                                (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
456                                 md.body->eadatasize);
457                 }
458                 if (rc) {
459                         ptlrpc_req_finished(req);
460                         RETURN(rc);
461                 }
462
463
464                 llu_update_inode(inode, md.body, md.lsm);
465                 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
466                         obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
467
468                 if (md.body->valid & OBD_MD_FLSIZE)
469                         set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
470                                 &llu_i2info(inode)->lli_flags);
471                 ptlrpc_req_finished(req);
472         }
473
474         lsm = llu_i2info(inode)->lli_smd;
475         if (!lsm)       /* object not yet allocated, don't validate size */
476                 RETURN(0);
477
478         /* ll_glimpse_size will prefer locally cached writes if they extend
479          * the file */
480         RETURN(llu_glimpse_size(inode));
481 }
482
483 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
484 {
485         *b = *llu_i2stat(ino);
486 }
487
488 static int llu_iop_getattr(struct pnode *pno,
489                            struct inode *ino,
490                            struct intnl_stat *b)
491 {
492         int rc;
493         ENTRY;
494
495         liblustre_wait_event(0);
496
497         if (!ino) {
498                 LASSERT(pno);
499                 LASSERT(pno->p_base->pb_ino);
500                 ino = pno->p_base->pb_ino;
501         } else {
502                 LASSERT(!pno || pno->p_base->pb_ino == ino);
503         }
504
505         /* libsysio might call us directly without intent lock,
506          * we must re-fetch the attrs here
507          */
508         rc = llu_inode_revalidate(ino);
509         if (!rc) {
510                 copy_stat_buf(ino, b);
511                 LASSERT(!llu_i2info(ino)->lli_it);
512         }
513
514         liblustre_wait_event(0);
515         RETURN(rc);
516 }
517
518 static int null_if_equal(struct ldlm_lock *lock, void *data)
519 {
520         if (data == lock->l_ast_data) {
521                 lock->l_ast_data = NULL;
522
523                 if (lock->l_req_mode != lock->l_granted_mode)
524                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
525         }
526
527         return LDLM_ITER_CONTINUE;
528 }
529
530 void llu_clear_inode(struct inode *inode)
531 {
532         struct llu_inode_info *lli = llu_i2info(inode);
533         struct llu_sb_info *sbi = llu_i2sbi(inode);
534         ENTRY;
535
536         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
537                (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
538                inode);
539
540         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
541         md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
542                          null_if_equal, inode);
543
544         if (lli->lli_smd)
545                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
546                                   null_if_equal, inode);
547
548         if (lli->lli_smd) {
549                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
550                 lli->lli_smd = NULL;
551         }
552
553         if (lli->lli_symlink_name) {
554                 OBD_FREE(lli->lli_symlink_name,
555                          strlen(lli->lli_symlink_name) + 1);
556                 lli->lli_symlink_name = NULL;
557         }
558
559         EXIT;
560 }
561
562 void llu_iop_gone(struct inode *inode)
563 {
564         struct llu_inode_info *lli = llu_i2info(inode);
565         ENTRY;
566
567         liblustre_wait_event(0);
568         llu_clear_inode(inode);
569
570         OBD_FREE(lli, sizeof(*lli));
571         EXIT;
572 }
573
574 static int inode_setattr(struct inode * inode, struct iattr * attr)
575 {
576         unsigned int ia_valid = attr->ia_valid;
577         struct intnl_stat *st = llu_i2stat(inode);
578         int error = 0;
579
580         /*
581          * inode_setattr() is only ever invoked with ATTR_SIZE (by
582          * llu_setattr_raw()) when file has no bodies. Check this.
583          */
584         LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
585
586         if (ia_valid & ATTR_SIZE)
587                 st->st_size = attr->ia_size;
588         if (ia_valid & ATTR_UID)
589                 st->st_uid = attr->ia_uid;
590         if (ia_valid & ATTR_GID)
591                 st->st_gid = attr->ia_gid;
592         if (ia_valid & ATTR_ATIME)
593                 st->st_atime = attr->ia_atime;
594         if (ia_valid & ATTR_MTIME)
595                 st->st_mtime = attr->ia_mtime;
596         if (ia_valid & ATTR_CTIME)
597                 st->st_ctime = attr->ia_ctime;
598         if (ia_valid & ATTR_MODE) {
599                 st->st_mode = attr->ia_mode;
600                 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
601                         st->st_mode &= ~S_ISGID;
602         }
603         /* mark_inode_dirty(inode); */
604         return error;
605 }
606
607 /* If this inode has objects allocated to it (lsm != NULL), then the OST
608  * object(s) determine the file size and mtime.  Otherwise, the MDS will
609  * keep these values until such a time that objects are allocated for it.
610  * We do the MDS operations first, as it is checking permissions for us.
611  * We don't to the MDS RPC if there is nothing that we want to store there,
612  * otherwise there is no harm in updating mtime/atime on the MDS if we are
613  * going to do an RPC anyways.
614  *
615  * If we are doing a truncate, we will send the mtime and ctime updates
616  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
617  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
618  * at the same time.
619  */
620 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
621 {
622         struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
623         struct llu_sb_info *sbi = llu_i2sbi(inode);
624         struct intnl_stat *st = llu_i2stat(inode);
625         struct ptlrpc_request *request = NULL;
626         struct md_op_data op_data = { { 0 } };
627         int ia_valid = attr->ia_valid;
628         int rc = 0;
629         ENTRY;
630
631         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
632
633         if (ia_valid & ATTR_SIZE) {
634                 if (attr->ia_size > ll_file_maxbytes(inode)) {
635                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
636                                (long long)attr->ia_size,
637                                ll_file_maxbytes(inode));
638                         RETURN(-EFBIG);
639                 }
640
641                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
642         }
643
644         /* We mark all of the fields "set" so MDS/OST does not re-set them */
645         if (attr->ia_valid & ATTR_CTIME) {
646                 attr->ia_ctime = CURRENT_TIME;
647                 attr->ia_valid |= ATTR_CTIME_SET;
648         }
649         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
650                 attr->ia_atime = CURRENT_TIME;
651                 attr->ia_valid |= ATTR_ATIME_SET;
652         }
653         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
654                 attr->ia_mtime = CURRENT_TIME;
655                 attr->ia_valid |= ATTR_MTIME_SET;
656         }
657
658         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
659                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
660                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
661                        LTIME_S(CURRENT_TIME));
662         if (lsm)
663                 attr->ia_valid &= ~ATTR_SIZE;
664
665         /* If only OST attributes being set on objects, don't do MDS RPC.
666          * In that case, we need to check permissions and update the local
667          * inode ourselves so we can call obdo_from_inode() always. */
668         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
669                 struct lustre_md md;
670                 llu_prepare_md_op_data(&op_data, inode, NULL, NULL, 0, 0);
671
672                 rc = md_setattr(sbi->ll_md_exp, &op_data,
673                                 attr, NULL, 0, NULL, 0, &request);
674
675                 if (rc) {
676                         ptlrpc_req_finished(request);
677                         if (rc != -EPERM && rc != -EACCES)
678                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
679                         RETURN(rc);
680                 }
681
682                 rc = md_get_lustre_md(sbi->ll_md_exp, request, 0,
683                                       sbi->ll_dt_exp, &md);
684                 if (rc) {
685                         ptlrpc_req_finished(request);
686                         RETURN(rc);
687                 }
688
689                 /* We call inode_setattr to adjust timestamps.
690                  * If there is at least some data in file, we cleared ATTR_SIZE
691                  * above to avoid invoking vmtruncate, otherwise it is important
692                  * to call vmtruncate in inode_setattr to update inode->i_size
693                  * (bug 6196) */
694                 inode_setattr(inode, attr);
695                 llu_update_inode(inode, md.body, md.lsm);
696                 ptlrpc_req_finished(request);
697
698                 if (!lsm || !S_ISREG(st->st_mode)) {
699                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
700                         RETURN(0);
701                 }
702         } else {
703                 /* The OST doesn't check permissions, but the alternative is
704                  * a gratuitous RPC to the MDS.  We already rely on the client
705                  * to do read/write/truncate permission checks, so is mtime OK?
706                  */
707                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
708                         /* from sys_utime() */
709                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
710                                 if (current->fsuid != st->st_uid &&
711                                     (rc = ll_permission(inode, MAY_WRITE)) != 0)
712                                         RETURN(rc);
713                         } else {
714                                 /* from inode_change_ok() */
715                                 if (current->fsuid != st->st_uid &&
716                                     !capable(CAP_FOWNER))
717                                         RETURN(-EPERM);
718                         }
719                 }
720
721                 /* Won't invoke llu_vmtruncate(), as we already cleared
722                  * ATTR_SIZE */
723                 inode_setattr(inode, attr);
724         }
725
726         if (ia_valid & ATTR_SIZE) {
727                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
728                                                            OBD_OBJECT_EOF} };
729                 struct lustre_handle lockh = { 0, };
730                 struct lustre_handle match_lockh = { 0, };
731
732                 int err;
733                 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
734                 int lock_mode;
735                 obd_flag obd_flags;
736
737                 /* check that there are no matching locks */
738                 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
739                                   LCK_PW, &flags, inode, &match_lockh) <= 0);
740
741                 /* XXX when we fix the AST intents to pass the discard-range
742                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
743                  * XXX here. */
744                 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
745
746                 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
747                         lock_mode = LCK_NL;
748                         obd_flags = OBD_FL_TRUNCLOCK;
749                         CDEBUG(D_INODE, "delegating locking to the OST");
750                 } else {
751                         lock_mode = LCK_PW;
752                         obd_flags = 0;
753                 }
754
755                 /* with lock_mode == LK_NL no lock is taken. */
756                 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
757                                      &lockh, flags);
758                 if (rc != ELDLM_OK) {
759                         if (rc > 0)
760                                 RETURN(-ENOLCK);
761                         RETURN(rc);
762                 }
763
764                 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
765
766                 /* unlock now as we don't mind others file lockers racing with
767                  * the mds updates below? */
768                 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
769                 if (err) {
770                         CERROR("llu_extent_unlock failed: %d\n", err);
771                         if (!rc)
772                                 rc = err;
773                 }
774         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
775                 struct obdo oa;
776
777                 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
778                        (long long)st->st_ino, LTIME_S(attr->ia_mtime));
779                 oa.o_id = lsm->lsm_object_id;
780                 oa.o_valid = OBD_MD_FLID;
781                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
782                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
783                 rc = obd_setattr(sbi->ll_dt_exp, &oa, lsm, NULL);
784                 if (rc)
785                         CERROR("obd_setattr fails: rc=%d\n", rc);
786         }
787         RETURN(rc);
788 }
789
790 /* here we simply act as a thin layer to glue it with
791  * llu_setattr_raw(), which is copy from kernel
792  */
793 static int llu_iop_setattr(struct pnode *pno,
794                            struct inode *ino,
795                            unsigned mask,
796                            struct intnl_stat *stbuf)
797 {
798         struct iattr iattr;
799         int rc;
800         ENTRY;
801
802         liblustre_wait_event(0);
803
804         LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
805                            SETATTR_UID | SETATTR_GID |
806                            SETATTR_LEN | SETATTR_MODE)));
807         memset(&iattr, 0, sizeof(iattr));
808
809         if (mask & SETATTR_MODE) {
810                 iattr.ia_mode = stbuf->st_mode;
811                 iattr.ia_valid |= ATTR_MODE;
812         }
813         if (mask & SETATTR_MTIME) {
814                 iattr.ia_mtime = stbuf->st_mtime;
815                 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
816         }
817         if (mask & SETATTR_ATIME) {
818                 iattr.ia_atime = stbuf->st_atime;
819                 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
820         }
821         if (mask & SETATTR_UID) {
822                 iattr.ia_uid = stbuf->st_uid;
823                 iattr.ia_valid |= ATTR_UID;
824         }
825         if (mask & SETATTR_GID) {
826                 iattr.ia_gid = stbuf->st_gid;
827                 iattr.ia_valid |= ATTR_GID;
828         }
829         if (mask & SETATTR_LEN) {
830                 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
831                 iattr.ia_valid |= ATTR_SIZE;
832         }
833
834         iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
835         iattr.ia_ctime = CURRENT_TIME;
836
837         rc = llu_setattr_raw(ino, &iattr);
838         liblustre_wait_event(0);
839         RETURN(rc);
840 }
841
842 #define EXT2_LINK_MAX           32000
843
844 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
845 {
846         struct inode *dir = pno->p_base->pb_parent->pb_ino;
847         struct qstr *qstr = &pno->p_base->pb_name;
848         const char *name = qstr->name;
849         int len = qstr->len;
850         struct ptlrpc_request *request = NULL;
851         struct llu_sb_info *sbi = llu_i2sbi(dir);
852         struct md_op_data op_data = { { 0 } };
853         struct lu_placement_hint hint = {
854                 .ph_pname = NULL,
855                 .ph_cname = qstr,
856                 .ph_opc = LUSTRE_OPC_SYMLINK
857         };
858         int err = -EMLINK;
859         ENTRY;
860
861         liblustre_wait_event(0);
862         if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
863                 RETURN(err);
864
865         /* allocate new fid */
866         err = llu_fid_md_alloc(sbi, &op_data.fid2, &hint);
867         if (err) {
868                 CERROR("can't allocate new fid, rc %d\n", err);
869                 RETURN(err);
870         }
871         llu_prepare_md_op_data(&op_data, dir, NULL, name, len, 0);
872         err = md_create(sbi->ll_md_exp, &op_data,
873                         tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
874                         current->fsuid, current->fsgid, current->cap_effective,
875                         0, &request);
876         ptlrpc_req_finished(request);
877         liblustre_wait_event(0);
878         RETURN(err);
879 }
880
881 static int llu_readlink_internal(struct inode *inode,
882                                  struct ptlrpc_request **request,
883                                  char **symname)
884 {
885         struct llu_inode_info *lli = llu_i2info(inode);
886         struct llu_sb_info *sbi = llu_i2sbi(inode);
887         struct mdt_body *body;
888         struct intnl_stat *st = llu_i2stat(inode);
889         int rc, symlen = st->st_size + 1;
890         ENTRY;
891
892         *request = NULL;
893
894         if (lli->lli_symlink_name) {
895                 *symname = lli->lli_symlink_name;
896                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
897                 RETURN(0);
898         }
899
900         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
901                         OBD_MD_LINKNAME, symlen, request);
902         if (rc) {
903                 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
904                 RETURN(rc);
905         }
906
907         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
908         LASSERT (body != NULL);
909         LASSERT_REPSWABBED (*request, 0);
910
911         if ((body->valid & OBD_MD_LINKNAME) == 0) {
912                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
913                 GOTO (failed, rc = -EPROTO);
914         }
915
916         LASSERT(symlen != 0);
917         if (body->eadatasize != symlen) {
918                 CERROR("inode %llu: symlink length %d not expected %d\n",
919                        (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
920                 GOTO(failed, rc = -EPROTO);
921         }
922
923         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
924         if (*symname == NULL ||
925             strnlen(*symname, symlen) != symlen - 1) {
926                 /* not full/NULL terminated */
927                 CERROR("inode %llu: symlink not NULL terminated string"
928                        "of length %d\n", (long long)st->st_ino, symlen - 1);
929                 GOTO(failed, rc = -EPROTO);
930         }
931
932         OBD_ALLOC(lli->lli_symlink_name, symlen);
933         /* do not return an error if we cannot cache the symlink locally */
934         if (lli->lli_symlink_name)
935                 memcpy(lli->lli_symlink_name, *symname, symlen);
936
937         RETURN(0);
938
939  failed:
940         ptlrpc_req_finished (*request);
941         RETURN (-EPROTO);
942 }
943
944 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
945 {
946         struct inode *inode = pno->p_base->pb_ino;
947         struct ptlrpc_request *request;
948         char *symname;
949         int rc;
950         ENTRY;
951
952         liblustre_wait_event(0);
953         rc = llu_readlink_internal(inode, &request, &symname);
954         if (rc)
955                 GOTO(out, rc);
956
957         LASSERT(symname);
958         strncpy(data, symname, bufsize);
959         rc = strlen(symname);
960
961         ptlrpc_req_finished(request);
962  out:
963         liblustre_wait_event(0);
964         RETURN(rc);
965 }
966
967 static int llu_iop_mknod_raw(struct pnode *pno,
968                              mode_t mode,
969                              dev_t dev)
970 {
971         struct ptlrpc_request *request = NULL;
972         struct inode *dir = pno->p_parent->p_base->pb_ino;
973         struct llu_sb_info *sbi = llu_i2sbi(dir);
974         struct md_op_data op_data = { { 0 } };
975         int err = -EMLINK;
976         struct lu_placement_hint hint = {
977                 .ph_pname = NULL,
978                 .ph_cname = &pno->p_base->pb_name,
979                 .ph_opc = LUSTRE_OPC_MKNOD
980         };
981         ENTRY;
982
983         liblustre_wait_event(0);
984         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
985                (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
986                (long long)llu_i2stat(dir)->st_ino);
987
988         if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
989                 RETURN(err);
990
991         switch (mode & S_IFMT) {
992         case 0:
993         case S_IFREG:
994                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
995         case S_IFCHR:
996         case S_IFBLK:
997         case S_IFIFO:
998         case S_IFSOCK:
999                 /* allocate new fid */
1000                 err = llu_fid_md_alloc(sbi, &op_data.fid2, &hint);
1001                 if (err) {
1002                         CERROR("can't allocate new fid, rc %d\n", err);
1003                         RETURN(err);
1004                 }
1005
1006                 llu_prepare_md_op_data(&op_data, dir, NULL,
1007                                        pno->p_base->pb_name.name,
1008                                        pno->p_base->pb_name.len,
1009                                        0);
1010                 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1011                                 current->fsuid, current->fsgid,
1012                                 current->cap_effective, dev, &request);
1013                 ptlrpc_req_finished(request);
1014                 break;
1015         case S_IFDIR:
1016                 err = -EPERM;
1017                 break;
1018         default:
1019                 err = -EINVAL;
1020         }
1021         liblustre_wait_event(0);
1022         RETURN(err);
1023 }
1024
1025 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1026 {
1027         struct inode *src = old->p_base->pb_ino;
1028         struct inode *dir = new->p_parent->p_base->pb_ino;
1029         const char *name = new->p_base->pb_name.name;
1030         int namelen = new->p_base->pb_name.len;
1031         struct ptlrpc_request *request = NULL;
1032         struct md_op_data op_data = { { 0 } };
1033         int rc;
1034         ENTRY;
1035
1036         LASSERT(src);
1037         LASSERT(dir);
1038
1039         liblustre_wait_event(0);
1040         llu_prepare_md_op_data(&op_data, src, dir, name, namelen, 0);
1041         rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1042         ptlrpc_req_finished(request);
1043         liblustre_wait_event(0);
1044
1045         RETURN(rc);
1046 }
1047
1048 /*
1049  * libsysio will clear the inode immediately after return
1050  */
1051 static int llu_iop_unlink_raw(struct pnode *pno)
1052 {
1053         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1054         struct qstr *qstr = &pno->p_base->pb_name;
1055         const char *name = qstr->name;
1056         int len = qstr->len;
1057         struct inode *target = pno->p_base->pb_ino;
1058         struct ptlrpc_request *request = NULL;
1059         struct md_op_data op_data = { { 0 } };
1060         int rc;
1061         ENTRY;
1062
1063         LASSERT(target);
1064
1065         liblustre_wait_event(0);
1066         llu_prepare_md_op_data(&op_data, dir, NULL, name, len, 0);
1067         rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1068         if (!rc)
1069                 rc = llu_objects_destroy(request, dir);
1070         ptlrpc_req_finished(request);
1071         liblustre_wait_event(0);
1072
1073         RETURN(rc);
1074 }
1075
1076 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1077 {
1078         struct inode *src = old->p_parent->p_base->pb_ino;
1079         struct inode *tgt = new->p_parent->p_base->pb_ino;
1080         const char *oldname = old->p_base->pb_name.name;
1081         int oldnamelen = old->p_base->pb_name.len;
1082         const char *newname = new->p_base->pb_name.name;
1083         int newnamelen = new->p_base->pb_name.len;
1084         struct ptlrpc_request *request = NULL;
1085         struct md_op_data op_data = { { 0 } };
1086         int rc;
1087         ENTRY;
1088
1089         LASSERT(src);
1090         LASSERT(tgt);
1091
1092         liblustre_wait_event(0);
1093         llu_prepare_md_op_data(&op_data, src, tgt, NULL, 0, 0);
1094         rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1095                        oldname, oldnamelen, newname, newnamelen,
1096                        &request);
1097         if (!rc) {
1098                 rc = llu_objects_destroy(request, src);
1099         }
1100
1101         ptlrpc_req_finished(request);
1102         liblustre_wait_event(0);
1103
1104         RETURN(rc);
1105 }
1106
1107 #ifdef _HAVE_STATVFS
1108 static int llu_statfs_internal(struct llu_sb_info *sbi,
1109                                struct obd_statfs *osfs,
1110                                unsigned long max_age)
1111 {
1112         struct obd_statfs obd_osfs;
1113         int rc;
1114         ENTRY;
1115
1116         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1117         if (rc) {
1118                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1119                 RETURN(rc);
1120         }
1121
1122         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1123                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1124
1125         rc = obd_statfs(class_exp2obd(sbi->ll_dt_exp), &obd_osfs, max_age);
1126         if (rc) {
1127                 CERROR("obd_statfs fails: rc = %d\n", rc);
1128                 RETURN(rc);
1129         }
1130
1131         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1132                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1133                obd_osfs.os_files);
1134
1135         osfs->os_blocks = obd_osfs.os_blocks;
1136         osfs->os_bfree = obd_osfs.os_bfree;
1137         osfs->os_bavail = obd_osfs.os_bavail;
1138
1139         /* If we don't have as many objects free on the OST as inodes
1140          * on the MDS, we reduce the total number of inodes to
1141          * compensate, so that the "inodes in use" number is correct.
1142          */
1143         if (obd_osfs.os_ffree < osfs->os_ffree) {
1144                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1145                         obd_osfs.os_ffree;
1146                 osfs->os_ffree = obd_osfs.os_ffree;
1147         }
1148
1149         RETURN(rc);
1150 }
1151
1152 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1153 {
1154         struct obd_statfs osfs;
1155         int rc;
1156
1157         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1158
1159         /* For now we will always get up-to-date statfs values, but in the
1160          * future we may allow some amount of caching on the client (e.g.
1161          * from QOS or lprocfs updates). */
1162         rc = llu_statfs_internal(sbi, &osfs, jiffies - 1);
1163         if (rc)
1164                 return rc;
1165
1166         statfs_unpack(sfs, &osfs);
1167
1168         if (sizeof(sfs->f_blocks) == 4) {
1169                 while (osfs.os_blocks > ~0UL) {
1170                         sfs->f_bsize <<= 1;
1171
1172                         osfs.os_blocks >>= 1;
1173                         osfs.os_bfree >>= 1;
1174                         osfs.os_bavail >>= 1;
1175                 }
1176         }
1177
1178         sfs->f_blocks = osfs.os_blocks;
1179         sfs->f_bfree = osfs.os_bfree;
1180         sfs->f_bavail = osfs.os_bavail;
1181
1182         return 0;
1183 }
1184
1185 static int llu_iop_statvfs(struct pnode *pno,
1186                            struct inode *ino,
1187                            struct intnl_statvfs *buf)
1188 {
1189         struct statfs fs;
1190         int rc;
1191         ENTRY;
1192
1193         liblustre_wait_event(0);
1194
1195 #ifndef __CYGWIN__
1196         LASSERT(pno->p_base->pb_ino);
1197         rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1198         if (rc)
1199                 RETURN(rc);
1200
1201         /* from native driver */
1202         buf->f_bsize = fs.f_bsize;  /* file system block size */
1203         buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1204         buf->f_blocks = fs.f_blocks;
1205         buf->f_bfree = fs.f_bfree;
1206         buf->f_bavail = fs.f_bavail;
1207         buf->f_files = fs.f_files;  /* Total number serial numbers */
1208         buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */
1209         buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1210         buf->f_fsid = fs.f_fsid.__val[1];
1211         buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */
1212         buf->f_namemax = fs.f_namelen;
1213 #endif
1214
1215         liblustre_wait_event(0);
1216         RETURN(0);
1217 }
1218 #endif /* _HAVE_STATVFS */
1219
1220 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1221 {
1222         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1223         struct qstr *qstr = &pno->p_base->pb_name;
1224         const char *name = qstr->name;
1225         int len = qstr->len;
1226         struct ptlrpc_request *request = NULL;
1227         struct intnl_stat *st = llu_i2stat(dir);
1228         struct md_op_data op_data = { { 0 } };
1229         struct lu_placement_hint hint = {
1230                 .ph_pname = NULL,
1231                 .ph_cname = qstr,
1232                 .ph_opc = LUSTRE_OPC_MKDIR
1233         };
1234
1235         int err = -EMLINK;
1236         ENTRY;
1237
1238         liblustre_wait_event(0);
1239         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1240                (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1241
1242         if (st->st_nlink >= EXT2_LINK_MAX)
1243                 RETURN(err);
1244
1245         /* allocate new fid */
1246         err = llu_fid_md_alloc(llu_i2sbi(dir), &op_data.fid2, &hint);
1247         if (err) {
1248                 CERROR("can't allocate new fid, rc %d\n", err);
1249                 RETURN(err);
1250         }
1251         llu_prepare_md_op_data(&op_data, dir, NULL, name, len, 0);
1252         err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0, mode,
1253                         current->fsuid, current->fsgid, current->cap_effective,
1254                         0, &request);
1255         ptlrpc_req_finished(request);
1256         liblustre_wait_event(0);
1257         RETURN(err);
1258 }
1259
1260 static int llu_iop_rmdir_raw(struct pnode *pno)
1261 {
1262         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1263         struct qstr *qstr = &pno->p_base->pb_name;
1264         const char *name = qstr->name;
1265         int len = qstr->len;
1266         struct ptlrpc_request *request = NULL;
1267         struct md_op_data op_data = { { 0 } };
1268         int rc;
1269         ENTRY;
1270
1271         liblustre_wait_event(0);
1272         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1273                (long long)llu_i2stat(dir)->st_ino,
1274                llu_i2info(dir)->lli_st_generation, dir);
1275
1276         llu_prepare_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1277         rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1278         ptlrpc_req_finished(request);
1279
1280         liblustre_wait_event(0);
1281         RETURN(rc);
1282 }
1283
1284 #ifdef O_DIRECT
1285 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1286 #else
1287 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1288 #endif
1289 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1290
1291 /* refer to ll_file_flock() for details */
1292 static int llu_file_flock(struct inode *ino,
1293                           int cmd,
1294                           struct file_lock *file_lock)
1295 {
1296         struct obd_device *obddev;
1297         struct llu_inode_info *lli = llu_i2info(ino);
1298         struct intnl_stat *st = llu_i2stat(ino);
1299         struct ldlm_res_id res_id =
1300                 { .name = {fid_seq(&lli->lli_fid),
1301                            fid_oid(&lli->lli_fid),
1302                            fid_ver(&lli->lli_fid),
1303                            LDLM_FLOCK} };
1304         struct lustre_handle lockh = {0};
1305         ldlm_policy_data_t flock;
1306         ldlm_mode_t mode = 0;
1307         int flags = 0;
1308         int rc;
1309
1310         CDEBUG(D_VFSTRACE, "VFS Op:inode="LPU64" file_lock=%p\n",
1311                st->st_ino, file_lock);
1312
1313         flock.l_flock.pid = file_lock->fl_pid;
1314         flock.l_flock.start = file_lock->fl_start;
1315         flock.l_flock.end = file_lock->fl_end;
1316
1317         switch (file_lock->fl_type) {
1318         case F_RDLCK:
1319                 mode = LCK_PR;
1320                 break;
1321         case F_UNLCK:
1322                 mode = LCK_NL;
1323                 break;
1324         case F_WRLCK:
1325                 mode = LCK_PW;
1326                 break;
1327         default:
1328                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1329                 LBUG();
1330         }
1331
1332         switch (cmd) {
1333         case F_SETLKW:
1334 #ifdef F_SETLKW64
1335 #if F_SETLKW64 != F_SETLKW
1336         case F_SETLKW64:
1337 #endif
1338 #endif
1339                 flags = 0;
1340                 break;
1341         case F_SETLK:
1342 #ifdef F_SETLK64
1343 #if F_SETLK64 != F_SETLK
1344         case F_SETLK64:
1345 #endif
1346 #endif
1347                 flags = LDLM_FL_BLOCK_NOWAIT;
1348                 break;
1349         case F_GETLK:
1350 #ifdef F_GETLK64
1351 #if F_GETLK64 != F_GETLK
1352         case F_GETLK64:
1353 #endif
1354 #endif
1355                 flags = LDLM_FL_TEST_LOCK;
1356                 file_lock->fl_type = mode;
1357                 break;
1358         default:
1359                 CERROR("unknown fcntl cmd: %d\n", cmd);
1360                 LBUG();
1361         }
1362
1363         CDEBUG(D_DLMTRACE, "inode="LPU64", pid=%u, flags=%#x, mode=%u, "
1364                "start="LPU64", end="LPU64"\n", st->st_ino, flock.l_flock.pid,
1365                flags, mode, flock.l_flock.start, flock.l_flock.end);
1366
1367         obddev = llu_i2mdcexp(ino)->exp_obd;
1368         rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, obddev->obd_namespace,
1369                               res_id, LDLM_FLOCK, &flock, mode, &flags,
1370                               NULL, ldlm_flock_completion_ast, NULL, file_lock,
1371                               NULL, 0, NULL, &lockh);
1372         RETURN(rc);
1373 }
1374
1375 static int assign_type(struct file_lock *fl, int type)
1376 {
1377         switch (type) {
1378         case F_RDLCK:
1379         case F_WRLCK:
1380         case F_UNLCK:
1381                 fl->fl_type = type;
1382                 return 0;
1383         default:
1384                 return -EINVAL;
1385         }
1386 }
1387
1388 static int flock_to_posix_lock(struct inode *ino,
1389                                struct file_lock *fl,
1390                                struct flock *l)
1391 {
1392         switch (l->l_whence) {
1393         /* XXX: only SEEK_SET is supported in lustre */
1394         case SEEK_SET:
1395                 fl->fl_start = 0;
1396                 break;
1397         default:
1398                 return -EINVAL;
1399         }
1400
1401         fl->fl_end = l->l_len - 1;
1402         if (l->l_len < 0)
1403                 return -EINVAL;
1404         if (l->l_len == 0)
1405                 fl->fl_end = OFFSET_MAX;
1406
1407         fl->fl_pid = getpid();
1408         fl->fl_flags = FL_POSIX;
1409         fl->fl_notify = NULL;
1410         fl->fl_insert = NULL;
1411         fl->fl_remove = NULL;
1412         /* XXX: these fields can't be filled with suitable values,
1413                 but I think lustre doesn't use them.
1414          */
1415         fl->fl_owner = NULL;
1416         fl->fl_file = NULL;
1417
1418         return assign_type(fl, l->l_type);
1419 }
1420
1421 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1422 {
1423         struct file_lock fl;
1424         int error;
1425
1426         error = EINVAL;
1427         if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1428                 goto out;
1429
1430         error = flock_to_posix_lock(ino, &fl, flock);
1431         if (error)
1432                 goto out;
1433
1434         error = llu_file_flock(ino, F_GETLK, &fl);
1435         if (error)
1436                 goto out;
1437
1438         flock->l_type = F_UNLCK;
1439         if (fl.fl_type != F_UNLCK) {
1440                 flock->l_pid = fl.fl_pid;
1441                 flock->l_start = fl.fl_start;
1442                 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1443                         fl.fl_end - fl.fl_start + 1;
1444                 flock->l_whence = SEEK_SET;
1445                 flock->l_type = fl.fl_type;
1446         }
1447
1448 out:
1449         return error;
1450 }
1451
1452 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1453 {
1454         struct file_lock fl;
1455         int flags = llu_i2info(ino)->lli_open_flags + 1;
1456         int error;
1457
1458         error = flock_to_posix_lock(ino, &fl, flock);
1459         if (error)
1460                 goto out;
1461         if (cmd == F_SETLKW)
1462                 fl.fl_flags |= FL_SLEEP;
1463
1464         error = -EBADF;
1465         switch (flock->l_type) {
1466         case F_RDLCK:
1467                 if (!(flags & FMODE_READ))
1468                         goto out;
1469                 break;
1470         case F_WRLCK:
1471                 if (!(flags & FMODE_WRITE))
1472                         goto out;
1473                 break;
1474         case F_UNLCK:
1475                 break;
1476         default:
1477                 error = -EINVAL;
1478                 goto out;
1479         }
1480
1481         error = llu_file_flock(ino, cmd, &fl);
1482         if (error)
1483                 goto out;
1484
1485 out:
1486         return error;
1487 }
1488
1489 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1490 {
1491         struct llu_inode_info *lli = llu_i2info(ino);
1492         long flags;
1493         struct flock *flock;
1494         long err = 0;
1495
1496         liblustre_wait_event(0);
1497         switch (cmd) {
1498         case F_GETFL:
1499                 *rtn = lli->lli_open_flags;
1500                 break;
1501         case F_SETFL:
1502                 flags = va_arg(ap, long);
1503                 flags &= FCNTL_FLMASK;
1504                 if (flags & FCNTL_FLMASK_INVALID) {
1505                         CERROR("liblustre don't support O_NONBLOCK, O_ASYNC, "
1506                                "and O_DIRECT on file descriptor\n");
1507                         *rtn = -EINVAL;
1508                         err = EINVAL;
1509                         break;
1510                 }
1511                 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1512                                       (lli->lli_open_flags & ~FCNTL_FLMASK);
1513                 *rtn = 0;
1514                 break;
1515         case F_GETLK:
1516 #ifdef F_GETLK64
1517 #if F_GETLK64 != F_GETLK
1518         case F_GETLK64:
1519 #endif
1520 #endif
1521                 flock = va_arg(ap, struct flock *);
1522                 err = llu_fcntl_getlk(ino, flock);
1523                 *rtn = err? -1: 0;
1524                 break;
1525         case F_SETLK:
1526 #ifdef F_SETLKW64
1527 #if F_SETLKW64 != F_SETLKW
1528         case F_SETLKW64:
1529 #endif
1530 #endif
1531         case F_SETLKW:
1532 #ifdef F_SETLK64
1533 #if F_SETLK64 != F_SETLK
1534         case F_SETLK64:
1535 #endif
1536 #endif
1537                 flock = va_arg(ap, struct flock *);
1538                 err = llu_fcntl_setlk(ino, cmd, flock);
1539                 *rtn = err? -1: 0;
1540                 break;
1541         default:
1542                 CERROR("unsupported fcntl cmd %x\n", cmd);
1543                 *rtn = -ENOSYS;
1544                 err = ENOSYS;
1545                 break;
1546         }
1547
1548         liblustre_wait_event(0);
1549         return err;
1550 }
1551
1552 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1553 {
1554         struct llu_inode_info *lli = llu_i2info(inode);
1555         struct ll_file_data *fd = lli->lli_file_data;
1556         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1557                                                     .end = OBD_OBJECT_EOF}};
1558         struct lustre_handle lockh = { 0 };
1559         struct lov_stripe_md *lsm = lli->lli_smd;
1560         ldlm_error_t err;
1561         int flags = 0;
1562         ENTRY;
1563
1564         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1565                 RETURN(-EINVAL);
1566         }
1567
1568         policy.l_extent.gid = arg;
1569         if (lli->lli_open_flags & O_NONBLOCK)
1570                 flags = LDLM_FL_BLOCK_NOWAIT;
1571
1572         err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1573                               flags);
1574         if (err)
1575                 RETURN(err);
1576
1577         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1578         fd->fd_gid = arg;
1579         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1580
1581         RETURN(0);
1582 }
1583
1584 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1585 {
1586         struct llu_inode_info *lli = llu_i2info(inode);
1587         struct ll_file_data *fd = lli->lli_file_data;
1588         struct lov_stripe_md *lsm = lli->lli_smd;
1589         ldlm_error_t err;
1590         ENTRY;
1591
1592         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1593                 RETURN(-EINVAL);
1594
1595         if (fd->fd_gid != arg)
1596                 RETURN(-EINVAL);
1597
1598         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1599
1600         err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1601         if (err)
1602                 RETURN(err);
1603
1604         fd->fd_gid = 0;
1605         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1606
1607         RETURN(0);
1608 }
1609
1610 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1611                          va_list ap)
1612 {
1613         unsigned long arg;
1614         int rc;
1615
1616         liblustre_wait_event(0);
1617
1618         switch (request) {
1619         case LL_IOC_GROUP_LOCK:
1620                 arg = va_arg(ap, unsigned long);
1621                 rc = llu_get_grouplock(ino, arg);
1622                 break;
1623         case LL_IOC_GROUP_UNLOCK:
1624                 arg = va_arg(ap, unsigned long);
1625                 rc = llu_put_grouplock(ino, arg);
1626                 break;
1627         default:
1628                 CERROR("did not support ioctl cmd %lx\n", request);
1629                 rc = -ENOSYS;
1630                 break;
1631         }
1632
1633         liblustre_wait_event(0);
1634         return rc;
1635 }
1636
1637 /*
1638  * we already do syncronous read/write
1639  */
1640 static int llu_iop_sync(struct inode *inode)
1641 {
1642         liblustre_wait_event(0);
1643         return 0;
1644 }
1645
1646 static int llu_iop_datasync(struct inode *inode)
1647 {
1648         liblustre_wait_event(0);
1649         return 0;
1650 }
1651
1652 struct filesys_ops llu_filesys_ops =
1653 {
1654         fsop_gone: llu_fsop_gone,
1655 };
1656
1657 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1658 {
1659         struct inode *inode;
1660         struct lu_fid fid;
1661         struct file_identifier fileid = {&fid, sizeof(fid)};
1662
1663         if ((md->body->valid &
1664              (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1665             (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1666                 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1667                 LBUG();
1668                 return ERR_PTR(-EPERM);
1669         }
1670
1671         /* try to find existing inode */
1672         fid = md->body->fid1;
1673
1674         inode = _sysio_i_find(fs, &fileid);
1675         if (inode) {
1676                 if (inode->i_zombie/* ||
1677                     lli->lli_st_generation != md->body->generation*/) {
1678                         I_RELE(inode);
1679                 }
1680                 else {
1681                         llu_update_inode(inode, md->body, md->lsm);
1682                         return inode;
1683                 }
1684         }
1685
1686         inode = llu_new_inode(fs, &fid);
1687         if (inode)
1688                 llu_update_inode(inode, md->body, md->lsm);
1689
1690         return inode;
1691 }
1692
1693 static int
1694 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1695 {
1696         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1697         __u32 valsize = sizeof(struct lov_desc);
1698         int rc, easize, def_easize, cookiesize;
1699         struct lov_desc desc;
1700         __u32 stripes;
1701         ENTRY;
1702
1703         rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1704                           &valsize, &desc);
1705         if (rc)
1706                 RETURN(rc);
1707
1708         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1709         lsm.lsm_stripe_count = stripes;
1710         easize = obd_size_diskmd(dt_exp, &lsm);
1711
1712         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1713         def_easize = obd_size_diskmd(dt_exp, &lsm);
1714
1715         cookiesize = stripes * sizeof(struct llog_cookie);
1716
1717         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1718                easize, cookiesize);
1719
1720         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1721         RETURN(rc);
1722 }
1723
1724 static int
1725 llu_fsswop_mount(const char *source,
1726                  unsigned flags,
1727                  const void *data __IS_UNUSED,
1728                  struct pnode *tocover,
1729                  struct mount **mntp)
1730 {
1731         struct filesys *fs;
1732         struct inode *root;
1733         struct pnode_base *rootpb;
1734         struct obd_device *obd;
1735         struct lu_fid rootfid;
1736         struct llu_sb_info *sbi;
1737         struct obd_statfs osfs;
1738         static struct qstr noname = { NULL, 0, 0 };
1739         struct ptlrpc_request *request = NULL;
1740         struct lustre_handle mdc_conn = {0, };
1741         struct lustre_handle osc_conn = {0, };
1742         struct lustre_md md;
1743         class_uuid_t uuid;
1744         struct config_llog_instance cfg;
1745         char ll_instance[sizeof(sbi) * 2 + 1];
1746         struct lustre_profile *lprof;
1747         char *zconf_mdsnid, *zconf_mdsname, *zconf_profile;
1748         char *osc = NULL, *mdc = NULL;
1749         int async = 1, err = -EINVAL;
1750         struct obd_connect_data ocd = {0,};
1751
1752         ENTRY;
1753
1754         if (ll_parse_mount_target(source,
1755                                   &zconf_mdsnid,
1756                                   &zconf_mdsname,
1757                                   &zconf_profile)) {
1758                 CERROR("mal-formed target %s\n", source);
1759                 RETURN(err);
1760         }
1761         if (!zconf_mdsnid || !zconf_mdsname || !zconf_profile) {
1762                 printf("Liblustre: invalid target %s\n", source);
1763                 RETURN(err);
1764         }
1765         /* allocate & initialize sbi */
1766         OBD_ALLOC(sbi, sizeof(*sbi));
1767         if (!sbi)
1768                 RETURN(-ENOMEM);
1769
1770         INIT_LIST_HEAD(&sbi->ll_conn_chain);
1771         generate_random_uuid(uuid);
1772         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1773
1774         /* generate a string unique to this super, let's try
1775          the address of the super itself.*/
1776         sprintf(ll_instance, "%p", sbi);
1777
1778         /* retrive & parse config log */
1779         cfg.cfg_instance = ll_instance;
1780         cfg.cfg_uuid = sbi->ll_sb_uuid;
1781         err = liblustre_process_log(&cfg, zconf_mdsnid, zconf_mdsname,
1782                                     zconf_profile, 1);
1783         if (err < 0) {
1784                 CERROR("Unable to process log: %s\n", zconf_profile);
1785                 GOTO(out_free, err);
1786         }
1787
1788         lprof = class_get_profile(zconf_profile);
1789         if (lprof == NULL) {
1790                 CERROR("No profile found: %s\n", zconf_profile);
1791                 GOTO(out_free, err = -EINVAL);
1792         }
1793         OBD_ALLOC(osc, strlen(lprof->lp_osc) + strlen(ll_instance) + 2);
1794         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
1795
1796         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) + strlen(ll_instance) + 2);
1797         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
1798
1799         if (!osc) {
1800                 CERROR("no osc\n");
1801                 GOTO(out_free, err = -EINVAL);
1802         }
1803         if (!mdc) {
1804                 CERROR("no mdc\n");
1805                 GOTO(out_free, err = -EINVAL);
1806         }
1807
1808         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1809         if (!fs) {
1810                 err = -ENOMEM;
1811                 goto out_free;
1812         }
1813
1814         obd = class_name2obd(mdc);
1815         if (!obd) {
1816                 CERROR("MDC %s: not setup or attached\n", mdc);
1817                 GOTO(out_free, err = -EINVAL);
1818         }
1819         obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
1820                            sizeof(async), &async, NULL);
1821
1822         ocd.ocd_connect_flags = OBD_CONNECT_IBITS|OBD_CONNECT_VERSION;
1823         ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
1824         ocd.ocd_version = LUSTRE_VERSION_CODE;
1825
1826         /* setup mdc */
1827         err = obd_connect(NULL, &mdc_conn, obd, &sbi->ll_sb_uuid, &ocd);
1828         if (err) {
1829                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1830                 GOTO(out_free, err);
1831         }
1832         sbi->ll_md_exp = class_conn2export(&mdc_conn);
1833
1834         err = obd_statfs(obd, &osfs, 100000000);
1835         if (err)
1836                 GOTO(out_mdc, err);
1837
1838         err = llu_fid_md_init(sbi);
1839         if (err)
1840                 GOTO(out_mdc, err);
1841
1842         /*
1843          * FIXME fill fs stat data into sbi here!!! FIXME
1844          */
1845
1846         /* setup osc */
1847         obd = class_name2obd(osc);
1848         if (!obd) {
1849                 CERROR("OSC %s: not setup or attached\n", osc);
1850                 GOTO(out_md_fid, err = -EINVAL);
1851         }
1852         obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
1853                            sizeof(async), &async, NULL);
1854
1855         obd->obd_upcall.onu_owner = &sbi->ll_lco;
1856         obd->obd_upcall.onu_upcall = ll_ocd_update;
1857
1858         ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK|OBD_CONNECT_REQPORTAL|
1859                                 OBD_CONNECT_VERSION|OBD_CONNECT_TRUNCLOCK;
1860         ocd.ocd_version = LUSTRE_VERSION_CODE;
1861         err = obd_connect(NULL, &osc_conn, obd, &sbi->ll_sb_uuid, &ocd);
1862         if (err) {
1863                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1864                 GOTO(out_mdc, err);
1865         }
1866         sbi->ll_dt_exp = class_conn2export(&osc_conn);
1867         sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
1868
1869         err = llu_fid_dt_init(sbi);
1870         if (err)
1871                 GOTO(out_osc, err);
1872
1873         llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
1874
1875         err = md_getstatus(sbi->ll_md_exp, &rootfid);
1876         if (err) {
1877                 CERROR("cannot mds_connect: rc = %d\n", err);
1878                 GOTO(out_dt_fid, err);
1879         }
1880         CDEBUG(D_SUPER, "rootfid "DFID3"\n", PFID3(&rootfid));
1881         sbi->ll_root_fid = rootfid;
1882
1883         /* fetch attr of root inode */
1884         err = md_getattr(sbi->ll_md_exp, &rootfid,
1885                          OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
1886         if (err) {
1887                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
1888                 GOTO(out_osc, err);
1889         }
1890
1891         err = md_get_lustre_md(sbi->ll_md_exp, request, 0,
1892                                sbi->ll_dt_exp, &md);
1893         if (err) {
1894                 CERROR("failed to understand root inode md: rc = %d\n",err);
1895                 GOTO(out_request, err);
1896         }
1897
1898         LASSERT(fid_is_sane(&sbi->ll_root_fid));
1899
1900         root = llu_iget(fs, &md);
1901         if (!root || IS_ERR(root)) {
1902                 CERROR("fail to generate root inode\n");
1903                 GOTO(out_request, err = -EBADF);
1904         }
1905
1906         /*
1907          * Generate base path-node for root.
1908          */
1909         rootpb = _sysio_pb_new(&noname, NULL, root);
1910         if (!rootpb) {
1911                 err = -ENOMEM;
1912                 goto out_inode;
1913         }
1914
1915         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
1916         if (err) {
1917                 _sysio_pb_gone(rootpb);
1918                 goto out_inode;
1919         }
1920
1921         ptlrpc_req_finished(request);
1922
1923         CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
1924         liblustre_wait_event(0);
1925
1926         return 0;
1927
1928 out_inode:
1929         _sysio_i_gone(root);
1930 out_request:
1931         ptlrpc_req_finished(request);
1932 out_dt_fid:
1933         llu_fid_dt_fini(sbi);
1934 out_osc:
1935         obd_disconnect(sbi->ll_dt_exp);
1936 out_md_fid:
1937         llu_fid_md_fini(sbi);
1938 out_mdc:
1939         obd_disconnect(sbi->ll_md_exp);
1940 out_free:
1941         if (osc)
1942                 OBD_FREE(osc, strlen(osc) + 1);
1943         if (mdc)
1944                 OBD_FREE(mdc, strlen(mdc) + 1);
1945         OBD_FREE(sbi, sizeof(*sbi));
1946         return err;
1947 }
1948
1949 struct fssw_ops llu_fssw_ops = {
1950         llu_fsswop_mount
1951 };
1952
1953 static struct inode_ops llu_inode_ops = {
1954         inop_lookup:    llu_iop_lookup,
1955         inop_getattr:   llu_iop_getattr,
1956         inop_setattr:   llu_iop_setattr,
1957         inop_filldirentries:     llu_iop_filldirentries,
1958         inop_mkdir:     llu_iop_mkdir_raw,
1959         inop_rmdir:     llu_iop_rmdir_raw,
1960         inop_symlink:   llu_iop_symlink_raw,
1961         inop_readlink:  llu_iop_readlink,
1962         inop_open:      llu_iop_open,
1963         inop_close:     llu_iop_close,
1964         inop_link:      llu_iop_link_raw,
1965         inop_unlink:    llu_iop_unlink_raw,
1966         inop_rename:    llu_iop_rename_raw,
1967         inop_pos:       llu_iop_pos,
1968         inop_read:      llu_iop_read,
1969         inop_write:     llu_iop_write,
1970         inop_iodone:    llu_iop_iodone,
1971         inop_fcntl:     llu_iop_fcntl,
1972         inop_sync:      llu_iop_sync,
1973         inop_datasync:  llu_iop_datasync,
1974         inop_ioctl:     llu_iop_ioctl,
1975         inop_mknod:     llu_iop_mknod_raw,
1976 #ifdef _HAVE_STATVFS
1977         inop_statvfs:   llu_iop_statvfs,
1978 #endif
1979         inop_gone:      llu_iop_gone,
1980 };