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