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