1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/super.c
38 * Lustre Light Super operations
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
50 #include <sys/queue.h>
52 # include <sys/statvfs.h>
54 # include <sys/statfs.h>
68 #include "llite_lib.h"
76 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
78 static int ll_permission(struct inode *inode, int mask)
80 struct intnl_stat *st = llu_i2stat(inode);
81 mode_t mode = st->st_mode;
83 if (current->fsuid == st->st_uid)
85 else if (cfs_curproc_is_in_groups(st->st_gid))
88 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
91 if ((mask & (MAY_READ|MAY_WRITE)) ||
92 (st->st_mode & S_IXUGO))
93 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
96 if (mask == MAY_READ ||
97 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
98 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
105 static void llu_fsop_gone(struct filesys *fs)
107 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
108 struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
112 cfs_list_del(&sbi->ll_conn_chain);
114 obd_disconnect(sbi->ll_dt_exp);
115 obd_disconnect(sbi->ll_md_exp);
117 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
118 class_manual_cleanup(obd);
120 OBD_FREE(sbi, sizeof(*sbi));
122 liblustre_wait_idle();
126 static struct inode_ops llu_inode_ops;
128 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
129 struct lustre_handle *lockh)
131 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
137 fid = &llu_i2info(inode)->lli_fid;
138 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
140 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
141 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
142 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
146 void llu_update_inode(struct inode *inode, struct lustre_md *md)
148 struct llu_inode_info *lli = llu_i2info(inode);
149 struct mdt_body *body = md->body;
150 struct lov_stripe_md *lsm = md->lsm;
151 struct intnl_stat *st = llu_i2stat(inode);
153 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
155 if (body->valid & OBD_MD_FLMODE)
156 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
157 if (body->valid & OBD_MD_FLTYPE)
158 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
161 if (lli->lli_smd == NULL) {
162 cl_inode_init(inode, md);
164 lli->lli_maxbytes = lsm->lsm_maxbytes;
165 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
166 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
168 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
169 CERROR("lsm mismatch for inode %lld\n",
170 (long long)st->st_ino);
176 if (body->valid & OBD_MD_FLMTIME &&
177 body->mtime > LTIME_S(st->st_mtime))
178 LTIME_S(st->st_mtime) = body->mtime;
179 if (body->valid & OBD_MD_FLATIME &&
180 body->atime > LTIME_S(st->st_atime))
181 LTIME_S(st->st_atime) = body->atime;
183 /* mtime is always updated with ctime, but can be set in past.
184 As write and utime(2) may happen within 1 second, and utime's
185 mtime has a priority over write's one, so take mtime from mds
186 for the same ctimes. */
187 if (body->valid & OBD_MD_FLCTIME &&
188 body->ctime >= LTIME_S(st->st_ctime)) {
189 LTIME_S(st->st_ctime) = body->ctime;
190 if (body->valid & OBD_MD_FLMTIME)
191 LTIME_S(st->st_mtime) = body->mtime;
193 if (S_ISREG(st->st_mode))
194 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
196 st->st_blksize = 4096;
197 if (body->valid & OBD_MD_FLUID)
198 st->st_uid = body->uid;
199 if (body->valid & OBD_MD_FLGID)
200 st->st_gid = body->gid;
201 if (body->valid & OBD_MD_FLNLINK)
202 st->st_nlink = body->nlink;
203 if (body->valid & OBD_MD_FLRDEV)
204 st->st_rdev = body->rdev;
205 if (body->valid & OBD_MD_FLFLAGS)
206 lli->lli_st_flags = body->flags;
207 if (body->valid & OBD_MD_FLSIZE) {
208 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
209 S_ISREG(st->st_mode) && lli->lli_smd) {
210 struct lustre_handle lockh;
213 /* As it is possible a blocking ast has been processed
214 * by this time, we need to check there is an UPDATE
215 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
217 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
220 st->st_size = body->size;
221 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
222 ldlm_lock_decref(&lockh, mode);
225 st->st_size = body->size;
228 if (body->valid & OBD_MD_FLBLOCKS)
229 st->st_blocks = body->blocks;
233 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
235 struct llu_inode_info *lli = llu_i2info(dst);
236 struct intnl_stat *st = llu_i2stat(dst);
238 valid &= src->o_valid;
240 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
241 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
244 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
245 (long)src->o_mtime, (long)src->o_ctime);
247 if (valid & OBD_MD_FLATIME)
248 LTIME_S(st->st_atime) = src->o_atime;
249 if (valid & OBD_MD_FLMTIME)
250 LTIME_S(st->st_mtime) = src->o_mtime;
251 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
252 LTIME_S(st->st_ctime) = src->o_ctime;
253 if (valid & OBD_MD_FLSIZE)
254 st->st_size = src->o_size;
255 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
256 st->st_blocks = src->o_blocks;
257 if (valid & OBD_MD_FLBLKSZ)
258 st->st_blksize = src->o_blksize;
259 if (valid & OBD_MD_FLTYPE)
260 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
261 if (valid & OBD_MD_FLMODE)
262 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
263 if (valid & OBD_MD_FLUID)
264 st->st_uid = src->o_uid;
265 if (valid & OBD_MD_FLGID)
266 st->st_gid = src->o_gid;
267 if (valid & OBD_MD_FLFLAGS)
268 lli->lli_st_flags = src->o_flags;
271 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
272 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
274 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
276 struct llu_inode_info *lli = llu_i2info(src);
277 struct intnl_stat *st = llu_i2stat(src);
278 obd_flag newvalid = 0;
280 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
281 CDEBUG(D_INODE, "valid %x, new time "CFS_TIME_T"/"CFS_TIME_T"\n",
282 valid, LTIME_S(st->st_mtime),
283 LTIME_S(st->st_ctime));
285 if (valid & OBD_MD_FLATIME) {
286 dst->o_atime = LTIME_S(st->st_atime);
287 newvalid |= OBD_MD_FLATIME;
289 if (valid & OBD_MD_FLMTIME) {
290 dst->o_mtime = LTIME_S(st->st_mtime);
291 newvalid |= OBD_MD_FLMTIME;
293 if (valid & OBD_MD_FLCTIME) {
294 dst->o_ctime = LTIME_S(st->st_ctime);
295 newvalid |= OBD_MD_FLCTIME;
297 if (valid & OBD_MD_FLSIZE) {
298 dst->o_size = st->st_size;
299 newvalid |= OBD_MD_FLSIZE;
301 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
302 dst->o_blocks = st->st_blocks;
303 newvalid |= OBD_MD_FLBLOCKS;
305 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
306 dst->o_blksize = st->st_blksize;
307 newvalid |= OBD_MD_FLBLKSZ;
309 if (valid & OBD_MD_FLTYPE) {
310 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
311 newvalid |= OBD_MD_FLTYPE;
313 if (valid & OBD_MD_FLMODE) {
314 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
315 newvalid |= OBD_MD_FLMODE;
317 if (valid & OBD_MD_FLUID) {
318 dst->o_uid = st->st_uid;
319 newvalid |= OBD_MD_FLUID;
321 if (valid & OBD_MD_FLGID) {
322 dst->o_gid = st->st_gid;
323 newvalid |= OBD_MD_FLGID;
325 if (valid & OBD_MD_FLFLAGS) {
326 dst->o_flags = lli->lli_st_flags;
327 newvalid |= OBD_MD_FLFLAGS;
329 if (valid & OBD_MD_FLGENER) {
330 dst->o_generation = lli->lli_st_generation;
331 newvalid |= OBD_MD_FLGENER;
333 if (valid & OBD_MD_FLFID) {
334 dst->o_fid = st->st_ino;
335 newvalid |= OBD_MD_FLFID;
338 dst->o_valid |= newvalid;
342 * Performs the getattr on the inode and updates its fields.
343 * If @sync != 0, perform the getattr under the server-side lock.
345 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
346 __u64 ioepoch, int sync)
348 struct llu_inode_info *lli = llu_i2info(inode);
349 struct ptlrpc_request_set *set;
350 struct lov_stripe_md *lsm = lli->lli_smd;
351 struct obd_info oinfo = { { { 0 } } };
359 oinfo.oi_oa->o_id = lsm->lsm_object_id;
360 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
361 oinfo.oi_oa->o_mode = S_IFREG;
362 oinfo.oi_oa->o_ioepoch = ioepoch;
363 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
364 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
365 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
366 OBD_MD_FLCTIME | OBD_MD_FLGROUP |
367 OBD_MD_FLATIME | OBD_MD_FLEPOCH;
369 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
370 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
373 set = ptlrpc_prep_set();
375 CERROR ("ENOMEM allocing request set\n");
378 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
380 rc = ptlrpc_set_wait(set);
381 ptlrpc_set_destroy(set);
386 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
387 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
390 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
391 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
392 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
393 (long long unsigned)llu_i2stat(inode)->st_size,
394 (long long unsigned)llu_i2stat(inode)->st_blocks,
395 (long long unsigned)llu_i2stat(inode)->st_blksize);
399 static struct inode* llu_new_inode(struct filesys *fs,
403 struct llu_inode_info *lli;
404 struct intnl_stat st = {
407 #ifndef AUTOMOUNT_FILE_NAME
408 .st_mode = fid->f_type & S_IFMT,
410 .st_mode = fid->f_type /* all of the bits! */
413 /* FIXME: fix this later */
420 OBD_ALLOC(lli, sizeof(*lli));
424 /* initialize lli here */
425 lli->lli_sbi = llu_fs2sbi(fs);
427 lli->lli_symlink_name = NULL;
429 lli->lli_maxbytes = (__u64)(~0UL);
430 lli->lli_file_data = NULL;
432 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
433 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
436 /* file identifier is needed by functions like _sysio_i_find() */
437 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
438 &st, 0, &llu_inode_ops, lli);
441 OBD_FREE(lli, sizeof(*lli));
446 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
448 struct lustre_handle lockh;
449 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
456 fid = &llu_i2info(inode)->lli_fid;
457 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
459 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
460 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
461 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
467 static int llu_inode_revalidate(struct inode *inode)
469 struct lov_stripe_md *lsm = NULL;
473 CERROR("REPORT THIS LINE TO PETER\n");
477 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
479 struct ptlrpc_request *req = NULL;
480 struct llu_sb_info *sbi = llu_i2sbi(inode);
481 struct md_op_data op_data = { { 0 } };
482 unsigned long valid = OBD_MD_FLGETATTR;
485 /* Why don't we update all valid MDS fields here, if we're
486 * doing an RPC anyways? -phil */
487 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
488 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
489 valid |= OBD_MD_FLEASIZE;
492 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, ealen,
494 op_data.op_valid = valid;
496 rc = md_getattr(sbi->ll_md_exp, &op_data, &req);
498 CERROR("failure %d inode %llu\n", rc,
499 (long long)llu_i2stat(inode)->st_ino);
502 rc = md_get_lustre_md(sbi->ll_md_exp, req,
503 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
505 /* XXX Too paranoid? */
506 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
507 !((md.body->valid & OBD_MD_FLNLINK) &&
508 (md.body->nlink == 0))) {
509 CERROR("Asked for %s eadata but got %s (%d)\n",
510 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
511 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
512 md.body->eadatasize);
515 ptlrpc_req_finished(req);
520 llu_update_inode(inode, &md);
521 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
522 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
523 ptlrpc_req_finished(req);
526 lsm = llu_i2info(inode)->lli_smd;
527 if (!lsm) /* object not yet allocated, don't validate size */
530 /* ll_glimpse_size will prefer locally cached writes if they extend
532 RETURN(cl_glimpse_size(inode));
535 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
537 *b = *llu_i2stat(ino);
540 static int llu_iop_getattr(struct pnode *pno,
542 struct intnl_stat *b)
547 liblustre_wait_event(0);
551 LASSERT(pno->p_base->pb_ino);
552 ino = pno->p_base->pb_ino;
554 LASSERT(!pno || pno->p_base->pb_ino == ino);
557 /* libsysio might call us directly without intent lock,
558 * we must re-fetch the attrs here
560 rc = llu_inode_revalidate(ino);
562 copy_stat_buf(ino, b);
563 LASSERT(!llu_i2info(ino)->lli_it);
566 liblustre_wait_event(0);
570 static int null_if_equal(struct ldlm_lock *lock, void *data)
572 if (data == lock->l_ast_data) {
573 lock->l_ast_data = NULL;
575 if (lock->l_req_mode != lock->l_granted_mode)
576 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
579 return LDLM_ITER_CONTINUE;
582 void llu_clear_inode(struct inode *inode)
584 struct llu_inode_info *lli = llu_i2info(inode);
585 struct llu_sb_info *sbi = llu_i2sbi(inode);
588 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
589 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
592 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
593 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
594 null_if_equal, inode);
597 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
598 null_if_equal, inode);
600 cl_inode_fini(inode);
603 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
607 if (lli->lli_symlink_name) {
608 OBD_FREE(lli->lli_symlink_name,
609 strlen(lli->lli_symlink_name) + 1);
610 lli->lli_symlink_name = NULL;
616 void llu_iop_gone(struct inode *inode)
618 struct llu_inode_info *lli = llu_i2info(inode);
621 liblustre_wait_event(0);
622 llu_clear_inode(inode);
624 OBD_FREE(lli, sizeof(*lli));
628 static int inode_setattr(struct inode * inode, struct iattr * attr)
630 unsigned int ia_valid = attr->ia_valid;
631 struct intnl_stat *st = llu_i2stat(inode);
635 * inode_setattr() is only ever invoked with ATTR_SIZE (by
636 * llu_setattr_raw()) when file has no bodies. Check this.
638 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
640 if (ia_valid & ATTR_SIZE)
641 st->st_size = attr->ia_size;
642 if (ia_valid & ATTR_UID)
643 st->st_uid = attr->ia_uid;
644 if (ia_valid & ATTR_GID)
645 st->st_gid = attr->ia_gid;
646 if (ia_valid & ATTR_ATIME)
647 st->st_atime = attr->ia_atime;
648 if (ia_valid & ATTR_MTIME)
649 st->st_mtime = attr->ia_mtime;
650 if (ia_valid & ATTR_CTIME)
651 st->st_ctime = attr->ia_ctime;
652 if (ia_valid & ATTR_MODE) {
653 st->st_mode = attr->ia_mode;
654 if (!cfs_curproc_is_in_groups(st->st_gid) &&
655 !cfs_capable(CFS_CAP_FSETID))
656 st->st_mode &= ~S_ISGID;
658 /* mark_inode_dirty(inode); */
662 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
663 struct md_open_data **mod)
666 struct llu_sb_info *sbi = llu_i2sbi(inode);
667 struct ptlrpc_request *request = NULL;
671 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
672 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
676 ptlrpc_req_finished(request);
677 if (rc != -EPERM && rc != -EACCES)
678 CERROR("md_setattr fails: rc = %d\n", rc);
682 rc = md_get_lustre_md(sbi->ll_md_exp, request,
683 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
685 ptlrpc_req_finished(request);
689 /* We call inode_setattr to adjust timestamps.
690 * If there is at least some data in file, we cleared ATTR_SIZE
691 * above to avoid invoking vmtruncate, otherwise it is important
692 * to call vmtruncate in inode_setattr to update inode->i_size
694 inode_setattr(inode, &op_data->op_attr);
695 llu_update_inode(inode, &md);
696 ptlrpc_req_finished(request);
701 /* Close IO epoch and send Size-on-MDS attribute update. */
702 static int llu_setattr_done_writing(struct inode *inode,
703 struct md_op_data *op_data,
704 struct md_open_data *mod)
706 struct llu_inode_info *lli = llu_i2info(inode);
707 struct intnl_stat *st = llu_i2stat(inode);
711 LASSERT(op_data != NULL);
712 if (!S_ISREG(st->st_mode))
715 /* XXX: pass och here for the recovery purpose. */
716 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
717 op_data->op_ioepoch, PFID(&lli->lli_fid));
719 op_data->op_flags = MF_EPOCH_CLOSE;
720 llu_done_writing_attr(inode, op_data);
721 llu_pack_inode2opdata(inode, op_data, NULL);
723 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
725 /* MDS has instructed us to obtain Size-on-MDS attribute
726 * from OSTs and send setattr to back to MDS. */
727 rc = llu_som_update(inode, op_data);
729 CERROR("inode %llu mdc truncate failed: rc = %d\n",
730 (unsigned long long)st->st_ino, rc);
735 /* If this inode has objects allocated to it (lsm != NULL), then the OST
736 * object(s) determine the file size and mtime. Otherwise, the MDS will
737 * keep these values until such a time that objects are allocated for it.
738 * We do the MDS operations first, as it is checking permissions for us.
739 * We don't to the MDS RPC if there is nothing that we want to store there,
740 * otherwise there is no harm in updating mtime/atime on the MDS if we are
741 * going to do an RPC anyways.
743 * If we are doing a truncate, we will send the mtime and ctime updates
744 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
745 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
748 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
750 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
751 struct intnl_stat *st = llu_i2stat(inode);
752 int ia_valid = attr->ia_valid;
753 struct md_op_data op_data = { { 0 } };
754 struct md_open_data *mod = NULL;
758 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
760 if (ia_valid & ATTR_SIZE) {
761 if (attr->ia_size > ll_file_maxbytes(inode)) {
762 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
763 (long long)attr->ia_size,
764 ll_file_maxbytes(inode));
768 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
771 /* We mark all of the fields "set" so MDS/OST does not re-set them */
772 if (attr->ia_valid & ATTR_CTIME) {
773 attr->ia_ctime = CFS_CURRENT_TIME;
774 attr->ia_valid |= ATTR_CTIME_SET;
776 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
777 attr->ia_atime = CFS_CURRENT_TIME;
778 attr->ia_valid |= ATTR_ATIME_SET;
780 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
781 attr->ia_mtime = CFS_CURRENT_TIME;
782 attr->ia_valid |= ATTR_MTIME_SET;
784 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
785 /* To avoid stale mtime on mds, obtain it from ost and send
787 rc = cl_glimpse_size(inode);
791 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
792 attr->ia_mtime = inode->i_stbuf.st_mtime;
795 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
796 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
797 ", now = "CFS_TIME_T"\n",
798 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
799 LTIME_S(CFS_CURRENT_TIME));
801 /* NB: ATTR_SIZE will only be set after this point if the size
802 * resides on the MDS, ie, this file has no objects. */
804 attr->ia_valid &= ~ATTR_SIZE;
806 /* If only OST attributes being set on objects, don't do MDS RPC.
807 * In that case, we need to check permissions and update the local
808 * inode ourselves so we can call obdo_from_inode() always. */
809 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
810 memcpy(&op_data.op_attr, attr, sizeof(*attr));
812 /* Open epoch for truncate. */
813 if (exp_connect_som(llu_i2mdexp(inode)) &&
814 (ia_valid & ATTR_SIZE))
815 op_data.op_flags = MF_EPOCH_OPEN;
816 rc = llu_md_setattr(inode, &op_data, &mod);
820 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
821 if (!lsm || !S_ISREG(st->st_mode)) {
822 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
826 /* The OST doesn't check permissions, but the alternative is
827 * a gratuitous RPC to the MDS. We already rely on the client
828 * to do read/write/truncate permission checks, so is mtime OK?
830 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
831 /* from sys_utime() */
832 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
833 if (current->fsuid != st->st_uid &&
834 (rc = ll_permission(inode, MAY_WRITE)) != 0)
837 /* from inode_change_ok() */
838 if (current->fsuid != st->st_uid &&
839 !cfs_capable(CFS_CAP_FOWNER))
845 /* Won't invoke llu_vmtruncate(), as we already cleared
847 inode_setattr(inode, attr);
850 if (ia_valid & ATTR_SIZE) {
851 rc = cl_setattr_do_truncate(inode, attr->ia_size, NULL);
852 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
853 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
854 (long long unsigned)st->st_ino, LTIME_S(attr->ia_mtime));
855 rc = cl_setattr_ost(inode, NULL);
859 if (op_data.op_ioepoch)
860 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
861 return rc ? rc : rc1;
864 /* here we simply act as a thin layer to glue it with
865 * llu_setattr_raw(), which is copy from kernel
867 static int llu_iop_setattr(struct pnode *pno,
870 struct intnl_stat *stbuf)
876 liblustre_wait_event(0);
878 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
879 SETATTR_UID | SETATTR_GID |
880 SETATTR_LEN | SETATTR_MODE)));
881 memset(&iattr, 0, sizeof(iattr));
883 if (mask & SETATTR_MODE) {
884 iattr.ia_mode = stbuf->st_mode;
885 iattr.ia_valid |= ATTR_MODE;
887 if (mask & SETATTR_MTIME) {
888 iattr.ia_mtime = stbuf->st_mtime;
889 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
891 if (mask & SETATTR_ATIME) {
892 iattr.ia_atime = stbuf->st_atime;
893 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
895 if (mask & SETATTR_UID) {
896 iattr.ia_uid = stbuf->st_uid;
897 iattr.ia_valid |= ATTR_UID;
899 if (mask & SETATTR_GID) {
900 iattr.ia_gid = stbuf->st_gid;
901 iattr.ia_valid |= ATTR_GID;
903 if (mask & SETATTR_LEN) {
904 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
905 iattr.ia_valid |= ATTR_SIZE;
908 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
909 iattr.ia_ctime = CFS_CURRENT_TIME;
911 rc = llu_setattr_raw(ino, &iattr);
912 liblustre_wait_idle();
916 #define EXT2_LINK_MAX 32000
918 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
920 struct inode *dir = pno->p_base->pb_parent->pb_ino;
921 struct qstr *qstr = &pno->p_base->pb_name;
922 const char *name = qstr->name;
924 struct ptlrpc_request *request = NULL;
925 struct llu_sb_info *sbi = llu_i2sbi(dir);
926 struct md_op_data op_data = {{ 0 }};
930 liblustre_wait_event(0);
931 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
934 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
937 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
938 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
939 cfs_curproc_cap_pack(), 0, &request);
940 ptlrpc_req_finished(request);
941 liblustre_wait_event(0);
945 static int llu_readlink_internal(struct inode *inode,
946 struct ptlrpc_request **request,
949 struct llu_inode_info *lli = llu_i2info(inode);
950 struct llu_sb_info *sbi = llu_i2sbi(inode);
951 struct mdt_body *body;
952 struct intnl_stat *st = llu_i2stat(inode);
953 struct md_op_data op_data = {{ 0 }};
954 int rc, symlen = st->st_size + 1;
960 if (lli->lli_symlink_name) {
961 *symname = lli->lli_symlink_name;
962 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
966 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, symlen,
968 op_data.op_valid = OBD_MD_LINKNAME;
970 rc = md_getattr(sbi->ll_md_exp, &op_data, request);
972 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
976 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
977 LASSERT(body != NULL);
979 if ((body->valid & OBD_MD_LINKNAME) == 0) {
980 CERROR ("OBD_MD_LINKNAME not set on reply\n");
981 GOTO (failed, rc = -EPROTO);
984 LASSERT(symlen != 0);
985 if (body->eadatasize != symlen) {
986 CERROR("inode %llu: symlink length %d not expected %d\n",
987 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
988 GOTO(failed, rc = -EPROTO);
991 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
992 if (*symname == NULL ||
993 strnlen(*symname, symlen) != symlen - 1) {
994 /* not full/NULL terminated */
995 CERROR("inode %llu: symlink not NULL terminated string"
996 "of length %d\n", (long long)st->st_ino, symlen - 1);
997 GOTO(failed, rc = -EPROTO);
1000 OBD_ALLOC(lli->lli_symlink_name, symlen);
1001 /* do not return an error if we cannot cache the symlink locally */
1002 if (lli->lli_symlink_name)
1003 memcpy(lli->lli_symlink_name, *symname, symlen);
1008 ptlrpc_req_finished (*request);
1012 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1014 struct inode *inode = pno->p_base->pb_ino;
1015 struct ptlrpc_request *request;
1020 liblustre_wait_event(0);
1021 rc = llu_readlink_internal(inode, &request, &symname);
1026 strncpy(data, symname, bufsize);
1027 rc = strlen(symname);
1029 ptlrpc_req_finished(request);
1031 liblustre_wait_event(0);
1035 static int llu_iop_mknod_raw(struct pnode *pno,
1039 struct ptlrpc_request *request = NULL;
1040 struct inode *dir = pno->p_parent->p_base->pb_ino;
1041 struct llu_sb_info *sbi = llu_i2sbi(dir);
1042 struct md_op_data op_data = {{ 0 }};
1046 liblustre_wait_event(0);
1047 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1048 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1049 (long long)llu_i2stat(dir)->st_ino);
1051 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1054 switch (mode & S_IFMT) {
1057 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1062 llu_prep_md_op_data(&op_data, dir, NULL,
1063 pno->p_base->pb_name.name,
1064 pno->p_base->pb_name.len, 0,
1067 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1068 current->fsuid, current->fsgid,
1069 cfs_curproc_cap_pack(), dev, &request);
1070 ptlrpc_req_finished(request);
1078 liblustre_wait_event(0);
1082 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1084 struct inode *src = old->p_base->pb_ino;
1085 struct inode *dir = new->p_parent->p_base->pb_ino;
1086 const char *name = new->p_base->pb_name.name;
1087 int namelen = new->p_base->pb_name.len;
1088 struct ptlrpc_request *request = NULL;
1089 struct md_op_data op_data = {{ 0 }};
1096 liblustre_wait_event(0);
1097 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1099 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1100 ptlrpc_req_finished(request);
1101 liblustre_wait_event(0);
1107 * libsysio will clear the inode immediately after return
1109 static int llu_iop_unlink_raw(struct pnode *pno)
1111 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1112 struct qstr *qstr = &pno->p_base->pb_name;
1113 const char *name = qstr->name;
1114 int len = qstr->len;
1115 struct inode *target = pno->p_base->pb_ino;
1116 struct ptlrpc_request *request = NULL;
1117 struct md_op_data op_data = { { 0 } };
1123 liblustre_wait_event(0);
1124 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1126 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1128 rc = llu_objects_destroy(request, dir);
1129 ptlrpc_req_finished(request);
1130 liblustre_wait_idle();
1135 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1137 struct inode *src = old->p_parent->p_base->pb_ino;
1138 struct inode *tgt = new->p_parent->p_base->pb_ino;
1139 const char *oldname = old->p_base->pb_name.name;
1140 int oldnamelen = old->p_base->pb_name.len;
1141 const char *newname = new->p_base->pb_name.name;
1142 int newnamelen = new->p_base->pb_name.len;
1143 struct ptlrpc_request *request = NULL;
1144 struct md_op_data op_data = { { 0 } };
1151 liblustre_wait_event(0);
1152 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1154 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1155 oldname, oldnamelen, newname, newnamelen,
1158 rc = llu_objects_destroy(request, src);
1161 ptlrpc_req_finished(request);
1162 liblustre_wait_idle();
1167 #ifdef _HAVE_STATVFS
1168 static int llu_statfs_internal(struct llu_sb_info *sbi,
1169 struct obd_statfs *osfs, __u64 max_age)
1171 struct obd_statfs obd_osfs;
1175 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1177 CERROR("md_statfs fails: rc = %d\n", rc);
1181 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1182 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1184 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1185 &obd_statfs, max_age, 0);
1187 CERROR("obd_statfs fails: rc = %d\n", rc);
1191 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1192 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1195 osfs->os_blocks = obd_osfs.os_blocks;
1196 osfs->os_bfree = obd_osfs.os_bfree;
1197 osfs->os_bavail = obd_osfs.os_bavail;
1199 /* If we don't have as many objects free on the OST as inodes
1200 * on the MDS, we reduce the total number of inodes to
1201 * compensate, so that the "inodes in use" number is correct.
1203 if (obd_osfs.os_ffree < osfs->os_ffree) {
1204 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1206 osfs->os_ffree = obd_osfs.os_ffree;
1212 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1214 struct obd_statfs osfs;
1217 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1219 /* For now we will always get up-to-date statfs values, but in the
1220 * future we may allow some amount of caching on the client (e.g.
1221 * from QOS or lprocfs updates). */
1222 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - CFS_HZ);
1226 statfs_unpack(sfs, &osfs);
1228 if (sizeof(sfs->f_blocks) == 4) {
1229 while (osfs.os_blocks > ~0UL) {
1232 osfs.os_blocks >>= 1;
1233 osfs.os_bfree >>= 1;
1234 osfs.os_bavail >>= 1;
1238 sfs->f_blocks = osfs.os_blocks;
1239 sfs->f_bfree = osfs.os_bfree;
1240 sfs->f_bavail = osfs.os_bavail;
1245 static int llu_iop_statvfs(struct pnode *pno,
1247 struct intnl_statvfs *buf)
1253 liblustre_wait_event(0);
1256 LASSERT(pno->p_base->pb_ino);
1257 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1261 /* from native driver */
1262 buf->f_bsize = fs.f_bsize; /* file system block size */
1263 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1264 buf->f_blocks = fs.f_blocks;
1265 buf->f_bfree = fs.f_bfree;
1266 buf->f_bavail = fs.f_bavail;
1267 buf->f_files = fs.f_files; /* Total number serial numbers */
1268 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1269 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1270 buf->f_fsid = fs.f_fsid.__val[1];
1271 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1272 buf->f_namemax = fs.f_namelen;
1275 liblustre_wait_event(0);
1278 #endif /* _HAVE_STATVFS */
1280 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1282 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1283 struct qstr *qstr = &pno->p_base->pb_name;
1284 const char *name = qstr->name;
1285 int len = qstr->len;
1286 struct ptlrpc_request *request = NULL;
1287 struct intnl_stat *st = llu_i2stat(dir);
1288 struct md_op_data op_data = {{ 0 }};
1292 liblustre_wait_event(0);
1293 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1294 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1296 if (st->st_nlink >= EXT2_LINK_MAX)
1299 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1302 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1303 mode | S_IFDIR, current->fsuid, current->fsgid,
1304 cfs_curproc_cap_pack(), 0, &request);
1305 ptlrpc_req_finished(request);
1306 liblustre_wait_event(0);
1310 static int llu_iop_rmdir_raw(struct pnode *pno)
1312 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1313 struct qstr *qstr = &pno->p_base->pb_name;
1314 const char *name = qstr->name;
1315 int len = qstr->len;
1316 struct ptlrpc_request *request = NULL;
1317 struct md_op_data op_data = {{ 0 }};
1321 liblustre_wait_event(0);
1322 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1323 (long long)llu_i2stat(dir)->st_ino,
1324 llu_i2info(dir)->lli_st_generation, dir);
1326 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1328 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1329 ptlrpc_req_finished(request);
1331 liblustre_wait_event(0);
1336 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1338 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1340 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1342 /* refer to ll_file_flock() for details */
1343 static int llu_file_flock(struct inode *ino,
1345 struct file_lock *file_lock)
1347 struct llu_inode_info *lli = llu_i2info(ino);
1348 struct intnl_stat *st = llu_i2stat(ino);
1349 struct ldlm_res_id res_id =
1350 { .name = {fid_seq(&lli->lli_fid),
1351 fid_oid(&lli->lli_fid),
1352 fid_ver(&lli->lli_fid),
1354 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1355 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1357 struct lustre_handle lockh = {0};
1358 ldlm_policy_data_t flock;
1362 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1363 (unsigned long long)st->st_ino, file_lock);
1365 flock.l_flock.pid = file_lock->fl_pid;
1366 flock.l_flock.start = file_lock->fl_start;
1367 flock.l_flock.end = file_lock->fl_end;
1369 switch (file_lock->fl_type) {
1371 einfo.ei_mode = LCK_PR;
1374 einfo.ei_mode = LCK_NL;
1377 einfo.ei_mode = LCK_PW;
1380 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1387 #if F_SETLKW64 != F_SETLKW
1395 #if F_SETLK64 != F_SETLK
1399 flags = LDLM_FL_BLOCK_NOWAIT;
1403 #if F_GETLK64 != F_GETLK
1407 flags = LDLM_FL_TEST_LOCK;
1408 file_lock->fl_type = einfo.ei_mode;
1411 CERROR("unknown fcntl cmd: %d\n", cmd);
1415 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1416 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1417 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1421 struct lmv_obd *lmv;
1422 struct obd_device *lmv_obd;
1423 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1424 lmv = &lmv_obd->u.lmv;
1426 if (lmv->desc.ld_tgt_count < 1)
1427 RETURN(rc = -ENODEV);
1429 if (lmv->tgts[0].ltd_exp != NULL)
1430 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1431 &flock, &flags, NULL, 0, &lockh, 0);
1438 static int assign_type(struct file_lock *fl, int type)
1451 static int flock_to_posix_lock(struct inode *ino,
1452 struct file_lock *fl,
1455 switch (l->l_whence) {
1456 /* XXX: only SEEK_SET is supported in lustre */
1464 fl->fl_end = l->l_len - 1;
1468 fl->fl_end = OFFSET_MAX;
1470 fl->fl_pid = getpid();
1471 fl->fl_flags = FL_POSIX;
1472 fl->fl_notify = NULL;
1473 fl->fl_insert = NULL;
1474 fl->fl_remove = NULL;
1475 /* XXX: these fields can't be filled with suitable values,
1476 but I think lustre doesn't use them.
1478 fl->fl_owner = NULL;
1481 return assign_type(fl, l->l_type);
1484 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1486 struct file_lock fl;
1490 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1493 error = flock_to_posix_lock(ino, &fl, flock);
1497 error = llu_file_flock(ino, F_GETLK, &fl);
1501 flock->l_type = F_UNLCK;
1502 if (fl.fl_type != F_UNLCK) {
1503 flock->l_pid = fl.fl_pid;
1504 flock->l_start = fl.fl_start;
1505 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1506 fl.fl_end - fl.fl_start + 1;
1507 flock->l_whence = SEEK_SET;
1508 flock->l_type = fl.fl_type;
1515 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1517 struct file_lock fl;
1518 int flags = llu_i2info(ino)->lli_open_flags + 1;
1521 error = flock_to_posix_lock(ino, &fl, flock);
1524 if (cmd == F_SETLKW)
1525 fl.fl_flags |= FL_SLEEP;
1528 switch (flock->l_type) {
1530 if (!(flags & FMODE_READ))
1534 if (!(flags & FMODE_WRITE))
1544 error = llu_file_flock(ino, cmd, &fl);
1552 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1554 struct llu_inode_info *lli = llu_i2info(ino);
1556 struct flock *flock;
1559 liblustre_wait_event(0);
1562 *rtn = lli->lli_open_flags;
1565 flags = va_arg(ap, long);
1566 flags &= FCNTL_FLMASK;
1567 if (flags & FCNTL_FLMASK_INVALID) {
1568 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1569 "the O_NONBLOCK or O_ASYNC flags. "
1570 "Please fix your application.\n");
1575 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1576 (lli->lli_open_flags & ~FCNTL_FLMASK);
1581 #if F_GETLK64 != F_GETLK
1585 flock = va_arg(ap, struct flock *);
1586 err = llu_fcntl_getlk(ino, flock);
1591 #if F_SETLKW64 != F_SETLKW
1597 #if F_SETLK64 != F_SETLK
1601 flock = va_arg(ap, struct flock *);
1602 err = llu_fcntl_setlk(ino, cmd, flock);
1606 CERROR("unsupported fcntl cmd %x\n", cmd);
1612 liblustre_wait_event(0);
1616 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1618 struct llu_inode_info *lli = llu_i2info(inode);
1619 struct ll_file_data *fd = lli->lli_file_data;
1621 struct ccc_grouplock grouplock;
1624 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1627 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1630 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1632 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1633 arg, (lli->lli_open_flags & O_NONBLOCK),
1639 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1640 fd->fd_grouplock = grouplock;
1645 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1647 struct llu_inode_info *lli = llu_i2info(inode);
1648 struct ll_file_data *fd = lli->lli_file_data;
1649 struct ccc_grouplock grouplock;
1652 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1655 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1657 if (fd->fd_grouplock.cg_gid != arg)
1660 grouplock = fd->fd_grouplock;
1661 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1662 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1664 cl_put_grouplock(&grouplock);
1669 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1671 struct llu_sb_info *sbi = llu_i2sbi(ino);
1672 struct ptlrpc_request *request = NULL;
1673 struct md_op_data op_data = {{ 0 }};
1674 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1677 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1680 LASSERT(sizeof(lum) == sizeof(*lump));
1681 LASSERT(sizeof(lum.lmm_objects[0]) ==
1682 sizeof(lump->lmm_objects[0]));
1683 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1686 switch (lum.lmm_magic) {
1687 case LOV_USER_MAGIC_V1: {
1688 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1689 lustre_swab_lov_user_md_v1(&lum);
1692 case LOV_USER_MAGIC_V3: {
1693 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1694 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1698 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1699 " %#08x != %#08x nor %#08x\n",
1700 lum.lmm_magic, LOV_USER_MAGIC_V1,
1706 /* swabbing is done in lov_setstripe() on server side */
1707 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1708 sizeof(lum), NULL, 0, &request, NULL);
1710 ptlrpc_req_finished(request);
1711 if (rc != -EPERM && rc != -EACCES)
1712 CERROR("md_setattr fails: rc = %d\n", rc);
1715 ptlrpc_req_finished(request);
1720 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1721 struct lov_user_md *lum, int lum_size)
1723 struct llu_sb_info *sbi = llu_i2sbi(ino);
1724 struct llu_inode_info *lli = llu_i2info(ino);
1725 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1726 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1727 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1728 struct ptlrpc_request *req = NULL;
1729 struct lustre_md md;
1730 struct md_op_data data = {{ 0 }};
1731 struct lustre_handle lockh;
1736 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1737 PFID(&lli->lli_fid));
1741 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1743 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1744 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1748 req = oit.d.lustre.it_data;
1749 rc = it_open_error(DISP_IT_EXECD, &oit);
1755 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1761 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1762 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1766 llu_update_inode(ino, &md);
1767 llu_local_open(lli, &oit);
1768 /* release intent */
1769 if (lustre_handle_is_used(&lockh))
1770 ldlm_lock_decref(&lockh, LCK_CR);
1771 ptlrpc_req_finished(req);
1773 rc = llu_file_release(ino);
1778 ptlrpc_req_finished(req);
1782 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1784 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1786 int flags = FMODE_WRITE;
1789 LASSERT(sizeof(lum) == sizeof(*lump));
1790 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1791 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1794 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1798 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1800 struct intnl_stat *st = llu_i2stat(ino);
1801 if (S_ISREG(st->st_mode))
1802 return llu_lov_file_setstripe(ino, arg);
1803 if (S_ISDIR(st->st_mode))
1804 return llu_lov_dir_setstripe(ino, arg);
1809 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1811 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1816 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1820 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1826 liblustre_wait_event(0);
1829 case LL_IOC_GROUP_LOCK:
1830 arg = va_arg(ap, unsigned long);
1831 rc = llu_get_grouplock(ino, arg);
1833 case LL_IOC_GROUP_UNLOCK:
1834 arg = va_arg(ap, unsigned long);
1835 rc = llu_put_grouplock(ino, arg);
1837 case LL_IOC_LOV_SETSTRIPE:
1838 arg = va_arg(ap, unsigned long);
1839 rc = llu_lov_setstripe(ino, arg);
1841 case LL_IOC_LOV_GETSTRIPE:
1842 arg = va_arg(ap, unsigned long);
1843 rc = llu_lov_getstripe(ino, arg);
1846 CERROR("did not support ioctl cmd %lx\n", request);
1851 liblustre_wait_event(0);
1856 * we already do syncronous read/write
1858 static int llu_iop_sync(struct inode *inode)
1860 liblustre_wait_event(0);
1864 static int llu_iop_datasync(struct inode *inode)
1866 liblustre_wait_event(0);
1870 struct filesys_ops llu_filesys_ops =
1872 fsop_gone: llu_fsop_gone,
1875 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1877 struct inode *inode;
1879 struct file_identifier fileid = {&fid, sizeof(fid)};
1881 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1882 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1883 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1885 return ERR_PTR(-EPERM);
1888 /* try to find existing inode */
1889 fid = md->body->fid1;
1891 inode = _sysio_i_find(fs, &fileid);
1893 if (inode->i_zombie/* ||
1894 lli->lli_st_generation != md->body->generation*/) {
1898 llu_update_inode(inode, md);
1903 inode = llu_new_inode(fs, &fid);
1905 llu_update_inode(inode, md);
1911 llu_fsswop_mount(const char *source,
1913 const void *data __IS_UNUSED,
1914 struct pnode *tocover,
1915 struct mount **mntp)
1919 struct pnode_base *rootpb;
1920 struct obd_device *obd;
1921 struct llu_sb_info *sbi;
1922 struct obd_statfs osfs;
1923 static struct qstr noname = { NULL, 0, 0 };
1924 struct ptlrpc_request *request = NULL;
1925 struct lustre_md md;
1927 struct config_llog_instance cfg = {0, };
1928 char ll_instance[sizeof(sbi) * 2 + 1];
1929 struct lustre_profile *lprof;
1930 char *zconf_mgsnid, *zconf_profile;
1931 char *osc = NULL, *mdc = NULL;
1932 int async = 1, err = -EINVAL;
1933 struct obd_connect_data ocd = {0,};
1934 struct md_op_data op_data = {{0}};
1938 if (ll_parse_mount_target(source,
1941 CERROR("mal-formed target %s\n", source);
1944 if (!zconf_mgsnid || !zconf_profile) {
1945 printf("Liblustre: invalid target %s\n", source);
1948 /* allocate & initialize sbi */
1949 OBD_ALLOC(sbi, sizeof(*sbi));
1953 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1954 ll_generate_random_uuid(uuid);
1955 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1957 /* generate a string unique to this super, let's try
1958 the address of the super itself.*/
1959 sprintf(ll_instance, "%p", sbi);
1961 /* retrive & parse config log */
1962 cfg.cfg_instance = ll_instance;
1963 cfg.cfg_uuid = sbi->ll_sb_uuid;
1964 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1966 CERROR("Unable to process log: %s\n", zconf_profile);
1967 GOTO(out_free, err);
1970 lprof = class_get_profile(zconf_profile);
1971 if (lprof == NULL) {
1972 CERROR("No profile found: %s\n", zconf_profile);
1973 GOTO(out_free, err = -EINVAL);
1975 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1976 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
1978 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
1979 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
1983 GOTO(out_free, err = -EINVAL);
1987 GOTO(out_free, err = -EINVAL);
1990 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1996 obd = class_name2obd(mdc);
1998 CERROR("MDC %s: not setup or attached\n", mdc);
1999 GOTO(out_free, err = -EINVAL);
2001 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2002 sizeof(async), &async, NULL);
2004 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2005 OBD_CONNECT_FID | OBD_CONNECT_AT |
2006 OBD_CONNECT_VBR | OBD_CONNECT_SOM;
2007 #ifdef LIBLUSTRE_POSIX_ACL
2008 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2010 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2011 ocd.ocd_version = LUSTRE_VERSION_CODE;
2014 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2016 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2017 GOTO(out_free, err);
2020 err = obd_statfs(obd, &osfs, 100000000, 0);
2025 * FIXME fill fs stat data into sbi here!!! FIXME
2029 obd = class_name2obd(osc);
2031 CERROR("OSC %s: not setup or attached\n", osc);
2032 GOTO(out_md, err = -EINVAL);
2034 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2035 sizeof(async), &async, NULL);
2037 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2038 obd->obd_upcall.onu_upcall = cl_ocd_update;
2040 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2041 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2042 OBD_CONNECT_FID | OBD_CONNECT_AT |
2045 ocd.ocd_version = LUSTRE_VERSION_CODE;
2046 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2048 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2051 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2052 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
2053 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
2055 fid_zero(&sbi->ll_root_fid);
2056 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
2058 CERROR("cannot mds_connect: rc = %d\n", err);
2059 GOTO(out_lock_cn_cb, err);
2061 if (!fid_is_sane(&sbi->ll_root_fid)) {
2062 CERROR("Invalid root fid during mount\n");
2063 GOTO(out_lock_cn_cb, err = -EINVAL);
2065 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
2067 op_data.op_fid1 = sbi->ll_root_fid;
2068 op_data.op_valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS;
2069 /* fetch attr of root inode */
2070 err = md_getattr(sbi->ll_md_exp, &op_data, &request);
2072 CERROR("md_getattr failed for root: rc = %d\n", err);
2073 GOTO(out_lock_cn_cb, err);
2076 err = md_get_lustre_md(sbi->ll_md_exp, request,
2077 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2079 CERROR("failed to understand root inode md: rc = %d\n",err);
2080 GOTO(out_request, err);
2083 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2085 root = llu_iget(fs, &md);
2086 if (!root || IS_ERR(root)) {
2087 CERROR("fail to generate root inode\n");
2088 GOTO(out_request, err = -EBADF);
2092 * Generate base path-node for root.
2094 rootpb = _sysio_pb_new(&noname, NULL, root);
2100 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2102 _sysio_pb_gone(rootpb);
2108 ptlrpc_req_finished(request);
2110 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2111 liblustre_wait_idle();
2116 _sysio_i_gone(root);
2118 ptlrpc_req_finished(request);
2120 obd_disconnect(sbi->ll_dt_exp);
2122 obd_disconnect(sbi->ll_md_exp);
2125 OBD_FREE(osc, strlen(osc) + 1);
2127 OBD_FREE(mdc, strlen(mdc) + 1);
2128 OBD_FREE(sbi, sizeof(*sbi));
2129 liblustre_wait_idle();
2133 struct fssw_ops llu_fssw_ops = {
2137 static struct inode_ops llu_inode_ops = {
2138 inop_lookup: llu_iop_lookup,
2139 inop_getattr: llu_iop_getattr,
2140 inop_setattr: llu_iop_setattr,
2141 inop_filldirentries: llu_iop_filldirentries,
2142 inop_mkdir: llu_iop_mkdir_raw,
2143 inop_rmdir: llu_iop_rmdir_raw,
2144 inop_symlink: llu_iop_symlink_raw,
2145 inop_readlink: llu_iop_readlink,
2146 inop_open: llu_iop_open,
2147 inop_close: llu_iop_close,
2148 inop_link: llu_iop_link_raw,
2149 inop_unlink: llu_iop_unlink_raw,
2150 inop_rename: llu_iop_rename_raw,
2151 inop_pos: llu_iop_pos,
2152 inop_read: llu_iop_read,
2153 inop_write: llu_iop_write,
2154 inop_iodone: llu_iop_iodone,
2155 inop_fcntl: llu_iop_fcntl,
2156 inop_sync: llu_iop_sync,
2157 inop_datasync: llu_iop_datasync,
2158 inop_ioctl: llu_iop_ioctl,
2159 inop_mknod: llu_iop_mknod_raw,
2160 #ifdef _HAVE_STATVFS
2161 inop_statvfs: llu_iop_statvfs,
2163 inop_gone: llu_iop_gone,