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