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