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