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