Whamcloud - gitweb
552a5869b9107b549ce3622d79e651a5d935192d
[fs/lustre-release.git] / lustre / liblustre / super.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/liblustre/super.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <assert.h>
46 #include <time.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <sys/queue.h>
51 #ifndef __CYGWIN__
52 # include <sys/statvfs.h>
53 #else
54 # include <sys/statfs.h>
55 #endif
56
57 #include <sysio.h>
58 #ifdef HAVE_XTIO_H
59 #include <xtio.h>
60 #endif
61 #include <fs.h>
62 #include <mount.h>
63 #include <inode.h>
64 #ifdef HAVE_FILE_H
65 #include <file.h>
66 #endif
67
68 #undef LIST_HEAD
69
70 #include "llite_lib.h"
71
72 #ifndef MAY_EXEC
73 #define MAY_EXEC        1
74 #define MAY_WRITE       2
75 #define MAY_READ        4
76 #endif
77
78 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
79
80 static int ll_permission(struct inode *inode, int mask)
81 {
82         struct intnl_stat *st = llu_i2stat(inode);
83         mode_t mode = st->st_mode;
84
85         if (current->fsuid == st->st_uid)
86                 mode >>= 6;
87         else if (in_group_p(st->st_gid))
88                 mode >>= 3;
89
90         if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
91                 return 0;
92
93         if ((mask & (MAY_READ|MAY_WRITE)) ||
94             (st->st_mode & S_IXUGO))
95                 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
96                         return 0;
97
98         if (mask == MAY_READ ||
99             (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
100                 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
101                         return 0;
102         }
103
104         return -EACCES;
105 }
106
107 static void llu_fsop_gone(struct filesys *fs)
108 {
109         struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
110         struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
111         int next = 0;
112         ENTRY;
113
114         list_del(&sbi->ll_conn_chain);
115         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) && !cfs_capable(CFS_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                                     !cfs_capable(CFS_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, tgt, strlen(tgt) + 1,
983                         S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
984                         cfs_curproc_cap_pack(), 0, &request);
985         ptlrpc_req_finished(request);
986         liblustre_wait_event(0);
987         RETURN(err);
988 }
989
990 static int llu_readlink_internal(struct inode *inode,
991                                  struct ptlrpc_request **request,
992                                  char **symname)
993 {
994         struct llu_inode_info *lli = llu_i2info(inode);
995         struct llu_sb_info *sbi = llu_i2sbi(inode);
996         struct mdt_body *body;
997         struct intnl_stat *st = llu_i2stat(inode);
998         int rc, symlen = st->st_size + 1;
999         ENTRY;
1000
1001         *request = NULL;
1002         *symname = NULL;
1003
1004         if (lli->lli_symlink_name) {
1005                 *symname = lli->lli_symlink_name;
1006                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
1007                 RETURN(0);
1008         }
1009
1010         rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
1011                         OBD_MD_LINKNAME, symlen, request);
1012         if (rc) {
1013                 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
1014                 RETURN(rc);
1015         }
1016
1017         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
1018         LASSERT(body != NULL);
1019
1020         if ((body->valid & OBD_MD_LINKNAME) == 0) {
1021                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
1022                 GOTO (failed, rc = -EPROTO);
1023         }
1024
1025         LASSERT(symlen != 0);
1026         if (body->eadatasize != symlen) {
1027                 CERROR("inode %llu: symlink length %d not expected %d\n",
1028                        (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1029                 GOTO(failed, rc = -EPROTO);
1030         }
1031
1032         *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
1033         if (*symname == NULL ||
1034             strnlen(*symname, symlen) != symlen - 1) {
1035                 /* not full/NULL terminated */
1036                 CERROR("inode %llu: symlink not NULL terminated string"
1037                        "of length %d\n", (long long)st->st_ino, symlen - 1);
1038                 GOTO(failed, rc = -EPROTO);
1039         }
1040
1041         OBD_ALLOC(lli->lli_symlink_name, symlen);
1042         /* do not return an error if we cannot cache the symlink locally */
1043         if (lli->lli_symlink_name)
1044                 memcpy(lli->lli_symlink_name, *symname, symlen);
1045
1046         RETURN(0);
1047
1048  failed:
1049         ptlrpc_req_finished (*request);
1050         RETURN (-EPROTO);
1051 }
1052
1053 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1054 {
1055         struct inode *inode = pno->p_base->pb_ino;
1056         struct ptlrpc_request *request;
1057         char *symname;
1058         int rc;
1059         ENTRY;
1060
1061         liblustre_wait_event(0);
1062         rc = llu_readlink_internal(inode, &request, &symname);
1063         if (rc)
1064                 GOTO(out, rc);
1065
1066         LASSERT(symname);
1067         strncpy(data, symname, bufsize);
1068         rc = strlen(symname);
1069
1070         ptlrpc_req_finished(request);
1071  out:
1072         liblustre_wait_event(0);
1073         RETURN(rc);
1074 }
1075
1076 static int llu_iop_mknod_raw(struct pnode *pno,
1077                              mode_t mode,
1078                              dev_t dev)
1079 {
1080         struct ptlrpc_request *request = NULL;
1081         struct inode *dir = pno->p_parent->p_base->pb_ino;
1082         struct llu_sb_info *sbi = llu_i2sbi(dir);
1083         struct md_op_data op_data = {{ 0 }};
1084         int err = -EMLINK;
1085         ENTRY;
1086
1087         liblustre_wait_event(0);
1088         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1089                (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1090                (long long)llu_i2stat(dir)->st_ino);
1091
1092         if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1093                 RETURN(err);
1094
1095         switch (mode & S_IFMT) {
1096         case 0:
1097         case S_IFREG:
1098                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1099         case S_IFCHR:
1100         case S_IFBLK:
1101         case S_IFIFO:
1102         case S_IFSOCK:
1103                 llu_prep_md_op_data(&op_data, dir, NULL,
1104                                     pno->p_base->pb_name.name,
1105                                     pno->p_base->pb_name.len, 0,
1106                                     LUSTRE_OPC_MKNOD);
1107
1108                 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1109                                 current->fsuid, current->fsgid,
1110                                 cfs_curproc_cap_pack(), dev, &request);
1111                 ptlrpc_req_finished(request);
1112                 break;
1113         case S_IFDIR:
1114                 err = -EPERM;
1115                 break;
1116         default:
1117                 err = -EINVAL;
1118         }
1119         liblustre_wait_event(0);
1120         RETURN(err);
1121 }
1122
1123 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1124 {
1125         struct inode *src = old->p_base->pb_ino;
1126         struct inode *dir = new->p_parent->p_base->pb_ino;
1127         const char *name = new->p_base->pb_name.name;
1128         int namelen = new->p_base->pb_name.len;
1129         struct ptlrpc_request *request = NULL;
1130         struct md_op_data op_data = {{ 0 }};
1131         int rc;
1132         ENTRY;
1133
1134         LASSERT(src);
1135         LASSERT(dir);
1136
1137         liblustre_wait_event(0);
1138         llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0, 
1139                             LUSTRE_OPC_ANY);
1140         rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1141         ptlrpc_req_finished(request);
1142         liblustre_wait_event(0);
1143
1144         RETURN(rc);
1145 }
1146
1147 /*
1148  * libsysio will clear the inode immediately after return
1149  */
1150 static int llu_iop_unlink_raw(struct pnode *pno)
1151 {
1152         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1153         struct qstr *qstr = &pno->p_base->pb_name;
1154         const char *name = qstr->name;
1155         int len = qstr->len;
1156         struct inode *target = pno->p_base->pb_ino;
1157         struct ptlrpc_request *request = NULL;
1158         struct md_op_data op_data = { { 0 } };
1159         int rc;
1160         ENTRY;
1161
1162         LASSERT(target);
1163
1164         liblustre_wait_event(0);
1165         llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0, 
1166                             LUSTRE_OPC_ANY);
1167         rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1168         if (!rc)
1169                 rc = llu_objects_destroy(request, dir);
1170         ptlrpc_req_finished(request);
1171         liblustre_wait_idle();
1172
1173         RETURN(rc);
1174 }
1175
1176 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1177 {
1178         struct inode *src = old->p_parent->p_base->pb_ino;
1179         struct inode *tgt = new->p_parent->p_base->pb_ino;
1180         const char *oldname = old->p_base->pb_name.name;
1181         int oldnamelen = old->p_base->pb_name.len;
1182         const char *newname = new->p_base->pb_name.name;
1183         int newnamelen = new->p_base->pb_name.len;
1184         struct ptlrpc_request *request = NULL;
1185         struct md_op_data op_data = { { 0 } };
1186         int rc;
1187         ENTRY;
1188
1189         LASSERT(src);
1190         LASSERT(tgt);
1191
1192         liblustre_wait_event(0);
1193         llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0, 
1194                             LUSTRE_OPC_ANY);
1195         rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1196                        oldname, oldnamelen, newname, newnamelen,
1197                        &request);
1198         if (!rc) {
1199                 rc = llu_objects_destroy(request, src);
1200         }
1201
1202         ptlrpc_req_finished(request);
1203         liblustre_wait_idle();
1204
1205         RETURN(rc);
1206 }
1207
1208 #ifdef _HAVE_STATVFS
1209 static int llu_statfs_internal(struct llu_sb_info *sbi,
1210                                struct obd_statfs *osfs, __u64 max_age)
1211 {
1212         struct obd_statfs obd_osfs;
1213         int rc;
1214         ENTRY;
1215
1216         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1217         if (rc) {
1218                 CERROR("md_statfs fails: rc = %d\n", rc);
1219                 RETURN(rc);
1220         }
1221
1222         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1223                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1224
1225         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1226                               &obd_statfs, max_age, 0);
1227         if (rc) {
1228                 CERROR("obd_statfs fails: rc = %d\n", rc);
1229                 RETURN(rc);
1230         }
1231
1232         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1233                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1234                obd_osfs.os_files);
1235
1236         osfs->os_blocks = obd_osfs.os_blocks;
1237         osfs->os_bfree = obd_osfs.os_bfree;
1238         osfs->os_bavail = obd_osfs.os_bavail;
1239
1240         /* If we don't have as many objects free on the OST as inodes
1241          * on the MDS, we reduce the total number of inodes to
1242          * compensate, so that the "inodes in use" number is correct.
1243          */
1244         if (obd_osfs.os_ffree < osfs->os_ffree) {
1245                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1246                         obd_osfs.os_ffree;
1247                 osfs->os_ffree = obd_osfs.os_ffree;
1248         }
1249
1250         RETURN(rc);
1251 }
1252
1253 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1254 {
1255         struct obd_statfs osfs;
1256         int rc;
1257
1258         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1259
1260         /* For now we will always get up-to-date statfs values, but in the
1261          * future we may allow some amount of caching on the client (e.g.
1262          * from QOS or lprocfs updates). */
1263         rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1264         if (rc)
1265                 return rc;
1266
1267         statfs_unpack(sfs, &osfs);
1268
1269         if (sizeof(sfs->f_blocks) == 4) {
1270                 while (osfs.os_blocks > ~0UL) {
1271                         sfs->f_bsize <<= 1;
1272
1273                         osfs.os_blocks >>= 1;
1274                         osfs.os_bfree >>= 1;
1275                         osfs.os_bavail >>= 1;
1276                 }
1277         }
1278
1279         sfs->f_blocks = osfs.os_blocks;
1280         sfs->f_bfree = osfs.os_bfree;
1281         sfs->f_bavail = osfs.os_bavail;
1282
1283         return 0;
1284 }
1285
1286 static int llu_iop_statvfs(struct pnode *pno,
1287                            struct inode *ino,
1288                            struct intnl_statvfs *buf)
1289 {
1290         struct statfs fs;
1291         int rc;
1292         ENTRY;
1293
1294         liblustre_wait_event(0);
1295
1296 #ifndef __CYGWIN__
1297         LASSERT(pno->p_base->pb_ino);
1298         rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1299         if (rc)
1300                 RETURN(rc);
1301
1302         /* from native driver */
1303         buf->f_bsize = fs.f_bsize;  /* file system block size */
1304         buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1305         buf->f_blocks = fs.f_blocks;
1306         buf->f_bfree = fs.f_bfree;
1307         buf->f_bavail = fs.f_bavail;
1308         buf->f_files = fs.f_files;  /* Total number serial numbers */
1309         buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */
1310         buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1311         buf->f_fsid = fs.f_fsid.__val[1];
1312         buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */
1313         buf->f_namemax = fs.f_namelen;
1314 #endif
1315
1316         liblustre_wait_event(0);
1317         RETURN(0);
1318 }
1319 #endif /* _HAVE_STATVFS */
1320
1321 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1322 {
1323         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1324         struct qstr *qstr = &pno->p_base->pb_name;
1325         const char *name = qstr->name;
1326         int len = qstr->len;
1327         struct ptlrpc_request *request = NULL;
1328         struct intnl_stat *st = llu_i2stat(dir);
1329         struct md_op_data op_data = {{ 0 }};
1330         int err = -EMLINK;
1331         ENTRY;
1332
1333         liblustre_wait_event(0);
1334         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1335                (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1336
1337         if (st->st_nlink >= EXT2_LINK_MAX)
1338                 RETURN(err);
1339
1340         llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0, 
1341                             LUSTRE_OPC_MKDIR);
1342
1343         err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1344                         mode | S_IFDIR, current->fsuid, current->fsgid,
1345                         cfs_curproc_cap_pack(), 0, &request);
1346         ptlrpc_req_finished(request);
1347         liblustre_wait_event(0);
1348         RETURN(err);
1349 }
1350
1351 static int llu_iop_rmdir_raw(struct pnode *pno)
1352 {
1353         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1354         struct qstr *qstr = &pno->p_base->pb_name;
1355         const char *name = qstr->name;
1356         int len = qstr->len;
1357         struct ptlrpc_request *request = NULL;
1358         struct md_op_data op_data = {{ 0 }};
1359         int rc;
1360         ENTRY;
1361
1362         liblustre_wait_event(0);
1363         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1364                (long long)llu_i2stat(dir)->st_ino,
1365                llu_i2info(dir)->lli_st_generation, dir);
1366
1367         llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR, 
1368                             LUSTRE_OPC_ANY);
1369         rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1370         ptlrpc_req_finished(request);
1371
1372         liblustre_wait_event(0);
1373         RETURN(rc);
1374 }
1375
1376 #ifdef O_DIRECT
1377 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1378 #else
1379 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1380 #endif
1381 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1382
1383 /* refer to ll_file_flock() for details */
1384 static int llu_file_flock(struct inode *ino,
1385                           int cmd,
1386                           struct file_lock *file_lock)
1387 {
1388         struct llu_inode_info *lli = llu_i2info(ino);
1389         struct intnl_stat *st = llu_i2stat(ino);
1390         struct ldlm_res_id res_id =
1391                 { .name = {fid_seq(&lli->lli_fid),
1392                            fid_oid(&lli->lli_fid),
1393                            fid_ver(&lli->lli_fid),
1394                            LDLM_FLOCK} };
1395         struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1396                 ldlm_flock_completion_ast, NULL, file_lock };
1397
1398         struct lustre_handle lockh = {0};
1399         ldlm_policy_data_t flock;
1400         int flags = 0;
1401         int rc;
1402
1403         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1404                (unsigned long long)st->st_ino, file_lock);
1405
1406         flock.l_flock.pid = file_lock->fl_pid;
1407         flock.l_flock.start = file_lock->fl_start;
1408         flock.l_flock.end = file_lock->fl_end;
1409
1410         switch (file_lock->fl_type) {
1411         case F_RDLCK:
1412                 einfo.ei_mode = LCK_PR;
1413                 break;
1414         case F_UNLCK:
1415                 einfo.ei_mode = LCK_NL;
1416                 break;
1417         case F_WRLCK:
1418                 einfo.ei_mode = LCK_PW;
1419                 break;
1420         default:
1421                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1422                 LBUG();
1423         }
1424
1425         switch (cmd) {
1426         case F_SETLKW:
1427 #ifdef F_SETLKW64
1428 #if F_SETLKW64 != F_SETLKW
1429         case F_SETLKW64:
1430 #endif
1431 #endif
1432                 flags = 0;
1433                 break;
1434         case F_SETLK:
1435 #ifdef F_SETLK64
1436 #if F_SETLK64 != F_SETLK
1437         case F_SETLK64:
1438 #endif
1439 #endif
1440                 flags = LDLM_FL_BLOCK_NOWAIT;
1441                 break;
1442         case F_GETLK:
1443 #ifdef F_GETLK64
1444 #if F_GETLK64 != F_GETLK
1445         case F_GETLK64:
1446 #endif
1447 #endif
1448                 flags = LDLM_FL_TEST_LOCK;
1449                 file_lock->fl_type = einfo.ei_mode;
1450                 break;
1451         default:
1452                 CERROR("unknown fcntl cmd: %d\n", cmd);
1453                 LBUG();
1454         }
1455
1456         CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1457                "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1458                flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1459                flock.l_flock.end);
1460
1461         {
1462                 struct lmv_obd *lmv;
1463                 struct obd_device *lmv_obd;
1464                 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1465                 lmv = &lmv_obd->u.lmv;
1466
1467                 if (lmv->desc.ld_tgt_count < 1)
1468                         RETURN(rc = -ENODEV);
1469                 
1470                 if (lmv->tgts[0].ltd_exp != NULL)
1471                         rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id, 
1472                                               &flock, &flags, NULL, 0, NULL, &lockh, 0);
1473                 else
1474                         rc = -ENODEV;
1475         }
1476         RETURN(rc);
1477 }
1478
1479 static int assign_type(struct file_lock *fl, int type)
1480 {
1481         switch (type) {
1482         case F_RDLCK:
1483         case F_WRLCK:
1484         case F_UNLCK:
1485                 fl->fl_type = type;
1486                 return 0;
1487         default:
1488                 return -EINVAL;
1489         }
1490 }
1491
1492 static int flock_to_posix_lock(struct inode *ino,
1493                                struct file_lock *fl,
1494                                struct flock *l)
1495 {
1496         switch (l->l_whence) {
1497         /* XXX: only SEEK_SET is supported in lustre */
1498         case SEEK_SET:
1499                 fl->fl_start = 0;
1500                 break;
1501         default:
1502                 return -EINVAL;
1503         }
1504
1505         fl->fl_end = l->l_len - 1;
1506         if (l->l_len < 0)
1507                 return -EINVAL;
1508         if (l->l_len == 0)
1509                 fl->fl_end = OFFSET_MAX;
1510
1511         fl->fl_pid = getpid();
1512         fl->fl_flags = FL_POSIX;
1513         fl->fl_notify = NULL;
1514         fl->fl_insert = NULL;
1515         fl->fl_remove = NULL;
1516         /* XXX: these fields can't be filled with suitable values,
1517                 but I think lustre doesn't use them.
1518          */
1519         fl->fl_owner = NULL;
1520         fl->fl_file = NULL;
1521
1522         return assign_type(fl, l->l_type);
1523 }
1524
1525 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1526 {
1527         struct file_lock fl;
1528         int error;
1529
1530         error = EINVAL;
1531         if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1532                 goto out;
1533
1534         error = flock_to_posix_lock(ino, &fl, flock);
1535         if (error)
1536                 goto out;
1537
1538         error = llu_file_flock(ino, F_GETLK, &fl);
1539         if (error)
1540                 goto out;
1541
1542         flock->l_type = F_UNLCK;
1543         if (fl.fl_type != F_UNLCK) {
1544                 flock->l_pid = fl.fl_pid;
1545                 flock->l_start = fl.fl_start;
1546                 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1547                         fl.fl_end - fl.fl_start + 1;
1548                 flock->l_whence = SEEK_SET;
1549                 flock->l_type = fl.fl_type;
1550         }
1551
1552 out:
1553         return error;
1554 }
1555
1556 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1557 {
1558         struct file_lock fl;
1559         int flags = llu_i2info(ino)->lli_open_flags + 1;
1560         int error;
1561
1562         error = flock_to_posix_lock(ino, &fl, flock);
1563         if (error)
1564                 goto out;
1565         if (cmd == F_SETLKW)
1566                 fl.fl_flags |= FL_SLEEP;
1567
1568         error = -EBADF;
1569         switch (flock->l_type) {
1570         case F_RDLCK:
1571                 if (!(flags & FMODE_READ))
1572                         goto out;
1573                 break;
1574         case F_WRLCK:
1575                 if (!(flags & FMODE_WRITE))
1576                         goto out;
1577                 break;
1578         case F_UNLCK:
1579                 break;
1580         default:
1581                 error = -EINVAL;
1582                 goto out;
1583         }
1584
1585         error = llu_file_flock(ino, cmd, &fl);
1586         if (error)
1587                 goto out;
1588
1589 out:
1590         return error;
1591 }
1592
1593 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1594 {
1595         struct llu_inode_info *lli = llu_i2info(ino);
1596         long flags;
1597         struct flock *flock;
1598         long err = 0;
1599
1600         liblustre_wait_event(0);
1601         switch (cmd) {
1602         case F_GETFL:
1603                 *rtn = lli->lli_open_flags;
1604                 break;
1605         case F_SETFL:
1606                 flags = va_arg(ap, long);
1607                 flags &= FCNTL_FLMASK;
1608                 if (flags & FCNTL_FLMASK_INVALID) {
1609                         LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1610                                            "the O_NONBLOCK or O_ASYNC flags. "
1611                                            "Please fix your application.\n");
1612                         *rtn = -EINVAL;
1613                         err = EINVAL;
1614                         break;
1615                 }
1616                 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1617                                       (lli->lli_open_flags & ~FCNTL_FLMASK);
1618                 *rtn = 0;
1619                 break;
1620         case F_GETLK:
1621 #ifdef F_GETLK64
1622 #if F_GETLK64 != F_GETLK
1623         case F_GETLK64:
1624 #endif
1625 #endif
1626                 flock = va_arg(ap, struct flock *);
1627                 err = llu_fcntl_getlk(ino, flock);
1628                 *rtn = err? -1: 0;
1629                 break;
1630         case F_SETLK:
1631 #ifdef F_SETLKW64
1632 #if F_SETLKW64 != F_SETLKW
1633         case F_SETLKW64:
1634 #endif
1635 #endif
1636         case F_SETLKW:
1637 #ifdef F_SETLK64
1638 #if F_SETLK64 != F_SETLK
1639         case F_SETLK64:
1640 #endif
1641 #endif
1642                 flock = va_arg(ap, struct flock *);
1643                 err = llu_fcntl_setlk(ino, cmd, flock);
1644                 *rtn = err? -1: 0;
1645                 break;
1646         default:
1647                 CERROR("unsupported fcntl cmd %x\n", cmd);
1648                 *rtn = -ENOSYS;
1649                 err = ENOSYS;
1650                 break;
1651         }
1652
1653         liblustre_wait_event(0);
1654         return err;
1655 }
1656
1657 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1658 {
1659         struct llu_inode_info *lli = llu_i2info(inode);
1660         struct ll_file_data *fd = lli->lli_file_data;
1661         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1662                                                     .end = OBD_OBJECT_EOF}};
1663         struct lustre_handle lockh = { 0 };
1664         struct lov_stripe_md *lsm = lli->lli_smd;
1665         ldlm_error_t err;
1666         int flags = 0;
1667         ENTRY;
1668
1669         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1670                 RETURN(-EINVAL);
1671         }
1672
1673         policy.l_extent.gid = arg;
1674         if (lli->lli_open_flags & O_NONBLOCK)
1675                 flags = LDLM_FL_BLOCK_NOWAIT;
1676
1677         err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1678                               flags);
1679         if (err)
1680                 RETURN(err);
1681
1682         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1683         fd->fd_gid = arg;
1684         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1685
1686         RETURN(0);
1687 }
1688
1689 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1690 {
1691         struct llu_inode_info *lli = llu_i2info(inode);
1692         struct ll_file_data *fd = lli->lli_file_data;
1693         struct lov_stripe_md *lsm = lli->lli_smd;
1694         ldlm_error_t err;
1695         ENTRY;
1696
1697         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1698                 RETURN(-EINVAL);
1699
1700         if (fd->fd_gid != arg)
1701                 RETURN(-EINVAL);
1702
1703         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1704
1705         err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1706         if (err)
1707                 RETURN(err);
1708
1709         fd->fd_gid = 0;
1710         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1711
1712         RETURN(0);
1713 }
1714
1715 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1716 {
1717         struct llu_sb_info *sbi = llu_i2sbi(ino);
1718         struct ptlrpc_request *request = NULL;
1719         struct md_op_data op_data = {{ 0 }};
1720         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1721         int rc = 0;
1722
1723         llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0, 
1724                             LUSTRE_OPC_ANY);
1725
1726         LASSERT(sizeof(lum) == sizeof(*lump));
1727         LASSERT(sizeof(lum.lmm_objects[0]) ==
1728                 sizeof(lump->lmm_objects[0]));
1729         rc = copy_from_user(&lum, lump, sizeof(lum));
1730         if (rc)
1731                 return(-EFAULT);
1732
1733         if (lum.lmm_magic != LOV_USER_MAGIC)
1734                 RETURN(-EINVAL);
1735
1736         if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1737                 lustre_swab_lov_user_md(&lum);
1738
1739         /* swabbing is done in lov_setstripe() on server side */
1740         rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1741                         sizeof(lum), NULL, 0, &request, NULL);
1742         if (rc) {
1743                 ptlrpc_req_finished(request);
1744                 if (rc != -EPERM && rc != -EACCES)
1745                         CERROR("md_setattr fails: rc = %d\n", rc);
1746                 return rc;
1747         }
1748         ptlrpc_req_finished(request);
1749
1750         return rc;
1751 }
1752
1753 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1754                                      struct lov_user_md *lum, int lum_size)
1755 {
1756         struct llu_sb_info *sbi = llu_i2sbi(ino);
1757         struct llu_inode_info *lli = llu_i2info(ino);
1758         struct llu_inode_info *lli2 = NULL;
1759         struct lov_stripe_md *lsm;
1760         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1761         struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1762                 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1763
1764         struct ptlrpc_request *req = NULL;
1765         struct lustre_md md;
1766         struct md_op_data data = {{ 0 }};
1767         struct lustre_handle lockh;
1768         int rc = 0;
1769         ENTRY;
1770
1771         lsm = lli->lli_smd;
1772         if (lsm) {
1773                 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1774                        PFID(&lli->lli_fid));
1775                 return -EEXIST;
1776         }
1777
1778         OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1779         if (!lli2)
1780                 return -ENOMEM;
1781
1782         memcpy(lli2, lli, sizeof(struct llu_inode_info));
1783         lli2->lli_open_count = 0;
1784         lli2->lli_it = NULL;
1785         lli2->lli_file_data = NULL;
1786         lli2->lli_smd = NULL;
1787         lli2->lli_symlink_name = NULL;
1788         ino->i_private = lli2;
1789
1790         llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1791                             LUSTRE_OPC_ANY);
1792
1793         rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1794                         &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1795         if (rc)
1796                 GOTO(out, rc);
1797
1798         req = oit.d.lustre.it_data;
1799         rc = it_open_error(DISP_IT_EXECD, &oit);
1800         if (rc) {
1801                 req->rq_replay = 0;
1802                 GOTO(out, rc);
1803         }
1804
1805         rc = it_open_error(DISP_OPEN_OPEN, &oit);
1806         if (rc) {
1807                 req->rq_replay = 0;
1808                 GOTO(out, rc);
1809         }
1810
1811         rc = md_get_lustre_md(sbi->ll_md_exp, req,
1812                               sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1813         if (rc)
1814                 GOTO(out, rc);
1815
1816         llu_update_inode(ino, md.body, md.lsm);
1817         lli->lli_smd = lli2->lli_smd;
1818         lli2->lli_smd = NULL;
1819
1820         llu_local_open(lli2, &oit);
1821
1822         /* release intent */
1823         if (lustre_handle_is_used(&lockh))
1824                 ldlm_lock_decref(&lockh, LCK_CR);
1825
1826         ptlrpc_req_finished(req);
1827         req = NULL;
1828
1829         rc = llu_file_release(ino);
1830  out:
1831         ino->i_private = lli;
1832         if (lli2)
1833                 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1834         if (req != NULL)
1835                 ptlrpc_req_finished(req);
1836         RETURN(rc);
1837 }
1838
1839 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1840 {
1841         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1842         int rc;
1843         int flags = FMODE_WRITE;
1844         ENTRY;
1845
1846         LASSERT(sizeof(lum) == sizeof(*lump));
1847         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1848         rc = copy_from_user(&lum, lump, sizeof(lum));
1849         if (rc)
1850                 RETURN(-EFAULT);
1851
1852         rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1853         RETURN(rc);
1854 }
1855
1856 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1857 {
1858         struct intnl_stat *st = llu_i2stat(ino);
1859         if (S_ISREG(st->st_mode))
1860                 return llu_lov_file_setstripe(ino, arg);
1861         if (S_ISDIR(st->st_mode))
1862                 return llu_lov_dir_setstripe(ino, arg);
1863
1864         return -EINVAL;
1865 }
1866
1867 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1868 {
1869         struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1870
1871         if (!lsm)
1872                 RETURN(-ENODATA);
1873
1874         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1875                             (void *)arg);
1876 }
1877
1878 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1879                          va_list ap)
1880 {
1881         unsigned long arg;
1882         int rc;
1883
1884         liblustre_wait_event(0);
1885
1886         switch (request) {
1887         case LL_IOC_GROUP_LOCK:
1888                 arg = va_arg(ap, unsigned long);
1889                 rc = llu_get_grouplock(ino, arg);
1890                 break;
1891         case LL_IOC_GROUP_UNLOCK:
1892                 arg = va_arg(ap, unsigned long);
1893                 rc = llu_put_grouplock(ino, arg);
1894                 break;
1895         case LL_IOC_LOV_SETSTRIPE:
1896                 arg = va_arg(ap, unsigned long);
1897                 rc = llu_lov_setstripe(ino, arg);
1898                 break;
1899         case LL_IOC_LOV_GETSTRIPE:
1900                 arg = va_arg(ap, unsigned long);
1901                 rc = llu_lov_getstripe(ino, arg);
1902                 break;
1903         default:
1904                 CERROR("did not support ioctl cmd %lx\n", request);
1905                 rc = -ENOSYS;
1906                 break;
1907         }
1908
1909         liblustre_wait_event(0);
1910         return rc;
1911 }
1912
1913 /*
1914  * we already do syncronous read/write
1915  */
1916 static int llu_iop_sync(struct inode *inode)
1917 {
1918         liblustre_wait_event(0);
1919         return 0;
1920 }
1921
1922 static int llu_iop_datasync(struct inode *inode)
1923 {
1924         liblustre_wait_event(0);
1925         return 0;
1926 }
1927
1928 struct filesys_ops llu_filesys_ops =
1929 {
1930         fsop_gone: llu_fsop_gone,
1931 };
1932
1933 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1934 {
1935         struct inode *inode;
1936         struct lu_fid fid;
1937         struct file_identifier fileid = {&fid, sizeof(fid)};
1938
1939         if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1940             (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1941                 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1942                 LBUG();
1943                 return ERR_PTR(-EPERM);
1944         }
1945
1946         /* try to find existing inode */
1947         fid = md->body->fid1;
1948
1949         inode = _sysio_i_find(fs, &fileid);
1950         if (inode) {
1951                 if (inode->i_zombie/* ||
1952                     lli->lli_st_generation != md->body->generation*/) {
1953                         I_RELE(inode);
1954                 }
1955                 else {
1956                         llu_update_inode(inode, md->body, md->lsm);
1957                         return inode;
1958                 }
1959         }
1960
1961         inode = llu_new_inode(fs, &fid);
1962         if (inode)
1963                 llu_update_inode(inode, md->body, md->lsm);
1964
1965         return inode;
1966 }
1967
1968 static int
1969 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1970 {
1971         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1972         __u32 valsize = sizeof(struct lov_desc);
1973         int rc, easize, def_easize, cookiesize;
1974         struct lov_desc desc;
1975         __u32 stripes;
1976         ENTRY;
1977
1978         rc = obd_get_info(dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
1979                           &valsize, &desc, NULL);
1980         if (rc)
1981                 RETURN(rc);
1982
1983         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1984         lsm.lsm_stripe_count = stripes;
1985         easize = obd_size_diskmd(dt_exp, &lsm);
1986
1987         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1988         def_easize = obd_size_diskmd(dt_exp, &lsm);
1989
1990         cookiesize = stripes * sizeof(struct llog_cookie);
1991
1992         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1993                easize, cookiesize);
1994
1995         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1996         RETURN(rc);
1997 }
1998
1999 static int
2000 llu_fsswop_mount(const char *source,
2001                  unsigned flags,
2002                  const void *data __IS_UNUSED,
2003                  struct pnode *tocover,
2004                  struct mount **mntp)
2005 {
2006         struct filesys *fs;
2007         struct inode *root;
2008         struct pnode_base *rootpb;
2009         struct obd_device *obd;
2010         struct lu_fid rootfid;
2011         struct llu_sb_info *sbi;
2012         struct obd_statfs osfs;
2013         static struct qstr noname = { NULL, 0, 0 };
2014         struct ptlrpc_request *request = NULL;
2015         struct lustre_handle md_conn = {0, };
2016         struct lustre_handle dt_conn = {0, };
2017         struct lustre_md md;
2018         class_uuid_t uuid;
2019         struct config_llog_instance cfg = {0, };
2020         char ll_instance[sizeof(sbi) * 2 + 1];
2021         struct lustre_profile *lprof;
2022         char *zconf_mgsnid, *zconf_profile;
2023         char *osc = NULL, *mdc = NULL;
2024         int async = 1, err = -EINVAL;
2025         struct obd_connect_data ocd = {0,};
2026
2027         ENTRY;
2028
2029         if (ll_parse_mount_target(source,
2030                                   &zconf_mgsnid,
2031                                   &zconf_profile)) {
2032                 CERROR("mal-formed target %s\n", source);
2033                 RETURN(err);
2034         }
2035         if (!zconf_mgsnid || !zconf_profile) {
2036                 printf("Liblustre: invalid target %s\n", source);
2037                 RETURN(err);
2038         }
2039         /* allocate & initialize sbi */
2040         OBD_ALLOC(sbi, sizeof(*sbi));
2041         if (!sbi)
2042                 RETURN(-ENOMEM);
2043
2044         CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
2045         ll_generate_random_uuid(uuid);
2046         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2047
2048         /* generate a string unique to this super, let's try
2049          the address of the super itself.*/
2050         sprintf(ll_instance, "%p", sbi);
2051
2052         /* retrive & parse config log */
2053         cfg.cfg_instance = ll_instance;
2054         cfg.cfg_uuid = sbi->ll_sb_uuid;
2055         err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2056         if (err < 0) {
2057                 CERROR("Unable to process log: %s\n", zconf_profile);
2058                 GOTO(out_free, err);
2059         }
2060
2061         lprof = class_get_profile(zconf_profile);
2062         if (lprof == NULL) {
2063                 CERROR("No profile found: %s\n", zconf_profile);
2064                 GOTO(out_free, err = -EINVAL);
2065         }
2066         OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2067         sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2068
2069         OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2070         sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2071
2072         if (!osc) {
2073                 CERROR("no osc\n");
2074                 GOTO(out_free, err = -EINVAL);
2075         }
2076         if (!mdc) {
2077                 CERROR("no mdc\n");
2078                 GOTO(out_free, err = -EINVAL);
2079         }
2080
2081         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2082         if (!fs) {
2083                 err = -ENOMEM;
2084                 goto out_free;
2085         }
2086
2087         obd = class_name2obd(mdc);
2088         if (!obd) {
2089                 CERROR("MDC %s: not setup or attached\n", mdc);
2090                 GOTO(out_free, err = -EINVAL);
2091         }
2092         obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2093                            sizeof(async), &async, NULL);
2094
2095         ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2096                                 OBD_CONNECT_FID | OBD_CONNECT_AT;
2097 #ifdef LIBLUSTRE_POSIX_ACL
2098         ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2099 #endif
2100         ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2101         ocd.ocd_version = LUSTRE_VERSION_CODE;
2102
2103         /* setup mdc */
2104         err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2105         if (err) {
2106                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2107                 GOTO(out_free, err);
2108         }
2109         sbi->ll_md_exp = class_conn2export(&md_conn);
2110
2111         err = obd_statfs(obd, &osfs, 100000000, 0);
2112         if (err)
2113                 GOTO(out_md, err);
2114
2115         /*
2116          * FIXME fill fs stat data into sbi here!!! FIXME
2117          */
2118
2119         /* setup osc */
2120         obd = class_name2obd(osc);
2121         if (!obd) {
2122                 CERROR("OSC %s: not setup or attached\n", osc);
2123                 GOTO(out_md, err = -EINVAL);
2124         }
2125         obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2126                            sizeof(async), &async, NULL);
2127
2128         obd->obd_upcall.onu_owner = &sbi->ll_lco;
2129         obd->obd_upcall.onu_upcall = ll_ocd_update;
2130
2131         ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2132                                 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2133                                 OBD_CONNECT_FID | OBD_CONNECT_AT;
2134         ocd.ocd_version = LUSTRE_VERSION_CODE;
2135         err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2136         if (err) {
2137                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2138                 GOTO(out_md, err);
2139         }
2140         sbi->ll_dt_exp = class_conn2export(&dt_conn);
2141         sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2142
2143         err = obd_register_lock_cancel_cb(sbi->ll_dt_exp,
2144                                           llu_extent_lock_cancel_cb);
2145         if (err) {
2146                 CERROR("cannot register lock cancel callback: rc = %d\n", err);
2147                 GOTO(out_dt, err);
2148         }
2149
2150         llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2151
2152         err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2153         if (err) {
2154                 CERROR("cannot mds_connect: rc = %d\n", err);
2155                 GOTO(out_lock_cn_cb, err);
2156         }
2157         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2158         sbi->ll_root_fid = rootfid;
2159
2160         /* fetch attr of root inode */
2161         err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2162                          OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2163         if (err) {
2164                 CERROR("md_getattr failed for root: rc = %d\n", err);
2165                 GOTO(out_lock_cn_cb, err);
2166         }
2167
2168         err = md_get_lustre_md(sbi->ll_md_exp, request,
2169                                sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2170         if (err) {
2171                 CERROR("failed to understand root inode md: rc = %d\n",err);
2172                 GOTO(out_request, err);
2173         }
2174
2175         LASSERT(fid_is_sane(&sbi->ll_root_fid));
2176
2177         root = llu_iget(fs, &md);
2178         if (!root || IS_ERR(root)) {
2179                 CERROR("fail to generate root inode\n");
2180                 GOTO(out_request, err = -EBADF);
2181         }
2182
2183         /*
2184          * Generate base path-node for root.
2185          */
2186         rootpb = _sysio_pb_new(&noname, NULL, root);
2187         if (!rootpb) {
2188                 err = -ENOMEM;
2189                 goto out_inode;
2190         }
2191
2192         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2193         if (err) {
2194                 _sysio_pb_gone(rootpb);
2195                 goto out_inode;
2196         }
2197
2198         ptlrpc_req_finished(request);
2199
2200         CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2201         liblustre_wait_idle();
2202
2203         return 0;
2204
2205 out_inode:
2206         _sysio_i_gone(root);
2207 out_request:
2208         ptlrpc_req_finished(request);
2209 out_lock_cn_cb:
2210         obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
2211                                       llu_extent_lock_cancel_cb);
2212 out_dt:
2213         obd_disconnect(sbi->ll_dt_exp);
2214 out_md:
2215         obd_disconnect(sbi->ll_md_exp);
2216 out_free:
2217         if (osc)
2218                 OBD_FREE(osc, strlen(osc) + 1);
2219         if (mdc)
2220                 OBD_FREE(mdc, strlen(mdc) + 1);
2221         OBD_FREE(sbi, sizeof(*sbi));
2222         liblustre_wait_idle();
2223         return err;
2224 }
2225
2226 struct fssw_ops llu_fssw_ops = {
2227         llu_fsswop_mount
2228 };
2229
2230 static struct inode_ops llu_inode_ops = {
2231         inop_lookup:    llu_iop_lookup,
2232         inop_getattr:   llu_iop_getattr,
2233         inop_setattr:   llu_iop_setattr,
2234         inop_filldirentries:     llu_iop_filldirentries,
2235         inop_mkdir:     llu_iop_mkdir_raw,
2236         inop_rmdir:     llu_iop_rmdir_raw,
2237         inop_symlink:   llu_iop_symlink_raw,
2238         inop_readlink:  llu_iop_readlink,
2239         inop_open:      llu_iop_open,
2240         inop_close:     llu_iop_close,
2241         inop_link:      llu_iop_link_raw,
2242         inop_unlink:    llu_iop_unlink_raw,
2243         inop_rename:    llu_iop_rename_raw,
2244         inop_pos:       llu_iop_pos,
2245         inop_read:      llu_iop_read,
2246         inop_write:     llu_iop_write,
2247         inop_iodone:    llu_iop_iodone,
2248         inop_fcntl:     llu_iop_fcntl,
2249         inop_sync:      llu_iop_sync,
2250         inop_datasync:  llu_iop_datasync,
2251         inop_ioctl:     llu_iop_ioctl,
2252         inop_mknod:     llu_iop_mknod_raw,
2253 #ifdef _HAVE_STATVFS
2254         inop_statvfs:   llu_iop_statvfs,
2255 #endif
2256         inop_gone:      llu_iop_gone,
2257 };