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 unsigned long valid = OBD_MD_FLGETATTR;
484 /* Why don't we update all valid MDS fields here, if we're
485 * doing an RPC anyways? -phil */
486 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
487 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
488 valid |= OBD_MD_FLEASIZE;
490 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
491 NULL, valid, ealen, &req);
493 CERROR("failure %d inode %llu\n", rc,
494 (long long)llu_i2stat(inode)->st_ino);
497 rc = md_get_lustre_md(sbi->ll_md_exp, req,
498 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
500 /* XXX Too paranoid? */
501 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
502 !((md.body->valid & OBD_MD_FLNLINK) &&
503 (md.body->nlink == 0))) {
504 CERROR("Asked for %s eadata but got %s (%d)\n",
505 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
506 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
507 md.body->eadatasize);
510 ptlrpc_req_finished(req);
515 llu_update_inode(inode, &md);
516 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
517 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
518 ptlrpc_req_finished(req);
521 lsm = llu_i2info(inode)->lli_smd;
522 if (!lsm) /* object not yet allocated, don't validate size */
525 /* ll_glimpse_size will prefer locally cached writes if they extend
527 RETURN(cl_glimpse_size(inode));
530 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
532 *b = *llu_i2stat(ino);
535 static int llu_iop_getattr(struct pnode *pno,
537 struct intnl_stat *b)
542 liblustre_wait_event(0);
546 LASSERT(pno->p_base->pb_ino);
547 ino = pno->p_base->pb_ino;
549 LASSERT(!pno || pno->p_base->pb_ino == ino);
552 /* libsysio might call us directly without intent lock,
553 * we must re-fetch the attrs here
555 rc = llu_inode_revalidate(ino);
557 copy_stat_buf(ino, b);
558 LASSERT(!llu_i2info(ino)->lli_it);
561 liblustre_wait_event(0);
565 static int null_if_equal(struct ldlm_lock *lock, void *data)
567 if (data == lock->l_ast_data) {
568 lock->l_ast_data = NULL;
570 if (lock->l_req_mode != lock->l_granted_mode)
571 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
574 return LDLM_ITER_CONTINUE;
577 void llu_clear_inode(struct inode *inode)
579 struct llu_inode_info *lli = llu_i2info(inode);
580 struct llu_sb_info *sbi = llu_i2sbi(inode);
583 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
584 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
587 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
588 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
589 null_if_equal, inode);
592 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
593 null_if_equal, inode);
595 cl_inode_fini(inode);
598 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
602 if (lli->lli_symlink_name) {
603 OBD_FREE(lli->lli_symlink_name,
604 strlen(lli->lli_symlink_name) + 1);
605 lli->lli_symlink_name = NULL;
611 void llu_iop_gone(struct inode *inode)
613 struct llu_inode_info *lli = llu_i2info(inode);
616 liblustre_wait_event(0);
617 llu_clear_inode(inode);
619 OBD_FREE(lli, sizeof(*lli));
623 static int inode_setattr(struct inode * inode, struct iattr * attr)
625 unsigned int ia_valid = attr->ia_valid;
626 struct intnl_stat *st = llu_i2stat(inode);
630 * inode_setattr() is only ever invoked with ATTR_SIZE (by
631 * llu_setattr_raw()) when file has no bodies. Check this.
633 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
635 if (ia_valid & ATTR_SIZE)
636 st->st_size = attr->ia_size;
637 if (ia_valid & ATTR_UID)
638 st->st_uid = attr->ia_uid;
639 if (ia_valid & ATTR_GID)
640 st->st_gid = attr->ia_gid;
641 if (ia_valid & ATTR_ATIME)
642 st->st_atime = attr->ia_atime;
643 if (ia_valid & ATTR_MTIME)
644 st->st_mtime = attr->ia_mtime;
645 if (ia_valid & ATTR_CTIME)
646 st->st_ctime = attr->ia_ctime;
647 if (ia_valid & ATTR_MODE) {
648 st->st_mode = attr->ia_mode;
649 if (!cfs_curproc_is_in_groups(st->st_gid) &&
650 !cfs_capable(CFS_CAP_FSETID))
651 st->st_mode &= ~S_ISGID;
653 /* mark_inode_dirty(inode); */
657 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
658 struct md_open_data **mod)
661 struct llu_sb_info *sbi = llu_i2sbi(inode);
662 struct ptlrpc_request *request = NULL;
666 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
667 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
671 ptlrpc_req_finished(request);
672 if (rc != -EPERM && rc != -EACCES)
673 CERROR("md_setattr fails: rc = %d\n", rc);
677 rc = md_get_lustre_md(sbi->ll_md_exp, request,
678 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
680 ptlrpc_req_finished(request);
684 /* We call inode_setattr to adjust timestamps.
685 * If there is at least some data in file, we cleared ATTR_SIZE
686 * above to avoid invoking vmtruncate, otherwise it is important
687 * to call vmtruncate in inode_setattr to update inode->i_size
689 inode_setattr(inode, &op_data->op_attr);
690 llu_update_inode(inode, &md);
691 ptlrpc_req_finished(request);
696 /* Close IO epoch and send Size-on-MDS attribute update. */
697 static int llu_setattr_done_writing(struct inode *inode,
698 struct md_op_data *op_data,
699 struct md_open_data *mod)
701 struct llu_inode_info *lli = llu_i2info(inode);
702 struct intnl_stat *st = llu_i2stat(inode);
706 LASSERT(op_data != NULL);
707 if (!S_ISREG(st->st_mode))
710 /* XXX: pass och here for the recovery purpose. */
711 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
712 op_data->op_ioepoch, PFID(&lli->lli_fid));
714 op_data->op_flags = MF_EPOCH_CLOSE;
715 llu_done_writing_attr(inode, op_data);
716 llu_pack_inode2opdata(inode, op_data, NULL);
718 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
720 /* MDS has instructed us to obtain Size-on-MDS attribute
721 * from OSTs and send setattr to back to MDS. */
722 rc = llu_som_update(inode, op_data);
724 CERROR("inode %llu mdc truncate failed: rc = %d\n",
725 (unsigned long long)st->st_ino, rc);
730 /* If this inode has objects allocated to it (lsm != NULL), then the OST
731 * object(s) determine the file size and mtime. Otherwise, the MDS will
732 * keep these values until such a time that objects are allocated for it.
733 * We do the MDS operations first, as it is checking permissions for us.
734 * We don't to the MDS RPC if there is nothing that we want to store there,
735 * otherwise there is no harm in updating mtime/atime on the MDS if we are
736 * going to do an RPC anyways.
738 * If we are doing a truncate, we will send the mtime and ctime updates
739 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
740 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
743 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
745 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
746 struct intnl_stat *st = llu_i2stat(inode);
747 int ia_valid = attr->ia_valid;
748 struct md_op_data op_data = { { 0 } };
749 struct md_open_data *mod = NULL;
753 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
755 if (ia_valid & ATTR_SIZE) {
756 if (attr->ia_size > ll_file_maxbytes(inode)) {
757 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
758 (long long)attr->ia_size,
759 ll_file_maxbytes(inode));
763 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
766 /* We mark all of the fields "set" so MDS/OST does not re-set them */
767 if (attr->ia_valid & ATTR_CTIME) {
768 attr->ia_ctime = CFS_CURRENT_TIME;
769 attr->ia_valid |= ATTR_CTIME_SET;
771 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
772 attr->ia_atime = CFS_CURRENT_TIME;
773 attr->ia_valid |= ATTR_ATIME_SET;
775 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
776 attr->ia_mtime = CFS_CURRENT_TIME;
777 attr->ia_valid |= ATTR_MTIME_SET;
779 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
780 /* To avoid stale mtime on mds, obtain it from ost and send
782 rc = cl_glimpse_size(inode);
786 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
787 attr->ia_mtime = inode->i_stbuf.st_mtime;
790 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
791 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
792 ", now = "CFS_TIME_T"\n",
793 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
794 LTIME_S(CFS_CURRENT_TIME));
796 /* NB: ATTR_SIZE will only be set after this point if the size
797 * resides on the MDS, ie, this file has no objects. */
799 attr->ia_valid &= ~ATTR_SIZE;
801 /* If only OST attributes being set on objects, don't do MDS RPC.
802 * In that case, we need to check permissions and update the local
803 * inode ourselves so we can call obdo_from_inode() always. */
804 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
805 memcpy(&op_data.op_attr, attr, sizeof(*attr));
807 /* Open epoch for truncate. */
808 if (exp_connect_som(llu_i2mdexp(inode)) &&
809 (ia_valid & ATTR_SIZE))
810 op_data.op_flags = MF_EPOCH_OPEN;
811 rc = llu_md_setattr(inode, &op_data, &mod);
815 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
816 if (!lsm || !S_ISREG(st->st_mode)) {
817 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
821 /* The OST doesn't check permissions, but the alternative is
822 * a gratuitous RPC to the MDS. We already rely on the client
823 * to do read/write/truncate permission checks, so is mtime OK?
825 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
826 /* from sys_utime() */
827 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
828 if (current->fsuid != st->st_uid &&
829 (rc = ll_permission(inode, MAY_WRITE)) != 0)
832 /* from inode_change_ok() */
833 if (current->fsuid != st->st_uid &&
834 !cfs_capable(CFS_CAP_FOWNER))
840 /* Won't invoke llu_vmtruncate(), as we already cleared
842 inode_setattr(inode, attr);
845 if (ia_valid & ATTR_SIZE) {
846 rc = cl_setattr_do_truncate(inode, attr->ia_size, NULL);
847 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
848 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
849 (long long unsigned)st->st_ino, LTIME_S(attr->ia_mtime));
850 rc = cl_setattr_ost(inode, NULL);
854 if (op_data.op_ioepoch)
855 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
856 return rc ? rc : rc1;
859 /* here we simply act as a thin layer to glue it with
860 * llu_setattr_raw(), which is copy from kernel
862 static int llu_iop_setattr(struct pnode *pno,
865 struct intnl_stat *stbuf)
871 liblustre_wait_event(0);
873 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
874 SETATTR_UID | SETATTR_GID |
875 SETATTR_LEN | SETATTR_MODE)));
876 memset(&iattr, 0, sizeof(iattr));
878 if (mask & SETATTR_MODE) {
879 iattr.ia_mode = stbuf->st_mode;
880 iattr.ia_valid |= ATTR_MODE;
882 if (mask & SETATTR_MTIME) {
883 iattr.ia_mtime = stbuf->st_mtime;
884 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
886 if (mask & SETATTR_ATIME) {
887 iattr.ia_atime = stbuf->st_atime;
888 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
890 if (mask & SETATTR_UID) {
891 iattr.ia_uid = stbuf->st_uid;
892 iattr.ia_valid |= ATTR_UID;
894 if (mask & SETATTR_GID) {
895 iattr.ia_gid = stbuf->st_gid;
896 iattr.ia_valid |= ATTR_GID;
898 if (mask & SETATTR_LEN) {
899 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
900 iattr.ia_valid |= ATTR_SIZE;
903 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
904 iattr.ia_ctime = CFS_CURRENT_TIME;
906 rc = llu_setattr_raw(ino, &iattr);
907 liblustre_wait_idle();
911 #define EXT2_LINK_MAX 32000
913 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
915 struct inode *dir = pno->p_base->pb_parent->pb_ino;
916 struct qstr *qstr = &pno->p_base->pb_name;
917 const char *name = qstr->name;
919 struct ptlrpc_request *request = NULL;
920 struct llu_sb_info *sbi = llu_i2sbi(dir);
921 struct md_op_data op_data = {{ 0 }};
925 liblustre_wait_event(0);
926 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
929 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
932 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
933 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
934 cfs_curproc_cap_pack(), 0, &request);
935 ptlrpc_req_finished(request);
936 liblustre_wait_event(0);
940 static int llu_readlink_internal(struct inode *inode,
941 struct ptlrpc_request **request,
944 struct llu_inode_info *lli = llu_i2info(inode);
945 struct llu_sb_info *sbi = llu_i2sbi(inode);
946 struct mdt_body *body;
947 struct intnl_stat *st = llu_i2stat(inode);
948 int rc, symlen = st->st_size + 1;
954 if (lli->lli_symlink_name) {
955 *symname = lli->lli_symlink_name;
956 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
960 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
961 OBD_MD_LINKNAME, symlen, request);
963 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
967 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
968 LASSERT(body != NULL);
970 if ((body->valid & OBD_MD_LINKNAME) == 0) {
971 CERROR ("OBD_MD_LINKNAME not set on reply\n");
972 GOTO (failed, rc = -EPROTO);
975 LASSERT(symlen != 0);
976 if (body->eadatasize != symlen) {
977 CERROR("inode %llu: symlink length %d not expected %d\n",
978 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
979 GOTO(failed, rc = -EPROTO);
982 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
983 if (*symname == NULL ||
984 strnlen(*symname, symlen) != symlen - 1) {
985 /* not full/NULL terminated */
986 CERROR("inode %llu: symlink not NULL terminated string"
987 "of length %d\n", (long long)st->st_ino, symlen - 1);
988 GOTO(failed, rc = -EPROTO);
991 OBD_ALLOC(lli->lli_symlink_name, symlen);
992 /* do not return an error if we cannot cache the symlink locally */
993 if (lli->lli_symlink_name)
994 memcpy(lli->lli_symlink_name, *symname, symlen);
999 ptlrpc_req_finished (*request);
1003 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1005 struct inode *inode = pno->p_base->pb_ino;
1006 struct ptlrpc_request *request;
1011 liblustre_wait_event(0);
1012 rc = llu_readlink_internal(inode, &request, &symname);
1017 strncpy(data, symname, bufsize);
1018 rc = strlen(symname);
1020 ptlrpc_req_finished(request);
1022 liblustre_wait_event(0);
1026 static int llu_iop_mknod_raw(struct pnode *pno,
1030 struct ptlrpc_request *request = NULL;
1031 struct inode *dir = pno->p_parent->p_base->pb_ino;
1032 struct llu_sb_info *sbi = llu_i2sbi(dir);
1033 struct md_op_data op_data = {{ 0 }};
1037 liblustre_wait_event(0);
1038 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1039 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1040 (long long)llu_i2stat(dir)->st_ino);
1042 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1045 switch (mode & S_IFMT) {
1048 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1053 llu_prep_md_op_data(&op_data, dir, NULL,
1054 pno->p_base->pb_name.name,
1055 pno->p_base->pb_name.len, 0,
1058 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1059 current->fsuid, current->fsgid,
1060 cfs_curproc_cap_pack(), dev, &request);
1061 ptlrpc_req_finished(request);
1069 liblustre_wait_event(0);
1073 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1075 struct inode *src = old->p_base->pb_ino;
1076 struct inode *dir = new->p_parent->p_base->pb_ino;
1077 const char *name = new->p_base->pb_name.name;
1078 int namelen = new->p_base->pb_name.len;
1079 struct ptlrpc_request *request = NULL;
1080 struct md_op_data op_data = {{ 0 }};
1087 liblustre_wait_event(0);
1088 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1090 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1091 ptlrpc_req_finished(request);
1092 liblustre_wait_event(0);
1098 * libsysio will clear the inode immediately after return
1100 static int llu_iop_unlink_raw(struct pnode *pno)
1102 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1103 struct qstr *qstr = &pno->p_base->pb_name;
1104 const char *name = qstr->name;
1105 int len = qstr->len;
1106 struct inode *target = pno->p_base->pb_ino;
1107 struct ptlrpc_request *request = NULL;
1108 struct md_op_data op_data = { { 0 } };
1114 liblustre_wait_event(0);
1115 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1117 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1119 rc = llu_objects_destroy(request, dir);
1120 ptlrpc_req_finished(request);
1121 liblustre_wait_idle();
1126 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1128 struct inode *src = old->p_parent->p_base->pb_ino;
1129 struct inode *tgt = new->p_parent->p_base->pb_ino;
1130 const char *oldname = old->p_base->pb_name.name;
1131 int oldnamelen = old->p_base->pb_name.len;
1132 const char *newname = new->p_base->pb_name.name;
1133 int newnamelen = new->p_base->pb_name.len;
1134 struct ptlrpc_request *request = NULL;
1135 struct md_op_data op_data = { { 0 } };
1142 liblustre_wait_event(0);
1143 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1145 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1146 oldname, oldnamelen, newname, newnamelen,
1149 rc = llu_objects_destroy(request, src);
1152 ptlrpc_req_finished(request);
1153 liblustre_wait_idle();
1158 #ifdef _HAVE_STATVFS
1159 static int llu_statfs_internal(struct llu_sb_info *sbi,
1160 struct obd_statfs *osfs, __u64 max_age)
1162 struct obd_statfs obd_osfs;
1166 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1168 CERROR("md_statfs fails: rc = %d\n", rc);
1172 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1173 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1175 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1176 &obd_statfs, max_age, 0);
1178 CERROR("obd_statfs fails: rc = %d\n", rc);
1182 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1183 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1186 osfs->os_blocks = obd_osfs.os_blocks;
1187 osfs->os_bfree = obd_osfs.os_bfree;
1188 osfs->os_bavail = obd_osfs.os_bavail;
1190 /* If we don't have as many objects free on the OST as inodes
1191 * on the MDS, we reduce the total number of inodes to
1192 * compensate, so that the "inodes in use" number is correct.
1194 if (obd_osfs.os_ffree < osfs->os_ffree) {
1195 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1197 osfs->os_ffree = obd_osfs.os_ffree;
1203 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1205 struct obd_statfs osfs;
1208 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1210 /* For now we will always get up-to-date statfs values, but in the
1211 * future we may allow some amount of caching on the client (e.g.
1212 * from QOS or lprocfs updates). */
1213 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - CFS_HZ);
1217 statfs_unpack(sfs, &osfs);
1219 if (sizeof(sfs->f_blocks) == 4) {
1220 while (osfs.os_blocks > ~0UL) {
1223 osfs.os_blocks >>= 1;
1224 osfs.os_bfree >>= 1;
1225 osfs.os_bavail >>= 1;
1229 sfs->f_blocks = osfs.os_blocks;
1230 sfs->f_bfree = osfs.os_bfree;
1231 sfs->f_bavail = osfs.os_bavail;
1236 static int llu_iop_statvfs(struct pnode *pno,
1238 struct intnl_statvfs *buf)
1244 liblustre_wait_event(0);
1247 LASSERT(pno->p_base->pb_ino);
1248 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1252 /* from native driver */
1253 buf->f_bsize = fs.f_bsize; /* file system block size */
1254 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1255 buf->f_blocks = fs.f_blocks;
1256 buf->f_bfree = fs.f_bfree;
1257 buf->f_bavail = fs.f_bavail;
1258 buf->f_files = fs.f_files; /* Total number serial numbers */
1259 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1260 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1261 buf->f_fsid = fs.f_fsid.__val[1];
1262 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1263 buf->f_namemax = fs.f_namelen;
1266 liblustre_wait_event(0);
1269 #endif /* _HAVE_STATVFS */
1271 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1273 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1274 struct qstr *qstr = &pno->p_base->pb_name;
1275 const char *name = qstr->name;
1276 int len = qstr->len;
1277 struct ptlrpc_request *request = NULL;
1278 struct intnl_stat *st = llu_i2stat(dir);
1279 struct md_op_data op_data = {{ 0 }};
1283 liblustre_wait_event(0);
1284 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1285 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1287 if (st->st_nlink >= EXT2_LINK_MAX)
1290 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1293 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1294 mode | S_IFDIR, current->fsuid, current->fsgid,
1295 cfs_curproc_cap_pack(), 0, &request);
1296 ptlrpc_req_finished(request);
1297 liblustre_wait_event(0);
1301 static int llu_iop_rmdir_raw(struct pnode *pno)
1303 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1304 struct qstr *qstr = &pno->p_base->pb_name;
1305 const char *name = qstr->name;
1306 int len = qstr->len;
1307 struct ptlrpc_request *request = NULL;
1308 struct md_op_data op_data = {{ 0 }};
1312 liblustre_wait_event(0);
1313 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1314 (long long)llu_i2stat(dir)->st_ino,
1315 llu_i2info(dir)->lli_st_generation, dir);
1317 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1319 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1320 ptlrpc_req_finished(request);
1322 liblustre_wait_event(0);
1327 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1329 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1331 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1333 /* refer to ll_file_flock() for details */
1334 static int llu_file_flock(struct inode *ino,
1336 struct file_lock *file_lock)
1338 struct llu_inode_info *lli = llu_i2info(ino);
1339 struct intnl_stat *st = llu_i2stat(ino);
1340 struct ldlm_res_id res_id =
1341 { .name = {fid_seq(&lli->lli_fid),
1342 fid_oid(&lli->lli_fid),
1343 fid_ver(&lli->lli_fid),
1345 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1346 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1348 struct lustre_handle lockh = {0};
1349 ldlm_policy_data_t flock;
1353 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1354 (unsigned long long)st->st_ino, file_lock);
1356 flock.l_flock.pid = file_lock->fl_pid;
1357 flock.l_flock.start = file_lock->fl_start;
1358 flock.l_flock.end = file_lock->fl_end;
1360 switch (file_lock->fl_type) {
1362 einfo.ei_mode = LCK_PR;
1365 einfo.ei_mode = LCK_NL;
1368 einfo.ei_mode = LCK_PW;
1371 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1378 #if F_SETLKW64 != F_SETLKW
1386 #if F_SETLK64 != F_SETLK
1390 flags = LDLM_FL_BLOCK_NOWAIT;
1394 #if F_GETLK64 != F_GETLK
1398 flags = LDLM_FL_TEST_LOCK;
1399 file_lock->fl_type = einfo.ei_mode;
1402 CERROR("unknown fcntl cmd: %d\n", cmd);
1406 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1407 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1408 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1412 struct lmv_obd *lmv;
1413 struct obd_device *lmv_obd;
1414 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1415 lmv = &lmv_obd->u.lmv;
1417 if (lmv->desc.ld_tgt_count < 1)
1418 RETURN(rc = -ENODEV);
1420 if (lmv->tgts[0].ltd_exp != NULL)
1421 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1422 &flock, &flags, NULL, 0, &lockh, 0);
1429 static int assign_type(struct file_lock *fl, int type)
1442 static int flock_to_posix_lock(struct inode *ino,
1443 struct file_lock *fl,
1446 switch (l->l_whence) {
1447 /* XXX: only SEEK_SET is supported in lustre */
1455 fl->fl_end = l->l_len - 1;
1459 fl->fl_end = OFFSET_MAX;
1461 fl->fl_pid = getpid();
1462 fl->fl_flags = FL_POSIX;
1463 fl->fl_notify = NULL;
1464 fl->fl_insert = NULL;
1465 fl->fl_remove = NULL;
1466 /* XXX: these fields can't be filled with suitable values,
1467 but I think lustre doesn't use them.
1469 fl->fl_owner = NULL;
1472 return assign_type(fl, l->l_type);
1475 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1477 struct file_lock fl;
1481 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1484 error = flock_to_posix_lock(ino, &fl, flock);
1488 error = llu_file_flock(ino, F_GETLK, &fl);
1492 flock->l_type = F_UNLCK;
1493 if (fl.fl_type != F_UNLCK) {
1494 flock->l_pid = fl.fl_pid;
1495 flock->l_start = fl.fl_start;
1496 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1497 fl.fl_end - fl.fl_start + 1;
1498 flock->l_whence = SEEK_SET;
1499 flock->l_type = fl.fl_type;
1506 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1508 struct file_lock fl;
1509 int flags = llu_i2info(ino)->lli_open_flags + 1;
1512 error = flock_to_posix_lock(ino, &fl, flock);
1515 if (cmd == F_SETLKW)
1516 fl.fl_flags |= FL_SLEEP;
1519 switch (flock->l_type) {
1521 if (!(flags & FMODE_READ))
1525 if (!(flags & FMODE_WRITE))
1535 error = llu_file_flock(ino, cmd, &fl);
1543 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1545 struct llu_inode_info *lli = llu_i2info(ino);
1547 struct flock *flock;
1550 liblustre_wait_event(0);
1553 *rtn = lli->lli_open_flags;
1556 flags = va_arg(ap, long);
1557 flags &= FCNTL_FLMASK;
1558 if (flags & FCNTL_FLMASK_INVALID) {
1559 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1560 "the O_NONBLOCK or O_ASYNC flags. "
1561 "Please fix your application.\n");
1566 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1567 (lli->lli_open_flags & ~FCNTL_FLMASK);
1572 #if F_GETLK64 != F_GETLK
1576 flock = va_arg(ap, struct flock *);
1577 err = llu_fcntl_getlk(ino, flock);
1582 #if F_SETLKW64 != F_SETLKW
1588 #if F_SETLK64 != F_SETLK
1592 flock = va_arg(ap, struct flock *);
1593 err = llu_fcntl_setlk(ino, cmd, flock);
1597 CERROR("unsupported fcntl cmd %x\n", cmd);
1603 liblustre_wait_event(0);
1607 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1609 struct llu_inode_info *lli = llu_i2info(inode);
1610 struct ll_file_data *fd = lli->lli_file_data;
1612 struct ccc_grouplock grouplock;
1615 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1618 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1621 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1623 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1624 arg, (lli->lli_open_flags & O_NONBLOCK),
1630 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1631 fd->fd_grouplock = grouplock;
1636 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1638 struct llu_inode_info *lli = llu_i2info(inode);
1639 struct ll_file_data *fd = lli->lli_file_data;
1640 struct ccc_grouplock grouplock;
1643 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1646 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1648 if (fd->fd_grouplock.cg_gid != arg)
1651 grouplock = fd->fd_grouplock;
1652 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1653 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1655 cl_put_grouplock(&grouplock);
1660 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1662 struct llu_sb_info *sbi = llu_i2sbi(ino);
1663 struct ptlrpc_request *request = NULL;
1664 struct md_op_data op_data = {{ 0 }};
1665 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1668 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1671 LASSERT(sizeof(lum) == sizeof(*lump));
1672 LASSERT(sizeof(lum.lmm_objects[0]) ==
1673 sizeof(lump->lmm_objects[0]));
1674 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1677 switch (lum.lmm_magic) {
1678 case LOV_USER_MAGIC_V1: {
1679 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1680 lustre_swab_lov_user_md_v1(&lum);
1683 case LOV_USER_MAGIC_V3: {
1684 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1685 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1689 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1690 " %#08x != %#08x nor %#08x\n",
1691 lum.lmm_magic, LOV_USER_MAGIC_V1,
1697 /* swabbing is done in lov_setstripe() on server side */
1698 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1699 sizeof(lum), NULL, 0, &request, NULL);
1701 ptlrpc_req_finished(request);
1702 if (rc != -EPERM && rc != -EACCES)
1703 CERROR("md_setattr fails: rc = %d\n", rc);
1706 ptlrpc_req_finished(request);
1711 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1712 struct lov_user_md *lum, int lum_size)
1714 struct llu_sb_info *sbi = llu_i2sbi(ino);
1715 struct llu_inode_info *lli = llu_i2info(ino);
1716 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1717 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1718 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1719 struct ptlrpc_request *req = NULL;
1720 struct lustre_md md;
1721 struct md_op_data data = {{ 0 }};
1722 struct lustre_handle lockh;
1727 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1728 PFID(&lli->lli_fid));
1732 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1734 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1735 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1739 req = oit.d.lustre.it_data;
1740 rc = it_open_error(DISP_IT_EXECD, &oit);
1746 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1752 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1753 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1757 llu_update_inode(ino, &md);
1758 llu_local_open(lli, &oit);
1759 /* release intent */
1760 if (lustre_handle_is_used(&lockh))
1761 ldlm_lock_decref(&lockh, LCK_CR);
1762 ptlrpc_req_finished(req);
1764 rc = llu_file_release(ino);
1769 ptlrpc_req_finished(req);
1773 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1775 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1777 int flags = FMODE_WRITE;
1780 LASSERT(sizeof(lum) == sizeof(*lump));
1781 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1782 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1785 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1789 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1791 struct intnl_stat *st = llu_i2stat(ino);
1792 if (S_ISREG(st->st_mode))
1793 return llu_lov_file_setstripe(ino, arg);
1794 if (S_ISDIR(st->st_mode))
1795 return llu_lov_dir_setstripe(ino, arg);
1800 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1802 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1807 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1811 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1817 liblustre_wait_event(0);
1820 case LL_IOC_GROUP_LOCK:
1821 arg = va_arg(ap, unsigned long);
1822 rc = llu_get_grouplock(ino, arg);
1824 case LL_IOC_GROUP_UNLOCK:
1825 arg = va_arg(ap, unsigned long);
1826 rc = llu_put_grouplock(ino, arg);
1828 case LL_IOC_LOV_SETSTRIPE:
1829 arg = va_arg(ap, unsigned long);
1830 rc = llu_lov_setstripe(ino, arg);
1832 case LL_IOC_LOV_GETSTRIPE:
1833 arg = va_arg(ap, unsigned long);
1834 rc = llu_lov_getstripe(ino, arg);
1837 CERROR("did not support ioctl cmd %lx\n", request);
1842 liblustre_wait_event(0);
1847 * we already do syncronous read/write
1849 static int llu_iop_sync(struct inode *inode)
1851 liblustre_wait_event(0);
1855 static int llu_iop_datasync(struct inode *inode)
1857 liblustre_wait_event(0);
1861 struct filesys_ops llu_filesys_ops =
1863 fsop_gone: llu_fsop_gone,
1866 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1868 struct inode *inode;
1870 struct file_identifier fileid = {&fid, sizeof(fid)};
1872 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1873 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1874 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1876 return ERR_PTR(-EPERM);
1879 /* try to find existing inode */
1880 fid = md->body->fid1;
1882 inode = _sysio_i_find(fs, &fileid);
1884 if (inode->i_zombie/* ||
1885 lli->lli_st_generation != md->body->generation*/) {
1889 llu_update_inode(inode, md);
1894 inode = llu_new_inode(fs, &fid);
1896 llu_update_inode(inode, md);
1902 llu_fsswop_mount(const char *source,
1904 const void *data __IS_UNUSED,
1905 struct pnode *tocover,
1906 struct mount **mntp)
1910 struct pnode_base *rootpb;
1911 struct obd_device *obd;
1912 struct llu_sb_info *sbi;
1913 struct obd_statfs osfs;
1914 static struct qstr noname = { NULL, 0, 0 };
1915 struct ptlrpc_request *request = NULL;
1916 struct lustre_md md;
1918 struct config_llog_instance cfg = {0, };
1919 char ll_instance[sizeof(sbi) * 2 + 1];
1920 struct lustre_profile *lprof;
1921 char *zconf_mgsnid, *zconf_profile;
1922 char *osc = NULL, *mdc = NULL;
1923 int async = 1, err = -EINVAL;
1924 struct obd_connect_data ocd = {0,};
1928 if (ll_parse_mount_target(source,
1931 CERROR("mal-formed target %s\n", source);
1934 if (!zconf_mgsnid || !zconf_profile) {
1935 printf("Liblustre: invalid target %s\n", source);
1938 /* allocate & initialize sbi */
1939 OBD_ALLOC(sbi, sizeof(*sbi));
1943 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1944 ll_generate_random_uuid(uuid);
1945 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1947 /* generate a string unique to this super, let's try
1948 the address of the super itself.*/
1949 sprintf(ll_instance, "%p", sbi);
1951 /* retrive & parse config log */
1952 cfg.cfg_instance = ll_instance;
1953 cfg.cfg_uuid = sbi->ll_sb_uuid;
1954 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1956 CERROR("Unable to process log: %s\n", zconf_profile);
1957 GOTO(out_free, err);
1960 lprof = class_get_profile(zconf_profile);
1961 if (lprof == NULL) {
1962 CERROR("No profile found: %s\n", zconf_profile);
1963 GOTO(out_free, err = -EINVAL);
1965 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1966 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
1968 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
1969 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
1973 GOTO(out_free, err = -EINVAL);
1977 GOTO(out_free, err = -EINVAL);
1980 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1986 obd = class_name2obd(mdc);
1988 CERROR("MDC %s: not setup or attached\n", mdc);
1989 GOTO(out_free, err = -EINVAL);
1991 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1992 sizeof(async), &async, NULL);
1994 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1995 OBD_CONNECT_FID | OBD_CONNECT_AT |
1996 OBD_CONNECT_VBR | OBD_CONNECT_SOM;
1997 #ifdef LIBLUSTRE_POSIX_ACL
1998 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2000 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2001 ocd.ocd_version = LUSTRE_VERSION_CODE;
2004 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2006 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2007 GOTO(out_free, err);
2010 err = obd_statfs(obd, &osfs, 100000000, 0);
2015 * FIXME fill fs stat data into sbi here!!! FIXME
2019 obd = class_name2obd(osc);
2021 CERROR("OSC %s: not setup or attached\n", osc);
2022 GOTO(out_md, err = -EINVAL);
2024 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2025 sizeof(async), &async, NULL);
2027 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2028 obd->obd_upcall.onu_upcall = cl_ocd_update;
2030 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2031 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2032 OBD_CONNECT_FID | OBD_CONNECT_AT |
2035 ocd.ocd_version = LUSTRE_VERSION_CODE;
2036 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2038 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2041 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2042 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
2043 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
2045 fid_zero(&sbi->ll_root_fid);
2046 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
2048 CERROR("cannot mds_connect: rc = %d\n", err);
2049 GOTO(out_lock_cn_cb, err);
2051 if (!fid_is_sane(&sbi->ll_root_fid)) {
2052 CERROR("Invalid root fid during mount\n");
2053 GOTO(out_lock_cn_cb, err = -EINVAL);
2055 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
2057 /* fetch attr of root inode */
2058 err = md_getattr(sbi->ll_md_exp, &sbi->ll_root_fid, NULL,
2059 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2061 CERROR("md_getattr failed for root: rc = %d\n", err);
2062 GOTO(out_lock_cn_cb, err);
2065 err = md_get_lustre_md(sbi->ll_md_exp, request,
2066 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2068 CERROR("failed to understand root inode md: rc = %d\n",err);
2069 GOTO(out_request, err);
2072 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2074 root = llu_iget(fs, &md);
2075 if (!root || IS_ERR(root)) {
2076 CERROR("fail to generate root inode\n");
2077 GOTO(out_request, err = -EBADF);
2081 * Generate base path-node for root.
2083 rootpb = _sysio_pb_new(&noname, NULL, root);
2089 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2091 _sysio_pb_gone(rootpb);
2097 ptlrpc_req_finished(request);
2099 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2100 liblustre_wait_idle();
2105 _sysio_i_gone(root);
2107 ptlrpc_req_finished(request);
2109 obd_disconnect(sbi->ll_dt_exp);
2111 obd_disconnect(sbi->ll_md_exp);
2114 OBD_FREE(osc, strlen(osc) + 1);
2116 OBD_FREE(mdc, strlen(mdc) + 1);
2117 OBD_FREE(sbi, sizeof(*sbi));
2118 liblustre_wait_idle();
2122 struct fssw_ops llu_fssw_ops = {
2126 static struct inode_ops llu_inode_ops = {
2127 inop_lookup: llu_iop_lookup,
2128 inop_getattr: llu_iop_getattr,
2129 inop_setattr: llu_iop_setattr,
2130 inop_filldirentries: llu_iop_filldirentries,
2131 inop_mkdir: llu_iop_mkdir_raw,
2132 inop_rmdir: llu_iop_rmdir_raw,
2133 inop_symlink: llu_iop_symlink_raw,
2134 inop_readlink: llu_iop_readlink,
2135 inop_open: llu_iop_open,
2136 inop_close: llu_iop_close,
2137 inop_link: llu_iop_link_raw,
2138 inop_unlink: llu_iop_unlink_raw,
2139 inop_rename: llu_iop_rename_raw,
2140 inop_pos: llu_iop_pos,
2141 inop_read: llu_iop_read,
2142 inop_write: llu_iop_write,
2143 inop_iodone: llu_iop_iodone,
2144 inop_fcntl: llu_iop_fcntl,
2145 inop_sync: llu_iop_sync,
2146 inop_datasync: llu_iop_datasync,
2147 inop_ioctl: llu_iop_ioctl,
2148 inop_mknod: llu_iop_mknod_raw,
2149 #ifdef _HAVE_STATVFS
2150 inop_statvfs: llu_iop_statvfs,
2152 inop_gone: llu_iop_gone,