Whamcloud - gitweb
- landed b_hd_cray_merge3
[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  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/fcntl.h>
33 #include <sys/queue.h>
34
35 #ifndef __CYGWIN__
36 # include <sys/statvfs.h>
37 #else
38 # include <sys/statfs.h>
39 #endif
40
41 #ifdef HAVE_XTIO_H
42 #include <xtio.h>
43 #endif
44 #include <sysio.h>
45 #include <mount.h>
46 #include <inode.h>
47 #include <fs.h>
48 #ifdef HAVE_FILE_H
49 #include <file.h>
50 #endif
51
52 #undef LIST_HEAD
53 #include "llite_lib.h"
54
55 #ifndef MAY_EXEC
56 #  define MAY_EXEC        1
57 #  define MAY_WRITE       2
58 #  define MAY_READ        4
59 #endif
60
61 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
62
63 static int ll_permission(struct inode *inode, int mask)
64 {
65         struct llu_inode_info *lli = llu_i2info(inode);
66         mode_t mode = lli->lli_st_mode;
67
68         if (current->fsuid == lli->lli_st_uid)
69                 mode >>= 6;
70         else if (in_group_p(lli->lli_st_gid))
71                 mode >>= 3;
72
73         if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
74                 return 0;
75
76         if ((mask & (MAY_READ|MAY_WRITE)) ||
77             (lli->lli_st_mode & S_IXUGO))
78                 if (capable(CAP_DAC_OVERRIDE))
79                         return 0;
80
81         if (mask == MAY_READ ||
82             (S_ISDIR(lli->lli_st_mode) && !(mask & MAY_WRITE))) {
83                 if (capable(CAP_DAC_READ_SEARCH))
84                         return 0;
85         }
86
87         return -EACCES;
88 }
89
90 static void llu_fsop_gone(struct filesys *fs)
91 {
92         struct llu_sb_info *sbi = (struct llu_sb_info *)fs->fs_private;
93         struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
94         struct lustre_cfg lcfg;
95         int next = 0;
96         ENTRY;
97
98         list_del(&sbi->ll_conn_chain);
99         obd_disconnect(sbi->ll_dt_exp, 0);
100         obd_disconnect(sbi->ll_md_exp, 0);
101
102         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
103         {
104                 int err;
105
106                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
107                 err = class_process_config(&lcfg);
108                 if (err) {
109                         CERROR("cleanup failed: %s\n", obd->obd_name);
110                 }
111
112                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
113                 err = class_process_config(&lcfg);
114                 if (err) {
115                         CERROR("detach failed: %s\n", obd->obd_name);
116                 }
117         }
118
119         obd_disconnect(sbi->ll_md_exp, 0);
120         OBD_FREE(sbi, sizeof(*sbi));
121         EXIT;
122 }
123
124 struct inode_ops llu_inode_ops;
125
126 void llu_update_inode(struct inode *inode, struct mds_body *body,
127                       struct lov_stripe_md *lsm)
128 {
129         struct llu_inode_info *lli = llu_i2info(inode);
130
131         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
132         if (lsm != NULL) {
133                 if (lli->lli_smd == NULL) {
134                         lli->lli_smd = lsm;
135                         lli->lli_maxbytes = lsm->lsm_maxbytes;
136                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
137                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
138                 } else {
139                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
140                                 CERROR("lsm mismatch for inode %ld\n",
141                                        lli->lli_st_ino);
142                                 LBUG();
143                         }
144                 }
145         }
146
147         id_assign_fid(&lli->lli_id, &body->id1);
148         
149         if ((body->valid & OBD_MD_FLID) || (body->valid & OBD_MD_FLGENER))
150                 id_assign_stc(&lli->lli_id, &body->id1);
151         if (body->valid & OBD_MD_FLID)
152                 lli->lli_st_ino = id_ino(&body->id1);
153         if (body->valid & OBD_MD_FLGENER)
154                 lli->lli_st_generation = id_gen(&body->id1);
155
156         if (body->valid & OBD_MD_FLATIME)
157                 LTIME_S(lli->lli_st_atime) = body->atime;
158         if (body->valid & OBD_MD_FLMTIME)
159                 LTIME_S(lli->lli_st_mtime) = body->mtime;
160         if (body->valid & OBD_MD_FLCTIME)
161                 LTIME_S(lli->lli_st_ctime) = body->ctime;
162         if (body->valid & OBD_MD_FLMODE)
163                 lli->lli_st_mode = (lli->lli_st_mode & S_IFMT)|(body->mode & ~S_IFMT);
164         if (body->valid & OBD_MD_FLTYPE)
165                 lli->lli_st_mode = (lli->lli_st_mode & ~S_IFMT)|(body->mode & S_IFMT);
166         if (body->valid & OBD_MD_FLUID)
167                 lli->lli_st_uid = body->uid;
168         if (body->valid & OBD_MD_FLGID)
169                 lli->lli_st_gid = body->gid;
170         if (body->valid & OBD_MD_FLFLAGS)
171                 lli->lli_st_flags = body->flags;
172         if (body->valid & OBD_MD_FLNLINK)
173                 lli->lli_st_nlink = body->nlink;
174         if (body->valid & OBD_MD_FLRDEV)
175                 lli->lli_st_rdev = body->rdev;
176         if (body->valid & OBD_MD_FLSIZE)
177                 lli->lli_st_size = body->size;
178         if (body->valid & OBD_MD_FLBLOCKS)
179                 lli->lli_st_blocks = body->blocks;
180 }
181
182 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_valid valid)
183 {
184         struct llu_inode_info *lli = llu_i2info(dst);
185
186         valid &= src->o_valid;
187
188         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
189                 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
190                        src->o_valid, 
191                        LTIME_S(lli->lli_st_mtime), LTIME_S(lli->lli_st_ctime),
192                        (long)src->o_mtime, (long)src->o_ctime);
193
194         if (valid & OBD_MD_FLATIME)
195                 LTIME_S(lli->lli_st_atime) = src->o_atime;
196         if (valid & OBD_MD_FLMTIME)
197                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
198         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
199                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
200         if (valid & OBD_MD_FLSIZE)
201                 lli->lli_st_size = src->o_size;
202         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
203                 lli->lli_st_blocks = src->o_blocks;
204         if (valid & OBD_MD_FLBLKSZ)
205                 lli->lli_st_blksize = src->o_blksize;
206         if (valid & OBD_MD_FLTYPE)
207                 lli->lli_st_mode = (lli->lli_st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
208         if (valid & OBD_MD_FLMODE)
209                 lli->lli_st_mode = (lli->lli_st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
210         if (valid & OBD_MD_FLUID)
211                 lli->lli_st_uid = src->o_uid;
212         if (valid & OBD_MD_FLGID)
213                 lli->lli_st_gid = src->o_gid;
214         if (valid & OBD_MD_FLFLAGS)
215                 lli->lli_st_flags = src->o_flags;
216         if (valid & OBD_MD_FLGENER)
217                 lli->lli_st_generation = src->o_generation;
218 }
219
220 #define S_IRWXUGO       (S_IRWXU|S_IRWXG|S_IRWXO)
221 #define S_IALLUGO       (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
222
223 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_valid valid)
224 {
225         struct llu_inode_info *lli = llu_i2info(src);
226         obd_valid newvalid = 0;
227
228         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
229                 CDEBUG(D_INODE, "valid "LPX64", new time %lu/%lu\n",
230                        valid, LTIME_S(lli->lli_st_mtime), 
231                        LTIME_S(lli->lli_st_ctime));
232
233         if (valid & OBD_MD_FLATIME) {
234                 dst->o_atime = LTIME_S(lli->lli_st_atime);
235                 newvalid |= OBD_MD_FLATIME;
236         }
237         if (valid & OBD_MD_FLMTIME) {
238                 dst->o_mtime = LTIME_S(lli->lli_st_mtime);
239                 newvalid |= OBD_MD_FLMTIME;
240         }
241         if (valid & OBD_MD_FLCTIME) {
242                 dst->o_ctime = LTIME_S(lli->lli_st_ctime);
243                 newvalid |= OBD_MD_FLCTIME;
244         }
245         if (valid & OBD_MD_FLSIZE) {
246                 dst->o_size = lli->lli_st_size;
247                 newvalid |= OBD_MD_FLSIZE;
248         }
249         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
250                 dst->o_blocks = lli->lli_st_blocks;
251                 newvalid |= OBD_MD_FLBLOCKS;
252         }
253         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
254                 dst->o_blksize = lli->lli_st_blksize;
255                 newvalid |= OBD_MD_FLBLKSZ;
256         }
257         if (valid & OBD_MD_FLTYPE) {
258                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(lli->lli_st_mode & S_IFMT);
259                 newvalid |= OBD_MD_FLTYPE;
260         }
261         if (valid & OBD_MD_FLMODE) {
262                 dst->o_mode = (dst->o_mode & S_IFMT)|(lli->lli_st_mode & S_IALLUGO);
263                 newvalid |= OBD_MD_FLMODE;
264         }
265         if (valid & OBD_MD_FLUID) {
266                 dst->o_uid = lli->lli_st_uid;
267                 newvalid |= OBD_MD_FLUID;
268         }
269         if (valid & OBD_MD_FLGID) {
270                 dst->o_gid = lli->lli_st_gid;
271                 newvalid |= OBD_MD_FLGID;
272         }
273         if (valid & OBD_MD_FLFLAGS) {
274                 dst->o_flags = lli->lli_st_flags;
275                 newvalid |= OBD_MD_FLFLAGS;
276         }
277         if (valid & OBD_MD_FLGENER) {
278                 dst->o_generation = lli->lli_st_generation;
279                 newvalid |= OBD_MD_FLGENER;
280         }
281
282         dst->o_valid |= newvalid;
283 }
284
285 /*
286  * really does the getattr on the inode and updates its fields
287  */
288 int llu_inode_getattr(struct inode *inode, struct lov_stripe_md *lsm)
289 {
290         struct llu_inode_info *lli = llu_i2info(inode);
291         struct obd_export *exp = llu_i2dtexp(inode);
292         struct ptlrpc_request_set *set;
293         struct obdo oa;
294         obd_valid refresh_valid;
295         int rc;
296         ENTRY;
297
298         LASSERT(lsm);
299         LASSERT(lli);
300
301         memset(&oa, 0, sizeof oa);
302         oa.o_id = lsm->lsm_object_id;
303         oa.o_mode = S_IFREG;
304         oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
305                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
306                 OBD_MD_FLCTIME;
307
308         set = ptlrpc_prep_set();
309         if (set == NULL) {
310                 CERROR ("ENOMEM allocing request set\n");
311                 rc = -ENOMEM;
312         } else {
313                 rc = obd_getattr_async(exp, &oa, lsm, set);
314                 if (rc == 0)
315                         rc = ptlrpc_set_wait(set);
316                 ptlrpc_set_destroy(set);
317         }
318         if (rc)
319                 RETURN(rc);
320
321         refresh_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
322                         OBD_MD_FLCTIME | OBD_MD_FLSIZE;
323
324         /* We set this flag in commit write as we extend the file size.  When
325          * the bit is set and the lock is canceled that covers the file size,
326          * we clear the bit.  This is enough to protect the window where our
327          * local size extension is needed for writeback.  However, it relies on
328          * behaviour that won't be true in the near future.  This assumes that
329          * all getattr callers get extent locks, which they currnetly do.  It
330          * also assumes that we only send discarding asts for {0,eof} truncates
331          * as is currently the case.  This will have to be replaced by the
332          * proper eoc communication between clients and the ost, which is on
333          * its way. */
334         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
335                 if (oa.o_size < lli->lli_st_size)
336                         refresh_valid &= ~OBD_MD_FLSIZE;
337                 else 
338                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
339         }
340
341         obdo_refresh_inode(inode, &oa, refresh_valid);
342
343         RETURN(0);
344 }
345
346 static struct inode* llu_new_inode(struct filesys *fs,
347                                    struct lustre_id *id)
348 {
349         struct inode *inode;
350         struct llu_inode_info *lli;
351
352         OBD_ALLOC(lli, sizeof(*lli));
353         if (!lli)
354                 return NULL;
355
356         /* initialize lli here */
357         lli->lli_sbi = llu_fs2sbi(fs);
358         lli->lli_smd = NULL;
359         lli->lli_symlink_name = NULL;
360         lli->lli_flags = 0;
361         lli->lli_maxbytes = (__u64)(~0UL);
362         lli->lli_file_data = NULL;
363
364         lli->lli_sysio_fid.fid_data = &lli->lli_id;
365         lli->lli_sysio_fid.fid_len = sizeof(lli->lli_id);
366
367         memcpy(&lli->lli_id, id, sizeof(*id));
368
369         /* file identifier is needed by functions like _sysio_i_find() */
370         inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
371 #ifndef AUTOMOUNT_FILE_NAME
372                              id->li_stc.u.e3s.l3s_type & S_IFMT,
373 #else
374                              id->li_stc.u.e3s.l3s_type, /* all of the bits! */
375 #endif
376                              0, 0,
377                              &llu_inode_ops, lli);
378
379         if (!inode)
380                 OBD_FREE(lli, sizeof(*lli));
381
382         return inode;
383 }
384
385 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
386 {
387         struct llu_sb_info *sbi = llu_i2sbi(inode);
388         struct llu_inode_info *lli = llu_i2info(inode);
389         struct lustre_handle lockh;
390         struct ldlm_res_id res_id = { .name = {0} };
391         struct obd_device *obddev;
392         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
393         int flags;
394         ENTRY;
395
396         LASSERT(inode);
397
398         obddev = sbi->ll_md_exp->exp_obd;
399         res_id.name[0] = id_fid(&lli->lli_id);
400         res_id.name[1] = id_group(&lli->lli_id);
401
402         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
403
404         /* FIXME use LDLM_FL_TEST_LOCK instead */
405         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
406         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
407                             &policy, LCK_PR, &lockh)) {
408                 ldlm_lock_decref(&lockh, LCK_PR);
409                 RETURN(1);
410         }
411
412         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
413                             &policy, LCK_PW, &lockh)) {
414                 ldlm_lock_decref(&lockh, LCK_PW);
415                 RETURN(1);
416         }
417         RETURN(0);
418 }
419
420 static int llu_inode_revalidate(struct inode *inode)
421 {
422         struct llu_inode_info *lli = llu_i2info(inode);
423         struct lov_stripe_md *lsm = NULL;
424         ENTRY;
425
426         if (!inode) {
427                 CERROR("REPORT THIS LINE TO PETER\n");
428                 RETURN(0);
429         }
430
431         if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
432                 struct lustre_md md;
433                 struct ptlrpc_request *req = NULL;
434                 struct llu_sb_info *sbi = llu_i2sbi(inode);
435                 struct lustre_id id;
436                 __u64 valid = 0;
437                 int rc, ealen = 0;
438
439                 /* Why don't we update all valid MDS fields here, if we're doing
440                  * an RPC anyways?  -phil */
441                 if (S_ISREG(lli->lli_st_mode)) {
442                         ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
443                         valid |= OBD_MD_FLEASIZE;
444                 }
445                 ll_inode2id(&id, inode);
446                 rc = mdc_getattr(sbi->ll_md_exp, &id, valid, NULL, 0,
447                                  ealen, &req);
448                 if (rc) {
449                         CERROR("failure %d inode %lu\n", rc, lli->lli_st_ino);
450                         RETURN(-abs(rc));
451                 }
452                 rc = mdc_req2lustre_md(sbi->ll_md_exp, req, 0, 
453                                        sbi->ll_dt_exp, &md);
454
455                 /* XXX Too paranoid? */
456                 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
457                     !((md.body->valid & OBD_MD_FLNLINK) &&
458                       (md.body->nlink == 0))) {
459                         CERROR("Asked for %s eadata but got %s (%d)\n",
460                                (valid & OBD_MD_FLEASIZE) ? "some" : "no",
461                                (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
462                                 md.body->eadatasize);
463                 }
464                 if (rc) {
465                         ptlrpc_req_finished(req);
466                         RETURN(rc);
467                 }
468
469
470                 llu_update_inode(inode, md.body, md.lsm);
471                 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
472                         obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
473
474                 if (md.body->valid & OBD_MD_FLSIZE)
475                         set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
476                                 &llu_i2info(inode)->lli_flags);
477                 ptlrpc_req_finished(req);
478         }
479
480         lsm = llu_i2info(inode)->lli_smd;
481         if (!lsm)       /* object not yet allocated, don't validate size */
482                 RETURN(0);
483
484         /* ll_glimpse_size will prefer locally cached writes if they extend
485          * the file */
486         RETURN(llu_glimpse_size(inode));
487 }
488
489 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
490 {
491         struct llu_inode_info *lli = llu_i2info(ino);
492
493         b->st_dev = lli->lli_st_dev;
494         b->st_ino = lli->lli_st_ino;
495         b->st_mode = lli->lli_st_mode;
496         b->st_nlink = lli->lli_st_nlink;
497         b->st_uid = lli->lli_st_uid;
498         b->st_gid = lli->lli_st_gid;
499         b->st_rdev = lli->lli_st_rdev;
500         b->st_size = lli->lli_st_size;
501         b->st_blksize = lli->lli_st_blksize;
502         b->st_blocks = lli->lli_st_blocks;
503         b->st_atime = lli->lli_st_atime;
504         b->st_mtime = lli->lli_st_mtime;
505         b->st_ctime = lli->lli_st_ctime;
506 }
507
508 static int llu_iop_getattr(struct pnode *pno,
509                            struct inode *ino,
510                            struct intnl_stat *b)
511 {
512         int rc;
513         ENTRY;
514
515         liblustre_wait_event(0);
516
517         if (!ino) {
518                 LASSERT(pno);
519                 LASSERT(pno->p_base->pb_ino);
520                 ino = pno->p_base->pb_ino;
521         } else {
522                 LASSERT(!pno || pno->p_base->pb_ino == ino);
523         }
524
525         /* libsysio might call us directly without intent lock,
526          * we must re-fetch the attrs here
527          */
528         rc = llu_inode_revalidate(ino);
529         if (!rc) {
530                 copy_stat_buf(ino, b);
531                 LASSERT(!llu_i2info(ino)->lli_it);
532         }
533
534         RETURN(rc);
535 }
536
537 static int null_if_equal(struct ldlm_lock *lock, void *data)
538 {
539         if (data == lock->l_ast_data) {
540                 lock->l_ast_data = NULL;
541
542                 if (lock->l_req_mode != lock->l_granted_mode)
543                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
544         }
545
546         return LDLM_ITER_CONTINUE;
547 }
548
549 void llu_clear_inode(struct inode *inode)
550 {
551         struct lustre_id id;
552         struct llu_inode_info *lli = llu_i2info(inode);
553         struct llu_sb_info *sbi = llu_i2sbi(inode);
554         ENTRY;
555
556         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu(%p)\n", lli->lli_st_ino,
557                lli->lli_st_generation, inode);
558
559         ll_inode2id(&id, inode);
560         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
561         mdc_change_cbdata(sbi->ll_md_exp, &id, null_if_equal, inode);
562
563         if (lli->lli_smd)
564                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
565                                   null_if_equal, inode);
566
567         if (lli->lli_smd) {
568                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
569                 lli->lli_smd = NULL;
570         }
571
572         if (lli->lli_symlink_name) {
573                 OBD_FREE(lli->lli_symlink_name,
574                          strlen(lli->lli_symlink_name) + 1);
575                 lli->lli_symlink_name = NULL;
576         }
577
578         EXIT;
579 }
580
581 void llu_iop_gone(struct inode *inode)
582 {
583         struct llu_inode_info *lli = llu_i2info(inode);
584         ENTRY;
585
586         liblustre_wait_event(0);
587         llu_clear_inode(inode);
588
589         OBD_FREE(lli, sizeof(*lli));
590         EXIT;
591 }
592
593 static int inode_setattr(struct inode * inode, struct iattr * attr)
594 {
595         unsigned int ia_valid = attr->ia_valid;
596         struct llu_inode_info *lli = llu_i2info(inode);
597         int error = 0;
598
599         if (ia_valid & ATTR_SIZE) {
600                 error = llu_vmtruncate(inode, attr->ia_size);
601                 if (error)
602                         goto out;
603         }
604
605         if (ia_valid & ATTR_UID)
606                 lli->lli_st_uid = attr->ia_uid;
607         if (ia_valid & ATTR_GID)
608                 lli->lli_st_gid = attr->ia_gid;
609         if (ia_valid & ATTR_ATIME)
610                 lli->lli_st_atime = attr->ia_atime;
611         if (ia_valid & ATTR_MTIME)
612                 lli->lli_st_mtime = attr->ia_mtime;
613         if (ia_valid & ATTR_CTIME)
614                 lli->lli_st_ctime = attr->ia_ctime;
615         if (ia_valid & ATTR_MODE) {
616                 lli->lli_st_mode = attr->ia_mode;
617                 if (!in_group_p(lli->lli_st_gid) && !capable(CAP_FSETID))
618                         lli->lli_st_mode &= ~S_ISGID;
619         }
620         /* mark_inode_dirty(inode); */
621 out:
622         return error;
623 }
624
625 /* If this inode has objects allocated to it (lsm != NULL), then the OST
626  * object(s) determine the file size and mtime.  Otherwise, the MDS will
627  * keep these values until such a time that objects are allocated for it.
628  * We do the MDS operations first, as it is checking permissions for us.
629  * We don't to the MDS RPC if there is nothing that we want to store there,
630  * otherwise there is no harm in updating mtime/atime on the MDS if we are
631  * going to do an RPC anyways.
632  *
633  * If we are doing a truncate, we will send the mtime and ctime updates
634  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
635  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
636  * at the same time.
637  */
638 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
639 {
640         struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
641         struct llu_sb_info *sbi = llu_i2sbi(inode);
642         struct llu_inode_info *lli = llu_i2info(inode);
643         struct ptlrpc_request *request = NULL;
644         struct mdc_op_data op_data;
645         int ia_valid = attr->ia_valid;
646         int rc = 0;
647         ENTRY;
648
649         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
650
651         if (ia_valid & ATTR_SIZE) {
652                 if (attr->ia_size > ll_file_maxbytes(inode)) {
653                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
654                                attr->ia_size, ll_file_maxbytes(inode));
655                         RETURN(-EFBIG);
656                 }
657
658                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
659         }
660
661         /* We mark all of the fields "set" so MDS/OST does not re-set them */
662         if (attr->ia_valid & ATTR_CTIME) {
663                 attr->ia_ctime = CURRENT_TIME;
664                 attr->ia_valid |= ATTR_CTIME_SET;
665         }
666         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
667                 attr->ia_atime = CURRENT_TIME;
668                 attr->ia_valid |= ATTR_ATIME_SET;
669         }
670         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
671                 attr->ia_mtime = CURRENT_TIME;
672                 attr->ia_valid |= ATTR_MTIME_SET;
673         }
674
675         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
676                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
677                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
678                        LTIME_S(CURRENT_TIME));
679         if (lsm)
680                 attr->ia_valid &= ~ATTR_SIZE;
681
682         /* If only OST attributes being set on objects, don't do MDS RPC.
683          * In that case, we need to check permissions and update the local
684          * inode ourselves so we can call obdo_from_inode() always. */
685         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
686                 struct lustre_md md;
687                 llu_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
688
689                 rc = mdc_setattr(sbi->ll_md_exp, &op_data,
690                                  attr, NULL, 0, NULL, 0, &request);
691                 
692                 if (rc) {
693                         ptlrpc_req_finished(request);
694                         if (rc != -EPERM && rc != -EACCES)
695                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
696                         RETURN(rc);
697                 }
698
699                 rc = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
700                                        sbi->ll_dt_exp, &md);
701                 if (rc) {
702                         ptlrpc_req_finished(request);
703                         RETURN(rc);
704                 }
705
706                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
707                  * but needed to set timestamps backwards on utime. */
708                 inode_setattr(inode, attr);
709                 llu_update_inode(inode, md.body, md.lsm);
710                 ptlrpc_req_finished(request);
711
712                 if (!md.lsm || !S_ISREG(lli->lli_st_mode)) {
713                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
714                         RETURN(0);
715                 }
716         } else {
717                 /* The OST doesn't check permissions, but the alternative is
718                  * a gratuitous RPC to the MDS.  We already rely on the client
719                  * to do read/write/truncate permission checks, so is mtime OK?
720                  */
721                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
722                         /* from sys_utime() */
723                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
724                                 if (current->fsuid != lli->lli_st_uid &&
725                                     (rc = ll_permission(inode, MAY_WRITE)) != 0)
726                                         RETURN(rc);
727                         } else {
728                                 /* from inode_change_ok() */
729                                 if (current->fsuid != lli->lli_st_uid &&
730                                     !capable(CAP_FOWNER))
731                                         RETURN(-EPERM);
732                         }
733                 }
734
735                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
736                 inode_setattr(inode, attr);
737         }
738
739         if (ia_valid & ATTR_SIZE) {
740                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
741                                                            OBD_OBJECT_EOF} };
742                 struct lustre_handle lockh = { 0 };
743                 int err, ast_flags = 0;
744                 /* XXX when we fix the AST intents to pass the discard-range
745                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
746                  * XXX here. */
747                 if (attr->ia_size == 0)
748                         ast_flags = LDLM_AST_DISCARD_DATA;
749
750                 rc = llu_extent_lock(NULL, inode, lsm, LCK_PW, &policy,
751                                      &lockh, ast_flags);
752                 if (rc != ELDLM_OK) {
753                         if (rc > 0)
754                                 RETURN(-ENOLCK);
755                         RETURN(rc);
756                 }
757
758                 rc = llu_vmtruncate(inode, attr->ia_size);
759
760                 /* unlock now as we don't mind others file lockers racing with
761                  * the mds updates below? */
762                 err = llu_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
763                 if (err) {
764                         CERROR("llu_extent_unlock failed: %d\n", err);
765                         if (!rc)
766                                 rc = err;
767                 }
768         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
769                 struct obdo oa;
770
771                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
772                        lli->lli_st_ino, LTIME_S(attr->ia_mtime));
773                 oa.o_id = lsm->lsm_object_id;
774                 oa.o_valid = OBD_MD_FLID;
775                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
776                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
777                 rc = obd_setattr(sbi->ll_dt_exp, &oa, lsm, NULL);
778                 if (rc)
779                         CERROR("obd_setattr fails: rc=%d\n", rc);
780         }
781         RETURN(rc);
782 }
783
784 /* here we simply act as a thin layer to glue it with
785  * llu_setattr_raw(), which is copy from kernel
786  */
787 static int llu_iop_setattr(struct pnode *pno,
788                            struct inode *ino,
789                            unsigned mask,
790                            struct intnl_stat *stbuf)
791 {
792         struct iattr iattr;
793         ENTRY;
794
795         liblustre_wait_event(0);
796
797         LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME | 
798                            SETATTR_UID | SETATTR_GID |
799                            SETATTR_LEN | SETATTR_MODE)));
800         memset(&iattr, 0, sizeof(iattr));
801
802         if (mask & SETATTR_MODE) {
803                 iattr.ia_mode = stbuf->st_mode;
804                 iattr.ia_valid |= ATTR_MODE;
805         }
806         if (mask & SETATTR_MTIME) {
807                 iattr.ia_mtime = stbuf->st_mtime;
808                 iattr.ia_valid |= ATTR_MTIME;
809         }
810         if (mask & SETATTR_ATIME) {
811                 iattr.ia_atime = stbuf->st_atime;
812                 iattr.ia_valid |= ATTR_ATIME;
813         }
814         if (mask & SETATTR_UID) {
815                 iattr.ia_uid = stbuf->st_uid;
816                 iattr.ia_valid |= ATTR_UID;
817         }
818         if (mask & SETATTR_GID) {
819                 iattr.ia_gid = stbuf->st_gid;
820                 iattr.ia_valid |= ATTR_GID;
821         }
822         if (mask & SETATTR_LEN) {
823                 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
824                 iattr.ia_valid |= ATTR_SIZE;
825         }
826
827         iattr.ia_valid |= ATTR_RAW;
828
829         RETURN(llu_setattr_raw(ino, &iattr));
830 }
831
832 #define EXT2_LINK_MAX           32000
833
834 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
835 {
836         struct inode *dir = pno->p_base->pb_parent->pb_ino;
837         struct qstr *qstr = &pno->p_base->pb_name;
838         const char *name = qstr->name;
839         int len = qstr->len;
840         struct ptlrpc_request *request = NULL;
841         struct llu_sb_info *sbi = llu_i2sbi(dir);
842         struct mdc_op_data op_data;
843         int err = -EMLINK;
844         ENTRY;
845
846         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
847                 RETURN(err);
848
849         llu_prepare_mdc_data(&op_data, dir, NULL, name, len, 0);
850         err = mdc_create(sbi->ll_md_exp, &op_data,
851                          tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
852                          current->fsuid, current->fsgid, 0, &request);
853         ptlrpc_req_finished(request);
854         RETURN(err);
855 }
856
857 static int llu_readlink_internal(struct inode *inode,
858                                  struct ptlrpc_request **request,
859                                  char **symname)
860 {
861         struct llu_inode_info *lli = llu_i2info(inode);
862         struct llu_sb_info *sbi = llu_i2sbi(inode);
863         struct lustre_id id;
864         struct mds_body *body;
865         int rc, symlen = lli->lli_st_size + 1;
866         ENTRY;
867
868         *request = NULL;
869
870         if (lli->lli_symlink_name) {
871                 *symname = lli->lli_symlink_name;
872                 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
873                 RETURN(0);
874         }
875
876         ll_inode2id(&id, inode);
877         rc = mdc_getattr(sbi->ll_md_exp, &id,
878                          OBD_MD_LINKNAME, NULL, 0, symlen, request);
879         if (rc) {
880                 CERROR("inode %lu: rc = %d\n", lli->lli_st_ino, rc);
881                 RETURN(rc);
882         }
883
884         body = lustre_msg_buf ((*request)->rq_repmsg, 0, sizeof (*body));
885         LASSERT (body != NULL);
886         LASSERT_REPSWABBED (*request, 0);
887
888         if ((body->valid & OBD_MD_LINKNAME) == 0) {
889                 CERROR ("OBD_MD_LINKNAME not set on reply\n");
890                 GOTO (failed, rc = -EPROTO);
891         }
892         
893         LASSERT (symlen != 0);
894         if (body->eadatasize != symlen) {
895                 CERROR ("inode %lu: symlink length %d not expected %d\n",
896                         lli->lli_st_ino, body->eadatasize - 1, symlen - 1);
897                 GOTO (failed, rc = -EPROTO);
898         }
899
900         *symname = lustre_msg_buf ((*request)->rq_repmsg, 1, symlen);
901         if (*symname == NULL ||
902             strnlen (*symname, symlen) != symlen - 1) {
903                 /* not full/NULL terminated */
904                 CERROR ("inode %lu: symlink not NULL terminated string"
905                         "of length %d\n", lli->lli_st_ino, symlen - 1);
906                 GOTO (failed, rc = -EPROTO);
907         }
908
909         OBD_ALLOC(lli->lli_symlink_name, symlen);
910         /* do not return an error if we cannot cache the symlink locally */
911         if (lli->lli_symlink_name)
912                 memcpy(lli->lli_symlink_name, *symname, symlen);
913
914         RETURN(0);
915
916  failed:
917         ptlrpc_req_finished (*request);
918         RETURN (-EPROTO);
919 }
920
921 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
922 {
923         struct inode *inode = pno->p_base->pb_ino;
924         struct ptlrpc_request *request;
925         char *symname;
926         int rc;
927         ENTRY;
928
929         rc = llu_readlink_internal(inode, &request, &symname);
930         if (rc)
931                 GOTO(out, rc);
932
933         LASSERT(symname);
934         strncpy(data, symname, bufsize);
935
936         ptlrpc_req_finished(request);
937  out:
938         RETURN(rc);
939 }
940
941 static int llu_iop_mknod_raw(struct pnode *pno,
942                              mode_t mode,
943                              dev_t dev)
944 {
945         struct ptlrpc_request *request = NULL;
946         struct inode *dir = pno->p_parent->p_base->pb_ino;
947         struct llu_sb_info *sbi = llu_i2sbi(dir);
948         struct mdc_op_data op_data;
949         int err = -EMLINK;
950         ENTRY;
951
952         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu\n",
953                (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
954                llu_i2info(dir)->lli_st_ino);
955
956         if (llu_i2info(dir)->lli_st_nlink >= EXT2_LINK_MAX)
957                 RETURN(err);
958
959         mode &= ~current->fs->umask;
960
961         switch (mode & S_IFMT) {
962         case 0:
963         case S_IFREG:
964                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
965         case S_IFCHR:
966         case S_IFBLK:
967         case S_IFIFO:
968         case S_IFSOCK:
969                 llu_prepare_mdc_data(&op_data, dir, NULL,
970                                      pno->p_base->pb_name.name,
971                                      pno->p_base->pb_name.len,
972                                      0);
973                 err = mdc_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
974                                  current->fsuid, current->fsgid, dev, &request);
975                 ptlrpc_req_finished(request);
976                 break;
977         case S_IFDIR:
978                 err = -EPERM;
979                 break;
980         default:
981                 err = -EINVAL;
982         }
983         RETURN(err);
984 }
985
986 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
987 {
988         struct inode *src = old->p_base->pb_ino;
989         struct inode *dir = new->p_parent->p_base->pb_ino;
990         const char *name = new->p_base->pb_name.name;
991         int namelen = new->p_base->pb_name.len;
992         struct ptlrpc_request *request = NULL;
993         struct mdc_op_data op_data;
994         int rc;
995         ENTRY;
996
997         LASSERT(src);
998         LASSERT(dir);
999
1000         liblustre_wait_event(0);
1001         llu_prepare_mdc_data(&op_data, src, dir, name, namelen, 0);
1002         rc = mdc_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1003         ptlrpc_req_finished(request);
1004         liblustre_wait_event(0);
1005
1006         RETURN(rc);
1007 }
1008
1009 /*
1010  * libsysio will clear the inode immediately after return
1011  */
1012 static int llu_iop_unlink_raw(struct pnode *pno)
1013 {
1014         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1015         struct qstr *qstr = &pno->p_base->pb_name;
1016         const char *name = qstr->name;
1017         int len = qstr->len;
1018         struct inode *target = pno->p_base->pb_ino;
1019         struct ptlrpc_request *request = NULL;
1020         struct mdc_op_data op_data;
1021         int rc;
1022         ENTRY;
1023
1024         LASSERT(target);
1025
1026         liblustre_wait_event(0);
1027         llu_prepare_mdc_data(&op_data, dir, NULL, name, len, 0);
1028         rc = mdc_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1029         if (!rc)
1030                 rc = llu_objects_destroy(request, dir);
1031         ptlrpc_req_finished(request);
1032         liblustre_wait_event(0);
1033         RETURN(rc);
1034 }
1035
1036 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1037 {
1038         struct inode *src = old->p_parent->p_base->pb_ino;
1039         struct inode *tgt = new->p_parent->p_base->pb_ino;
1040         const char *oldname = old->p_base->pb_name.name;
1041         int oldnamelen = old->p_base->pb_name.len;
1042         const char *newname = new->p_base->pb_name.name;
1043         int newnamelen = new->p_base->pb_name.len;
1044         struct ptlrpc_request *request = NULL;
1045         struct mdc_op_data op_data;
1046         int rc;
1047         ENTRY;
1048
1049         LASSERT(src);
1050         LASSERT(tgt);
1051
1052         llu_prepare_mdc_data(&op_data, src, tgt, NULL, 0, 0);
1053         rc = mdc_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1054                         oldname, oldnamelen, newname, newnamelen,
1055                         &request);
1056         if (!rc) {
1057                 rc = llu_objects_destroy(request, src);
1058         }
1059
1060         ptlrpc_req_finished(request);
1061
1062         RETURN(rc);
1063 }
1064
1065 #ifdef _HAVE_STATVFS
1066 static int llu_statfs_internal(struct llu_sb_info *sbi,
1067                                struct obd_statfs *osfs,
1068                                unsigned long max_age)
1069 {
1070         struct obd_statfs obd_osfs;
1071         int rc;
1072         ENTRY;
1073
1074         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1075         if (rc) {
1076                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1077                 RETURN(rc);
1078         }
1079
1080         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1081                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1082
1083         rc = obd_statfs(class_exp2obd(sbi->ll_dt_exp), &obd_osfs, max_age);
1084         if (rc) {
1085                 CERROR("obd_statfs fails: rc = %d\n", rc);
1086                 RETURN(rc);
1087         }
1088
1089         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1090                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1091                obd_osfs.os_files);
1092
1093         osfs->os_blocks = obd_osfs.os_blocks;
1094         osfs->os_bfree = obd_osfs.os_bfree;
1095         osfs->os_bavail = obd_osfs.os_bavail;
1096
1097         /* If we don't have as many objects free on the OST as inodes
1098          * on the MDS, we reduce the total number of inodes to
1099          * compensate, so that the "inodes in use" number is correct.
1100          */
1101         if (obd_osfs.os_ffree < osfs->os_ffree) {
1102                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1103                         obd_osfs.os_ffree;
1104                 osfs->os_ffree = obd_osfs.os_ffree;
1105         }
1106
1107         RETURN(rc);
1108 }
1109
1110 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1111 {
1112         struct obd_statfs osfs;
1113         int rc;
1114
1115         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1116
1117         /* For now we will always get up-to-date statfs values, but in the
1118          * future we may allow some amount of caching on the client (e.g.
1119          * from QOS or lprocfs updates). */
1120         rc = llu_statfs_internal(sbi, &osfs, jiffies - 1);
1121         if (rc)
1122                 return rc;
1123
1124         statfs_unpack(sfs, &osfs);
1125
1126         if (sizeof(sfs->f_blocks) == 4) {
1127                 while (osfs.os_blocks > ~0UL) {
1128                         sfs->f_bsize <<= 1;
1129
1130                         osfs.os_blocks >>= 1;
1131                         osfs.os_bfree >>= 1;
1132                         osfs.os_bavail >>= 1;
1133                 }
1134         }
1135
1136         sfs->f_blocks = osfs.os_blocks;
1137         sfs->f_bfree = osfs.os_bfree;
1138         sfs->f_bavail = osfs.os_bavail;
1139
1140         return 0;
1141 }
1142
1143 static int llu_iop_statvfs(struct pnode *pno,
1144                            struct inode *ino,
1145                            struct intnl_statvfs *buf)
1146 {
1147         struct statfs fs;
1148         int rc;
1149         ENTRY;
1150
1151         liblustre_wait_event(0);
1152
1153 #ifndef __CYGWIN__
1154         LASSERT(pno->p_base->pb_ino);
1155         rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1156         if (rc)
1157                 RETURN(rc);
1158
1159         /* from native driver */
1160         buf->f_bsize = fs.f_bsize;  /* file system block size */
1161         buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1162         buf->f_blocks = fs.f_blocks;
1163         buf->f_bfree = fs.f_bfree;
1164         buf->f_bavail = fs.f_bavail;
1165         buf->f_files = fs.f_files;  /* Total number serial numbers */
1166         buf->f_ffree = fs.f_ffree;  /* Number free serial numbers */
1167         buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1168         buf->f_fsid = fs.f_fstc.__val[1];
1169         buf->f_flag = 0;            /* No equiv in statfs; maybe use type? */
1170         buf->f_namemax = fs.f_namelen;
1171 #endif
1172
1173         RETURN(0);
1174 }
1175 #endif /* _HAVE_STATVFS */
1176
1177 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1178 {
1179         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1180         struct qstr *qstr = &pno->p_base->pb_name;
1181         const char *name = qstr->name;
1182         int len = qstr->len;
1183         struct ptlrpc_request *request = NULL;
1184         struct llu_inode_info *lli = llu_i2info(dir);
1185         struct mdc_op_data op_data;
1186         int err = -EMLINK;
1187         ENTRY;
1188         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%lu(%p)\n",
1189                len, name, lli->lli_st_ino, lli->lli_st_generation, dir);
1190
1191         if (lli->lli_st_nlink >= EXT2_LINK_MAX)
1192                 RETURN(err);
1193
1194         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
1195         llu_prepare_mdc_data(&op_data, dir, NULL, name, len, 0);
1196         err = mdc_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0, mode,
1197                          current->fsuid, current->fsgid, 0, &request);
1198         ptlrpc_req_finished(request);
1199         RETURN(err);
1200 }
1201
1202 static int llu_iop_rmdir_raw(struct pnode *pno)
1203 {
1204         struct inode *dir = pno->p_base->pb_parent->pb_ino;
1205         struct qstr *qstr = &pno->p_base->pb_name;
1206         const char *name = qstr->name;
1207         int len = qstr->len;
1208         struct ptlrpc_request *request = NULL;
1209         struct mdc_op_data op_data;
1210         struct llu_inode_info *lli = llu_i2info(dir);
1211         int rc;
1212         ENTRY;
1213         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%lu(%p)\n",
1214                len, name, lli->lli_st_ino, lli->lli_st_generation, dir);
1215
1216         llu_prepare_mdc_data(&op_data, dir, NULL, name, len, S_IFDIR);
1217         rc = mdc_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1218         ptlrpc_req_finished(request);
1219
1220         RETURN(rc);
1221 }
1222
1223 #ifdef O_DIRECT
1224 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1225 #else
1226 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1227 #endif
1228 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1229
1230 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1231 {
1232         struct llu_inode_info *lli = llu_i2info(ino);
1233         long flags;
1234
1235         switch (cmd) {
1236         case F_GETFL:
1237                 *rtn = lli->lli_open_flags;
1238                 return 0;
1239         case F_SETFL:
1240                 flags = va_arg(ap, long);
1241                 flags &= FCNTL_FLMASK;
1242                 if (flags & FCNTL_FLMASK_INVALID) {
1243                         CERROR("liblustre don't support O_NONBLOCK, O_ASYNC, "
1244                                "and O_DIRECT on file descriptor\n");
1245                         *rtn = -1;
1246                         return EINVAL;
1247                 }
1248                 lli->lli_open_flags = (int) flags;
1249                 *rtn = 0;
1250                 return 0;
1251         }
1252
1253         CERROR("unsupported fcntl cmd %x\n", cmd);
1254         *rtn = -1;
1255         return ENOSYS;
1256 }
1257
1258 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1259 {
1260         struct llu_inode_info *lli = llu_i2info(inode);
1261         struct ll_file_data *fd = lli->lli_file_data;
1262         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1263                                                     .end = OBD_OBJECT_EOF}};
1264         struct lustre_handle lockh = { 0 };
1265         struct lov_stripe_md *lsm = lli->lli_smd;
1266         ldlm_error_t err;
1267         int flags = 0;
1268         ENTRY;
1269
1270         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1271                 RETURN(-EINVAL);
1272         }
1273
1274         policy.l_extent.gid = arg;
1275         if (lli->lli_open_flags & O_NONBLOCK)
1276                 flags = LDLM_FL_BLOCK_NOWAIT;
1277
1278         err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1279                               flags);
1280         if (err)
1281                 RETURN(err);
1282
1283         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1284         fd->fd_gid = arg;
1285         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1286
1287         RETURN(0);
1288 }
1289
1290 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1291 {
1292         struct llu_inode_info *lli = llu_i2info(inode);
1293         struct ll_file_data *fd = lli->lli_file_data;
1294         struct lov_stripe_md *lsm = lli->lli_smd;
1295         ldlm_error_t err;
1296         ENTRY;
1297
1298         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1299                 RETURN(-EINVAL);
1300
1301         if (fd->fd_gid != arg)
1302                 RETURN(-EINVAL);
1303
1304         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1305
1306         err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1307         if (err)
1308                 RETURN(err);
1309
1310         fd->fd_gid = 0;
1311         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1312
1313         RETURN(0);
1314 }       
1315
1316 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1317                          va_list ap)
1318 {
1319         unsigned long arg;
1320
1321         liblustre_wait_event(0);
1322
1323         switch (request) {
1324         case LL_IOC_GROUP_LOCK:
1325                 arg = va_arg(ap, unsigned long);
1326                 return llu_get_grouplock(ino, arg);
1327         case LL_IOC_GROUP_UNLOCK:
1328                 arg = va_arg(ap, unsigned long);
1329                 return llu_put_grouplock(ino, arg);
1330         }
1331
1332         CERROR("did not support ioctl cmd %lx\n", request);
1333         return -ENOSYS;
1334 }
1335
1336 /*
1337  * we already do syncronous read/write
1338  */
1339 static int llu_iop_sync(struct inode *inode)
1340 {
1341         liblustre_wait_event(0);
1342         return 0;
1343 }
1344
1345 static int llu_iop_datasync(struct inode *inode)
1346 {
1347         liblustre_wait_event(0);
1348         return 0;
1349 }
1350
1351 struct filesys_ops llu_filesys_ops =
1352 {
1353         fsop_gone: llu_fsop_gone,
1354 };
1355
1356 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1357 {
1358         struct inode *inode;
1359         struct lustre_id id;
1360         struct file_identifier fileid = {&id, sizeof(id)};
1361
1362         if ((md->body->valid &
1363              (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1364             (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1365                 CERROR("bad md body valid mask 0x"LPX64"\n", 
1366                        md->body->valid);
1367                 LBUG();
1368                 return ERR_PTR(-EPERM);
1369         }
1370
1371         id = md->body->id1;
1372
1373         /* try to find existing inode */
1374         inode = _sysio_i_find(fs, &fileid);
1375         if (inode) {
1376                 struct llu_inode_info *lli = llu_i2info(inode);
1377
1378                 if (inode->i_zombie ||
1379                     lli->lli_st_generation != id_gen(&md->body->id1)) {
1380                         I_RELE(inode);
1381                 }
1382                 else {
1383                         llu_update_inode(inode, md->body, md->lsm);
1384                         return inode;
1385                 }
1386         }
1387
1388         inode = llu_new_inode(fs, &id);
1389         if (inode)
1390                 llu_update_inode(inode, md->body, md->lsm);
1391         
1392         return inode;
1393 }
1394
1395 static int
1396 llu_fsswop_mount(const char *source,
1397                  unsigned flags,
1398                  const void *data __IS_UNUSED,
1399                  struct pnode *tocover,
1400                  struct mount **mntp)
1401 {
1402         struct filesys *fs;
1403         struct inode *root;
1404         struct pnode_base *rootpb;
1405         struct obd_device *obd;
1406         struct lustre_id rootid;
1407         struct llu_sb_info *sbi;
1408         struct obd_statfs osfs;
1409         static struct qstr noname = { NULL, 0, 0 };
1410         struct ptlrpc_request *request = NULL;
1411         struct lustre_handle lmv_conn = {0, };
1412         struct lustre_handle lov_conn = {0, };
1413         struct lustre_md md;
1414         class_uuid_t uuid;
1415         struct config_llog_instance cfg;
1416         struct lustre_profile *lprof;
1417         char *lov = NULL, *lmv = NULL;
1418         int async = 1, err = -EINVAL;
1419
1420         ENTRY;
1421
1422         /* allocate & initialize sbi */
1423         OBD_ALLOC(sbi, sizeof(*sbi));
1424         if (!sbi)
1425                 RETURN(-ENOMEM);
1426
1427         INIT_LIST_HEAD(&sbi->ll_conn_chain);
1428         generate_random_uuid(uuid);
1429         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1430
1431         /* generate a string unique to this super, let's try
1432          the address of the super itself.*/
1433         OBD_ALLOC(sbi->ll_instance, sizeof(sbi) * 2 + 1);
1434         if (sbi->ll_instance == NULL) 
1435                 GOTO(out_free, err = -ENOMEM);
1436         sprintf(sbi->ll_instance, "%p", sbi);
1437
1438         /* retrive & parse config log */
1439         cfg.cfg_instance = sbi->ll_instance;
1440         cfg.cfg_uuid = sbi->ll_sb_uuid;
1441         err = liblustre_process_log(&cfg, 1);
1442         if (err < 0) {
1443                 CERROR("Unable to process log: %s\n", g_zconf_profile);
1444                 GOTO(out_free, err);
1445         }
1446
1447         lprof = class_get_profile(g_zconf_profile);
1448         if (lprof == NULL) {
1449                 CERROR("No profile found: %s\n", g_zconf_profile);
1450                 GOTO(out_free, err = -EINVAL);
1451         }
1452         if (lov)
1453                 OBD_FREE(lov, strlen(lov) + 1);
1454         OBD_ALLOC(lov, strlen(lprof->lp_lov) + 
1455                   strlen(sbi->ll_instance) + 2);
1456         sprintf(lov, "%s-%s", lprof->lp_lov, sbi->ll_instance);
1457
1458         if (lmv)
1459                 OBD_FREE(lmv, strlen(lmv) + 1);
1460         OBD_ALLOC(lmv, strlen(lprof->lp_lmv) + 
1461                   strlen(sbi->ll_instance) + 2);
1462         sprintf(lmv, "%s-%s", lprof->lp_lmv, sbi->ll_instance);
1463
1464         if (!lov) {
1465                 CERROR("no osc\n");
1466                 GOTO(out_free, err = -EINVAL);
1467         }
1468         if (!lmv) {
1469                 CERROR("no mdc\n");
1470                 GOTO(out_free, err = -EINVAL);
1471         }
1472
1473         fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1474         if (!fs) {
1475                 err = -ENOMEM;
1476                 goto out_free;
1477         }
1478
1479         obd = class_name2obd(lmv);
1480         if (!obd) {
1481                 CERROR("MDC %s: not setup or attached\n", lmv);
1482                 GOTO(out_free, err = -EINVAL);
1483         }
1484         obd_set_info(obd->obd_self_export, strlen("async"), "async",
1485                      sizeof(async), &async);
1486 #if 0
1487         if (mdc_init_ea_size(obd, osc))
1488                 GOTO(out_free, err = -EINVAL);
1489 #endif
1490         /* setup mdc */
1491         err = obd_connect(&lmv_conn, obd, &sbi->ll_sb_uuid, 0);
1492         if (err) {
1493                 CERROR("cannot connect to %s: rc = %d\n", lmv, err);
1494                 GOTO(out_free, err);
1495         }
1496         sbi->ll_md_exp = class_conn2export(&lmv_conn);
1497
1498         err = obd_statfs(obd, &osfs, 100000000);
1499         if (err)
1500                 GOTO(out_lmv, err);
1501
1502         /*
1503          * FIXME fill fs stat data into sbi here!!! FIXME
1504          */
1505
1506         /* setup lov */
1507         obd = class_name2obd(lov);
1508         if (!obd) {
1509                 CERROR("OSC %s: not setup or attached\n", lov);
1510                 GOTO(out_lmv, err = -EINVAL);
1511         }
1512         obd_set_info(obd->obd_self_export, strlen("async"), "async",
1513                      sizeof(async), &async);
1514
1515         err = obd_connect(&lov_conn, obd, &sbi->ll_sb_uuid, 0);
1516         if (err) {
1517                 CERROR("cannot connect to %s: rc = %d\n", lov, err);
1518                 GOTO(out_lmv, err);
1519         }
1520         sbi->ll_dt_exp = class_conn2export(&lov_conn);
1521
1522         err = mdc_getstatus(sbi->ll_md_exp, &rootid);
1523         if (err) {
1524                 CERROR("cannot mds_connect: rc = %d\n", err);
1525                 GOTO(out_lov, err);
1526         }
1527         CDEBUG(D_SUPER, "rootid "LPU64"\n", rootid.li_stc.u.e3s.l3s_ino);
1528         sbi->ll_rootino = rootid.li_stc.u.e3s.l3s_ino;
1529
1530         /* fetch attr of root inode */
1531         err = mdc_getattr(sbi->ll_md_exp, &rootid,
1532                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, NULL, 0,
1533                           0, &request);
1534         if (err) {
1535                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
1536                 GOTO(out_lov, err);
1537         }
1538
1539         err = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
1540                                 sbi->ll_dt_exp, &md);
1541         if (err) {
1542                 CERROR("failed to understand root inode md: rc = %d\n",err);
1543                 GOTO(out_request, err);
1544         }
1545
1546         LASSERT(sbi->ll_rootino != 0);
1547
1548         root = llu_iget(fs, &md);
1549         if (!root || IS_ERR(root)) {
1550                 CERROR("fail to generate root inode\n");
1551                 GOTO(out_request, err = -EBADF);
1552         }
1553
1554         /*
1555          * Generate base path-node for root.
1556          */
1557         rootpb = _sysio_pb_new(&noname, NULL, root);
1558         if (!rootpb) {
1559                 err = -ENOMEM;
1560                 goto out_inode;
1561         }
1562
1563         err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
1564         if (err) {
1565                 _sysio_pb_gone(rootpb);
1566                 goto out_inode;
1567         }
1568
1569         ptlrpc_req_finished(request);
1570
1571         printf("LibLustre: namespace mounted successfully!\n");
1572
1573         return 0;
1574
1575 out_inode:
1576         _sysio_i_gone(root);
1577 out_request:
1578         ptlrpc_req_finished(request);
1579 out_lov:
1580         obd_disconnect(sbi->ll_dt_exp, 0);
1581 out_lmv:
1582         obd_disconnect(sbi->ll_md_exp, 0);
1583 out_free:
1584         OBD_FREE(sbi, sizeof(*sbi));
1585         return err;
1586 }
1587
1588 struct fssw_ops llu_fssw_ops = {
1589         llu_fsswop_mount
1590 };
1591
1592 struct inode_ops llu_inode_ops = {
1593         .inop_lookup         = llu_iop_lookup,
1594         .inop_getattr        = llu_iop_getattr,
1595         .inop_setattr        = llu_iop_setattr,
1596         .inop_getdirentries  = llu_iop_getdirentries,
1597         .inop_mkdir          = llu_iop_mkdir_raw,
1598         .inop_rmdir          = llu_iop_rmdir_raw,
1599         .inop_symlink        = llu_iop_symlink_raw,
1600         .inop_readlink       = llu_iop_readlink,
1601         .inop_open           = llu_iop_open,
1602         .inop_close          = llu_iop_close,
1603         .inop_link           = llu_iop_link_raw,
1604         .inop_unlink         = llu_iop_unlink_raw,
1605         .inop_rename         = llu_iop_rename_raw,
1606         .inop_iodone         = llu_iop_iodone,
1607         .inop_fcntl          = llu_iop_fcntl,
1608         .inop_sync           = llu_iop_sync,
1609         .inop_read           = llu_iop_read,
1610         .inop_write          = llu_iop_write,
1611         .inop_datasync       = llu_iop_datasync,
1612         .inop_ioctl          = llu_iop_ioctl,
1613         .inop_mknod          = llu_iop_mknod_raw,
1614 #ifdef _HAVE_STATVFS
1615         .inop_statvfs        = llu_iop_statvfs,
1616 #endif
1617         .inop_gone           = llu_iop_gone,
1618 };