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