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