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