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