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