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