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