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