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