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