Whamcloud - gitweb
d7d37cf44ad00d858db1653d0a524ce2316bd91e
[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         switch (lum.lmm_magic) {
1734         case LOV_USER_MAGIC_V1: {
1735                 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1736                         lustre_swab_lov_user_md_v1(&lum);
1737                 break;
1738                 }
1739         case LOV_USER_MAGIC_V3: {
1740                 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1741                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1742                 break;
1743                 }
1744         default: {
1745                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1746                                 " %#08x != %#08x nor %#08x\n",
1747                                 lum.lmm_magic, LOV_USER_MAGIC_V1,
1748                                 LOV_USER_MAGIC_V3);
1749                 RETURN(-EINVAL);
1750         }
1751         }
1752
1753         /* swabbing is done in lov_setstripe() on server side */
1754         rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1755                         sizeof(lum), NULL, 0, &request, NULL);
1756         if (rc) {
1757                 ptlrpc_req_finished(request);
1758                 if (rc != -EPERM && rc != -EACCES)
1759                         CERROR("md_setattr fails: rc = %d\n", rc);
1760                 return rc;
1761         }
1762         ptlrpc_req_finished(request);
1763
1764         return rc;
1765 }
1766
1767 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1768                                      struct lov_user_md *lum, int lum_size)
1769 {
1770         struct llu_sb_info *sbi = llu_i2sbi(ino);
1771         struct llu_inode_info *lli = llu_i2info(ino);
1772         struct llu_inode_info *lli2 = NULL;
1773         struct lov_stripe_md *lsm;
1774         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1775         struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1776                 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1777
1778         struct ptlrpc_request *req = NULL;
1779         struct lustre_md md;
1780         struct md_op_data data = {{ 0 }};
1781         struct lustre_handle lockh;
1782         int rc = 0;
1783         ENTRY;
1784
1785         lsm = lli->lli_smd;
1786         if (lsm) {
1787                 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1788                        PFID(&lli->lli_fid));
1789                 return -EEXIST;
1790         }
1791
1792         OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1793         if (!lli2)
1794                 return -ENOMEM;
1795
1796         memcpy(lli2, lli, sizeof(struct llu_inode_info));
1797         lli2->lli_open_count = 0;
1798         lli2->lli_it = NULL;
1799         lli2->lli_file_data = NULL;
1800         lli2->lli_smd = NULL;
1801         lli2->lli_symlink_name = NULL;
1802         ino->i_private = lli2;
1803
1804         llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1805                             LUSTRE_OPC_ANY);
1806
1807         rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1808                         &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1809         if (rc)
1810                 GOTO(out, rc);
1811
1812         req = oit.d.lustre.it_data;
1813         rc = it_open_error(DISP_IT_EXECD, &oit);
1814         if (rc) {
1815                 req->rq_replay = 0;
1816                 GOTO(out, rc);
1817         }
1818
1819         rc = it_open_error(DISP_OPEN_OPEN, &oit);
1820         if (rc) {
1821                 req->rq_replay = 0;
1822                 GOTO(out, rc);
1823         }
1824
1825         rc = md_get_lustre_md(sbi->ll_md_exp, req,
1826                               sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1827         if (rc)
1828                 GOTO(out, rc);
1829
1830         llu_update_inode(ino, md.body, md.lsm);
1831         lli->lli_smd = lli2->lli_smd;
1832         lli2->lli_smd = NULL;
1833
1834         llu_local_open(lli2, &oit);
1835
1836         /* release intent */
1837         if (lustre_handle_is_used(&lockh))
1838                 ldlm_lock_decref(&lockh, LCK_CR);
1839
1840         ptlrpc_req_finished(req);
1841         req = NULL;
1842
1843         rc = llu_file_release(ino);
1844  out:
1845         ino->i_private = lli;
1846         if (lli2)
1847                 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1848         if (req != NULL)
1849                 ptlrpc_req_finished(req);
1850         RETURN(rc);
1851 }
1852
1853 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1854 {
1855         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1856         int rc;
1857         int flags = FMODE_WRITE;
1858         ENTRY;
1859
1860         LASSERT(sizeof(lum) == sizeof(*lump));
1861         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1862         rc = copy_from_user(&lum, lump, sizeof(lum));
1863         if (rc)
1864                 RETURN(-EFAULT);
1865
1866         rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1867         RETURN(rc);
1868 }
1869
1870 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1871 {
1872         struct intnl_stat *st = llu_i2stat(ino);
1873         if (S_ISREG(st->st_mode))
1874                 return llu_lov_file_setstripe(ino, arg);
1875         if (S_ISDIR(st->st_mode))
1876                 return llu_lov_dir_setstripe(ino, arg);
1877
1878         return -EINVAL;
1879 }
1880
1881 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1882 {
1883         struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1884
1885         if (!lsm)
1886                 RETURN(-ENODATA);
1887
1888         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1889                             (void *)arg);
1890 }
1891
1892 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1893                          va_list ap)
1894 {
1895         unsigned long arg;
1896         int rc;
1897
1898         liblustre_wait_event(0);
1899
1900         switch (request) {
1901         case LL_IOC_GROUP_LOCK:
1902                 arg = va_arg(ap, unsigned long);
1903                 rc = llu_get_grouplock(ino, arg);
1904                 break;
1905         case LL_IOC_GROUP_UNLOCK:
1906                 arg = va_arg(ap, unsigned long);
1907                 rc = llu_put_grouplock(ino, arg);
1908                 break;
1909         case LL_IOC_LOV_SETSTRIPE:
1910                 arg = va_arg(ap, unsigned long);
1911                 rc = llu_lov_setstripe(ino, arg);
1912                 break;
1913         case LL_IOC_LOV_GETSTRIPE:
1914                 arg = va_arg(ap, unsigned long);
1915                 rc = llu_lov_getstripe(ino, arg);
1916                 break;
1917         default:
1918                 CERROR("did not support ioctl cmd %lx\n", request);
1919                 rc = -ENOSYS;
1920                 break;
1921         }
1922
1923         liblustre_wait_event(0);
1924         return rc;
1925 }
1926
1927 /*
1928  * we already do syncronous read/write
1929  */
1930 static int llu_iop_sync(struct inode *inode)
1931 {
1932         liblustre_wait_event(0);
1933         return 0;
1934 }
1935
1936 static int llu_iop_datasync(struct inode *inode)
1937 {
1938         liblustre_wait_event(0);
1939         return 0;
1940 }
1941
1942 struct filesys_ops llu_filesys_ops =
1943 {
1944         fsop_gone: llu_fsop_gone,
1945 };
1946
1947 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1948 {
1949         struct inode *inode;
1950         struct lu_fid fid;
1951         struct file_identifier fileid = {&fid, sizeof(fid)};
1952
1953         if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1954             (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1955                 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1956                 LBUG();
1957                 return ERR_PTR(-EPERM);
1958         }
1959
1960         /* try to find existing inode */
1961         fid = md->body->fid1;
1962
1963         inode = _sysio_i_find(fs, &fileid);
1964         if (inode) {
1965                 if (inode->i_zombie/* ||
1966                     lli->lli_st_generation != md->body->generation*/) {
1967                         I_RELE(inode);
1968                 }
1969                 else {
1970                         llu_update_inode(inode, md->body, md->lsm);
1971                         return inode;
1972                 }
1973         }
1974
1975         inode = llu_new_inode(fs, &fid);
1976         if (inode)
1977                 llu_update_inode(inode, md->body, md->lsm);
1978
1979         return inode;
1980 }
1981
1982 static int
1983 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1984 {
1985         /* even if default lov is LOV_MAGIC_V1 we use LOV_MAGIC_V3
1986          * to be sure buffer are large enough */
1987         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 };
1988         __u32 valsize = sizeof(struct lov_desc);
1989         int rc, easize, def_easize, cookiesize;
1990         struct lov_desc desc;
1991         __u32 stripes;
1992         ENTRY;
1993
1994         rc = obd_get_info(dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
1995                           &valsize, &desc, NULL);
1996         if (rc)
1997                 RETURN(rc);
1998
1999         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
2000         lsm.lsm_stripe_count = stripes;
2001         easize = obd_size_diskmd(dt_exp, &lsm);
2002
2003         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
2004         def_easize = obd_size_diskmd(dt_exp, &lsm);
2005
2006         cookiesize = stripes * sizeof(struct llog_cookie);
2007
2008         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
2009                easize, cookiesize);
2010
2011         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
2012         RETURN(rc);
2013 }
2014
2015 static int
2016 llu_fsswop_mount(const char *source,
2017                  unsigned flags,
2018                  const void *data __IS_UNUSED,
2019                  struct pnode *tocover,
2020                  struct mount **mntp)
2021 {
2022         struct filesys *fs;
2023         struct inode *root;
2024         struct pnode_base *rootpb;
2025         struct obd_device *obd;
2026         struct lu_fid rootfid;
2027         struct llu_sb_info *sbi;
2028         struct obd_statfs osfs;
2029         static struct qstr noname = { NULL, 0, 0 };
2030         struct ptlrpc_request *request = NULL;
2031         struct lustre_handle md_conn = {0, };
2032         struct lustre_handle dt_conn = {0, };
2033         struct lustre_md md;
2034         class_uuid_t uuid;
2035         struct config_llog_instance cfg = {0, };
2036         char ll_instance[sizeof(sbi) * 2 + 1];
2037         struct lustre_profile *lprof;
2038         char *zconf_mgsnid, *zconf_profile;
2039         char *osc = NULL, *mdc = NULL;
2040         int async = 1, err = -EINVAL;
2041         struct obd_connect_data ocd = {0,};
2042
2043         ENTRY;
2044
2045         if (ll_parse_mount_target(source,
2046                                   &zconf_mgsnid,
2047                                   &zconf_profile)) {
2048                 CERROR("mal-formed target %s\n", source);
2049                 RETURN(err);
2050         }
2051         if (!zconf_mgsnid || !zconf_profile) {
2052                 printf("Liblustre: invalid target %s\n", source);
2053                 RETURN(err);
2054         }
2055         /* allocate & initialize sbi */
2056         OBD_ALLOC(sbi, sizeof(*sbi));
2057         if (!sbi)
2058                 RETURN(-ENOMEM);
2059
2060         CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
2061         ll_generate_random_uuid(uuid);
2062         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2063
2064         /* generate a string unique to this super, let's try
2065          the address of the super itself.*/
2066         sprintf(ll_instance, "%p", sbi);
2067
2068         /* retrive & parse config log */
2069         cfg.cfg_instance = ll_instance;
2070         cfg.cfg_uuid = sbi->ll_sb_uuid;
2071         err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2072         if (err < 0) {
2073                 CERROR("Unable to process log: %s\n", zconf_profile);
2074                 GOTO(out_free, err);
2075         }
2076
2077         lprof = class_get_profile(zconf_profile);
2078         if (lprof == NULL) {
2079                 CERROR("No profile found: %s\n", zconf_profile);
2080                 GOTO(out_free, err = -EINVAL);
2081         }
2082         OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2083         sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2084
2085         OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2086         sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2087
2088         if (!osc) {
2089                 CERROR("no osc\n");
2090                 GOTO(out_free, err = -EINVAL);
2091         }
2092         if (!mdc) {
2093                 CERROR("no mdc\n");
2094                 GOTO(out_free, err = -EINVAL);
2095         }
2096
2097         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2098         if (!fs) {
2099                 err = -ENOMEM;
2100                 goto out_free;
2101         }
2102
2103         obd = class_name2obd(mdc);
2104         if (!obd) {
2105                 CERROR("MDC %s: not setup or attached\n", mdc);
2106                 GOTO(out_free, err = -EINVAL);
2107         }
2108         obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2109                            sizeof(async), &async, NULL);
2110
2111         ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2112                                 OBD_CONNECT_FID | OBD_CONNECT_AT;
2113 #ifdef LIBLUSTRE_POSIX_ACL
2114         ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2115 #endif
2116         ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2117         ocd.ocd_version = LUSTRE_VERSION_CODE;
2118
2119         /* setup mdc */
2120         err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2121         if (err) {
2122                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2123                 GOTO(out_free, err);
2124         }
2125         sbi->ll_md_exp = class_conn2export(&md_conn);
2126
2127         err = obd_statfs(obd, &osfs, 100000000, 0);
2128         if (err)
2129                 GOTO(out_md, err);
2130
2131         /*
2132          * FIXME fill fs stat data into sbi here!!! FIXME
2133          */
2134
2135         /* setup osc */
2136         obd = class_name2obd(osc);
2137         if (!obd) {
2138                 CERROR("OSC %s: not setup or attached\n", osc);
2139                 GOTO(out_md, err = -EINVAL);
2140         }
2141         obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2142                            sizeof(async), &async, NULL);
2143
2144         obd->obd_upcall.onu_owner = &sbi->ll_lco;
2145         obd->obd_upcall.onu_upcall = ll_ocd_update;
2146
2147         ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2148                                 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2149                                 OBD_CONNECT_FID | OBD_CONNECT_AT;
2150         ocd.ocd_version = LUSTRE_VERSION_CODE;
2151         err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2152         if (err) {
2153                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2154                 GOTO(out_md, err);
2155         }
2156         sbi->ll_dt_exp = class_conn2export(&dt_conn);
2157         sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2158
2159         err = obd_register_lock_cancel_cb(sbi->ll_dt_exp,
2160                                           llu_extent_lock_cancel_cb);
2161         if (err) {
2162                 CERROR("cannot register lock cancel callback: rc = %d\n", err);
2163                 GOTO(out_dt, err);
2164         }
2165
2166         llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2167
2168         err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2169         if (err) {
2170                 CERROR("cannot mds_connect: rc = %d\n", err);
2171                 GOTO(out_lock_cn_cb, err);
2172         }
2173         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2174         sbi->ll_root_fid = rootfid;
2175
2176         /* fetch attr of root inode */
2177         err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2178                          OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2179         if (err) {
2180                 CERROR("md_getattr failed for root: rc = %d\n", err);
2181                 GOTO(out_lock_cn_cb, err);
2182         }
2183
2184         err = md_get_lustre_md(sbi->ll_md_exp, request,
2185                                sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2186         if (err) {
2187                 CERROR("failed to understand root inode md: rc = %d\n",err);
2188                 GOTO(out_request, err);
2189         }
2190
2191         LASSERT(fid_is_sane(&sbi->ll_root_fid));
2192
2193         root = llu_iget(fs, &md);
2194         if (!root || IS_ERR(root)) {
2195                 CERROR("fail to generate root inode\n");
2196                 GOTO(out_request, err = -EBADF);
2197         }
2198
2199         /*
2200          * Generate base path-node for root.
2201          */
2202         rootpb = _sysio_pb_new(&noname, NULL, root);
2203         if (!rootpb) {
2204                 err = -ENOMEM;
2205                 goto out_inode;
2206         }
2207
2208         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2209         if (err) {
2210                 _sysio_pb_gone(rootpb);
2211                 goto out_inode;
2212         }
2213
2214         ptlrpc_req_finished(request);
2215
2216         CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2217         liblustre_wait_idle();
2218
2219         return 0;
2220
2221 out_inode:
2222         _sysio_i_gone(root);
2223 out_request:
2224         ptlrpc_req_finished(request);
2225 out_lock_cn_cb:
2226         obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
2227                                       llu_extent_lock_cancel_cb);
2228 out_dt:
2229         obd_disconnect(sbi->ll_dt_exp);
2230 out_md:
2231         obd_disconnect(sbi->ll_md_exp);
2232 out_free:
2233         if (osc)
2234                 OBD_FREE(osc, strlen(osc) + 1);
2235         if (mdc)
2236                 OBD_FREE(mdc, strlen(mdc) + 1);
2237         OBD_FREE(sbi, sizeof(*sbi));
2238         liblustre_wait_idle();
2239         return err;
2240 }
2241
2242 struct fssw_ops llu_fssw_ops = {
2243         llu_fsswop_mount
2244 };
2245
2246 static struct inode_ops llu_inode_ops = {
2247         inop_lookup:    llu_iop_lookup,
2248         inop_getattr:   llu_iop_getattr,
2249         inop_setattr:   llu_iop_setattr,
2250         inop_filldirentries:     llu_iop_filldirentries,
2251         inop_mkdir:     llu_iop_mkdir_raw,
2252         inop_rmdir:     llu_iop_rmdir_raw,
2253         inop_symlink:   llu_iop_symlink_raw,
2254         inop_readlink:  llu_iop_readlink,
2255         inop_open:      llu_iop_open,
2256         inop_close:     llu_iop_close,
2257         inop_link:      llu_iop_link_raw,
2258         inop_unlink:    llu_iop_unlink_raw,
2259         inop_rename:    llu_iop_rename_raw,
2260         inop_pos:       llu_iop_pos,
2261         inop_read:      llu_iop_read,
2262         inop_write:     llu_iop_write,
2263         inop_iodone:    llu_iop_iodone,
2264         inop_fcntl:     llu_iop_fcntl,
2265         inop_sync:      llu_iop_sync,
2266         inop_datasync:  llu_iop_datasync,
2267         inop_ioctl:     llu_iop_ioctl,
2268         inop_mknod:     llu_iop_mknod_raw,
2269 #ifdef _HAVE_STATVFS
2270         inop_statvfs:   llu_iop_statvfs,
2271 #endif
2272         inop_gone:      llu_iop_gone,
2273 };