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