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