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