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