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