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