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