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