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