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_FLATIME) {
177 if (body->atime > LTIME_S(st->st_atime))
178 LTIME_S(st->st_atime) = body->atime;
179 lli->lli_lvb.lvb_atime = body->atime;
181 if (body->valid & OBD_MD_FLMTIME) {
182 if (body->mtime > LTIME_S(st->st_mtime))
183 LTIME_S(st->st_mtime) = body->mtime;
184 lli->lli_lvb.lvb_mtime = body->mtime;
186 if (body->valid & OBD_MD_FLCTIME) {
187 if (body->ctime > LTIME_S(st->st_ctime))
188 LTIME_S(st->st_ctime) = body->ctime;
189 lli->lli_lvb.lvb_ctime = body->ctime;
191 if (S_ISREG(st->st_mode))
192 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
194 st->st_blksize = 4096;
195 if (body->valid & OBD_MD_FLUID)
196 st->st_uid = body->uid;
197 if (body->valid & OBD_MD_FLGID)
198 st->st_gid = body->gid;
199 if (body->valid & OBD_MD_FLNLINK)
200 st->st_nlink = body->nlink;
201 if (body->valid & OBD_MD_FLRDEV)
202 st->st_rdev = body->rdev;
203 if (body->valid & OBD_MD_FLFLAGS)
204 lli->lli_st_flags = body->flags;
205 if (body->valid & OBD_MD_FLSIZE) {
206 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
207 S_ISREG(st->st_mode) && lli->lli_smd) {
208 struct lustre_handle lockh;
211 /* As it is possible a blocking ast has been processed
212 * by this time, we need to check there is an UPDATE
213 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
215 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
218 st->st_size = body->size;
219 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
220 ldlm_lock_decref(&lockh, mode);
223 st->st_size = body->size;
226 if (body->valid & OBD_MD_FLBLOCKS)
227 st->st_blocks = body->blocks;
231 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
233 struct llu_inode_info *lli = llu_i2info(dst);
234 struct intnl_stat *st = llu_i2stat(dst);
236 valid &= src->o_valid;
238 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
239 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
242 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
243 (long)src->o_mtime, (long)src->o_ctime);
245 if (valid & OBD_MD_FLATIME)
246 LTIME_S(st->st_atime) = src->o_atime;
247 if (valid & OBD_MD_FLMTIME)
248 LTIME_S(st->st_mtime) = src->o_mtime;
249 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
250 LTIME_S(st->st_ctime) = src->o_ctime;
251 if (valid & OBD_MD_FLSIZE)
252 st->st_size = src->o_size;
253 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
254 st->st_blocks = src->o_blocks;
255 if (valid & OBD_MD_FLBLKSZ)
256 st->st_blksize = src->o_blksize;
257 if (valid & OBD_MD_FLTYPE)
258 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
259 if (valid & OBD_MD_FLMODE)
260 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
261 if (valid & OBD_MD_FLUID)
262 st->st_uid = src->o_uid;
263 if (valid & OBD_MD_FLGID)
264 st->st_gid = src->o_gid;
265 if (valid & OBD_MD_FLFLAGS)
266 lli->lli_st_flags = src->o_flags;
269 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
270 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
272 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
274 struct llu_inode_info *lli = llu_i2info(src);
275 struct intnl_stat *st = llu_i2stat(src);
276 obd_flag newvalid = 0;
278 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
279 CDEBUG(D_INODE, "valid %x, new time "CFS_TIME_T"/"CFS_TIME_T"\n",
280 valid, LTIME_S(st->st_mtime),
281 LTIME_S(st->st_ctime));
283 if (valid & OBD_MD_FLATIME) {
284 dst->o_atime = LTIME_S(st->st_atime);
285 newvalid |= OBD_MD_FLATIME;
287 if (valid & OBD_MD_FLMTIME) {
288 dst->o_mtime = LTIME_S(st->st_mtime);
289 newvalid |= OBD_MD_FLMTIME;
291 if (valid & OBD_MD_FLCTIME) {
292 dst->o_ctime = LTIME_S(st->st_ctime);
293 newvalid |= OBD_MD_FLCTIME;
295 if (valid & OBD_MD_FLSIZE) {
296 dst->o_size = st->st_size;
297 newvalid |= OBD_MD_FLSIZE;
299 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
300 dst->o_blocks = st->st_blocks;
301 newvalid |= OBD_MD_FLBLOCKS;
303 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
304 dst->o_blksize = st->st_blksize;
305 newvalid |= OBD_MD_FLBLKSZ;
307 if (valid & OBD_MD_FLTYPE) {
308 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
309 newvalid |= OBD_MD_FLTYPE;
311 if (valid & OBD_MD_FLMODE) {
312 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
313 newvalid |= OBD_MD_FLMODE;
315 if (valid & OBD_MD_FLUID) {
316 dst->o_uid = st->st_uid;
317 newvalid |= OBD_MD_FLUID;
319 if (valid & OBD_MD_FLGID) {
320 dst->o_gid = st->st_gid;
321 newvalid |= OBD_MD_FLGID;
323 if (valid & OBD_MD_FLFLAGS) {
324 dst->o_flags = lli->lli_st_flags;
325 newvalid |= OBD_MD_FLFLAGS;
327 if (valid & OBD_MD_FLGENER) {
328 dst->o_generation = lli->lli_st_generation;
329 newvalid |= OBD_MD_FLGENER;
331 if (valid & OBD_MD_FLFID) {
332 dst->o_fid = st->st_ino;
333 newvalid |= OBD_MD_FLFID;
336 dst->o_valid |= newvalid;
340 * Performs the getattr on the inode and updates its fields.
341 * If @sync != 0, perform the getattr under the server-side lock.
343 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
344 __u64 ioepoch, int sync)
346 struct llu_inode_info *lli = llu_i2info(inode);
347 struct ptlrpc_request_set *set;
348 struct lov_stripe_md *lsm = lli->lli_smd;
349 struct obd_info oinfo = { { { 0 } } };
357 oinfo.oi_oa->o_id = lsm->lsm_object_id;
358 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
359 oinfo.oi_oa->o_mode = S_IFREG;
360 oinfo.oi_oa->o_ioepoch = ioepoch;
361 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
362 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
363 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
364 OBD_MD_FLCTIME | OBD_MD_FLGROUP |
365 OBD_MD_FLATIME | OBD_MD_FLEPOCH;
367 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
368 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
371 set = ptlrpc_prep_set();
373 CERROR ("ENOMEM allocing request set\n");
376 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
378 rc = ptlrpc_set_wait(set);
379 ptlrpc_set_destroy(set);
384 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
385 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
388 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
389 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
390 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
391 (long long unsigned)llu_i2stat(inode)->st_size,
392 (long long unsigned)llu_i2stat(inode)->st_blocks,
393 (long long unsigned)llu_i2stat(inode)->st_blksize);
397 static struct inode* llu_new_inode(struct filesys *fs,
401 struct llu_inode_info *lli;
402 struct intnl_stat st = {
405 #ifndef AUTOMOUNT_FILE_NAME
406 .st_mode = fid->f_type & S_IFMT,
408 .st_mode = fid->f_type /* all of the bits! */
411 /* FIXME: fix this later */
418 OBD_ALLOC(lli, sizeof(*lli));
422 /* initialize lli here */
423 lli->lli_sbi = llu_fs2sbi(fs);
425 lli->lli_symlink_name = NULL;
427 lli->lli_maxbytes = (__u64)(~0UL);
428 lli->lli_file_data = NULL;
430 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
431 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
434 /* file identifier is needed by functions like _sysio_i_find() */
435 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
436 &st, 0, &llu_inode_ops, lli);
439 OBD_FREE(lli, sizeof(*lli));
444 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
446 struct lustre_handle lockh;
447 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
454 fid = &llu_i2info(inode)->lli_fid;
455 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
457 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
458 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
459 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
465 static int llu_inode_revalidate(struct inode *inode)
467 struct llu_inode_info *lli = llu_i2info(inode);
468 struct intnl_stat *st = llu_i2stat(inode);
472 CERROR("REPORT THIS LINE TO PETER\n");
476 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
478 struct ptlrpc_request *req = NULL;
479 struct llu_sb_info *sbi = llu_i2sbi(inode);
480 struct md_op_data op_data = { { 0 } };
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(st->st_mode)) {
487 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
488 valid |= OBD_MD_FLEASIZE;
491 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, ealen,
493 op_data.op_valid = valid;
495 rc = md_getattr(sbi->ll_md_exp, &op_data, &req);
497 CERROR("failure %d inode %llu\n", rc,
498 (long long)st->st_ino);
501 rc = md_get_lustre_md(sbi->ll_md_exp, req,
502 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
504 /* XXX Too paranoid? */
505 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
506 !((md.body->valid & OBD_MD_FLNLINK) &&
507 (md.body->nlink == 0))) {
508 CERROR("Asked for %s eadata but got %s (%d)\n",
509 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
510 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
511 md.body->eadatasize);
514 ptlrpc_req_finished(req);
519 llu_update_inode(inode, &md);
520 if (md.lsm != NULL && lli->lli_smd != md.lsm)
521 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
522 ptlrpc_req_finished(req);
526 /* object not yet allocated, don't validate size */
527 st->st_atime = lli->lli_lvb.lvb_atime;
528 st->st_mtime = lli->lli_lvb.lvb_mtime;
529 st->st_ctime = lli->lli_lvb.lvb_ctime;
533 /* ll_glimpse_size will prefer locally cached writes if they extend
535 RETURN(cl_glimpse_size(inode));
538 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
540 *b = *llu_i2stat(ino);
543 static int llu_iop_getattr(struct pnode *pno,
545 struct intnl_stat *b)
550 liblustre_wait_event(0);
554 LASSERT(pno->p_base->pb_ino);
555 ino = pno->p_base->pb_ino;
557 LASSERT(!pno || pno->p_base->pb_ino == ino);
560 /* libsysio might call us directly without intent lock,
561 * we must re-fetch the attrs here
563 rc = llu_inode_revalidate(ino);
565 copy_stat_buf(ino, b);
566 LASSERT(!llu_i2info(ino)->lli_it);
569 liblustre_wait_event(0);
573 static int null_if_equal(struct ldlm_lock *lock, void *data)
575 if (data == lock->l_ast_data) {
576 lock->l_ast_data = NULL;
578 if (lock->l_req_mode != lock->l_granted_mode)
579 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
582 return LDLM_ITER_CONTINUE;
585 void llu_clear_inode(struct inode *inode)
587 struct llu_inode_info *lli = llu_i2info(inode);
588 struct llu_sb_info *sbi = llu_i2sbi(inode);
591 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
592 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
595 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
596 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
597 null_if_equal, inode);
600 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
601 null_if_equal, inode);
603 cl_inode_fini(inode);
606 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
610 if (lli->lli_symlink_name) {
611 OBD_FREE(lli->lli_symlink_name,
612 strlen(lli->lli_symlink_name) + 1);
613 lli->lli_symlink_name = NULL;
619 void llu_iop_gone(struct inode *inode)
621 struct llu_inode_info *lli = llu_i2info(inode);
624 liblustre_wait_event(0);
625 llu_clear_inode(inode);
627 OBD_FREE(lli, sizeof(*lli));
631 static int inode_setattr(struct inode * inode, struct iattr * attr)
633 unsigned int ia_valid = attr->ia_valid;
634 struct intnl_stat *st = llu_i2stat(inode);
638 * inode_setattr() is only ever invoked with ATTR_SIZE (by
639 * llu_setattr_raw()) when file has no bodies. Check this.
641 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
643 if (ia_valid & ATTR_SIZE)
644 st->st_size = attr->ia_size;
645 if (ia_valid & ATTR_UID)
646 st->st_uid = attr->ia_uid;
647 if (ia_valid & ATTR_GID)
648 st->st_gid = attr->ia_gid;
649 if (ia_valid & ATTR_ATIME)
650 st->st_atime = attr->ia_atime;
651 if (ia_valid & ATTR_MTIME)
652 st->st_mtime = attr->ia_mtime;
653 if (ia_valid & ATTR_CTIME)
654 st->st_ctime = attr->ia_ctime;
655 if (ia_valid & ATTR_MODE) {
656 st->st_mode = attr->ia_mode;
657 if (!cfs_curproc_is_in_groups(st->st_gid) &&
658 !cfs_capable(CFS_CAP_FSETID))
659 st->st_mode &= ~S_ISGID;
661 /* mark_inode_dirty(inode); */
665 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
666 struct md_open_data **mod)
669 struct llu_sb_info *sbi = llu_i2sbi(inode);
670 struct ptlrpc_request *request = NULL;
674 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
675 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
679 ptlrpc_req_finished(request);
680 if (rc != -EPERM && rc != -EACCES)
681 CERROR("md_setattr fails: rc = %d\n", rc);
685 rc = md_get_lustre_md(sbi->ll_md_exp, request,
686 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
688 ptlrpc_req_finished(request);
692 /* We call inode_setattr to adjust timestamps.
693 * If there is at least some data in file, we cleared ATTR_SIZE
694 * above to avoid invoking vmtruncate, otherwise it is important
695 * to call vmtruncate in inode_setattr to update inode->i_size
697 inode_setattr(inode, &op_data->op_attr);
698 llu_update_inode(inode, &md);
699 ptlrpc_req_finished(request);
704 /* Close IO epoch and send Size-on-MDS attribute update. */
705 static int llu_setattr_done_writing(struct inode *inode,
706 struct md_op_data *op_data,
707 struct md_open_data *mod)
709 struct llu_inode_info *lli = llu_i2info(inode);
710 struct intnl_stat *st = llu_i2stat(inode);
714 LASSERT(op_data != NULL);
715 if (!S_ISREG(st->st_mode))
718 /* XXX: pass och here for the recovery purpose. */
719 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
720 op_data->op_ioepoch, PFID(&lli->lli_fid));
722 op_data->op_flags = MF_EPOCH_CLOSE;
723 llu_done_writing_attr(inode, op_data);
724 llu_pack_inode2opdata(inode, op_data, NULL);
726 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
728 /* MDS has instructed us to obtain Size-on-MDS attribute
729 * from OSTs and send setattr to back to MDS. */
730 rc = llu_som_update(inode, op_data);
732 CERROR("inode %llu mdc truncate failed: rc = %d\n",
733 (unsigned long long)st->st_ino, rc);
738 /* If this inode has objects allocated to it (lsm != NULL), then the OST
739 * object(s) determine the file size and mtime. Otherwise, the MDS will
740 * keep these values until such a time that objects are allocated for it.
741 * We do the MDS operations first, as it is checking permissions for us.
742 * We don't to the MDS RPC if there is nothing that we want to store there,
743 * otherwise there is no harm in updating mtime/atime on the MDS if we are
744 * going to do an RPC anyways.
746 * If we are doing a truncate, we will send the mtime and ctime updates
747 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
748 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
751 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
753 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
754 struct intnl_stat *st = llu_i2stat(inode);
755 int ia_valid = attr->ia_valid;
756 struct md_op_data op_data = { { 0 } };
757 struct md_open_data *mod = NULL;
761 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
763 if (ia_valid & ATTR_SIZE) {
764 if (attr->ia_size > ll_file_maxbytes(inode)) {
765 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
766 (long long)attr->ia_size,
767 ll_file_maxbytes(inode));
771 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
774 /* We mark all of the fields "set" so MDS/OST does not re-set them */
775 if (attr->ia_valid & ATTR_CTIME) {
776 attr->ia_ctime = CFS_CURRENT_TIME;
777 attr->ia_valid |= ATTR_CTIME_SET;
779 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
780 attr->ia_atime = CFS_CURRENT_TIME;
781 attr->ia_valid |= ATTR_ATIME_SET;
783 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
784 attr->ia_mtime = CFS_CURRENT_TIME;
785 attr->ia_valid |= ATTR_MTIME_SET;
788 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
789 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
790 ", now = "CFS_TIME_T"\n",
791 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
792 LTIME_S(CFS_CURRENT_TIME));
794 /* NB: ATTR_SIZE will only be set after this point if the size
795 * resides on the MDS, ie, this file has no objects. */
797 attr->ia_valid &= ~ATTR_SIZE;
799 /* If only OST attributes being set on objects, don't do MDS RPC.
800 * In that case, we need to check permissions and update the local
801 * inode ourselves so we can call obdo_from_inode() always. */
802 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
803 memcpy(&op_data.op_attr, attr, sizeof(*attr));
805 /* Open epoch for truncate. */
806 if (exp_connect_som(llu_i2mdexp(inode)) &&
807 (ia_valid & ATTR_SIZE))
808 op_data.op_flags = MF_EPOCH_OPEN;
809 rc = llu_md_setattr(inode, &op_data, &mod);
813 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
814 if (!lsm || !S_ISREG(st->st_mode)) {
815 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
819 /* The OST doesn't check permissions, but the alternative is
820 * a gratuitous RPC to the MDS. We already rely on the client
821 * to do read/write/truncate permission checks, so is mtime OK?
823 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
824 /* from sys_utime() */
825 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
826 if (current->fsuid != st->st_uid &&
827 (rc = ll_permission(inode, MAY_WRITE)) != 0)
830 /* from inode_change_ok() */
831 if (current->fsuid != st->st_uid &&
832 !cfs_capable(CFS_CAP_FOWNER))
838 /* Won't invoke llu_vmtruncate(), as we already cleared
840 inode_setattr(inode, attr);
843 if (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)) {
844 /* mtime is set to past sending setattr op to osts under PW
845 * 0:EOF extent lock (like truncate under PW new_size:EOF), if
846 * mtime is not set to past setattr op is not sent to osts */
847 if ((ia_valid & ATTR_SIZE) ||
848 LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime))
849 rc = cl_setattr_ost(inode, attr, NULL);
853 if (op_data.op_ioepoch)
854 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
855 return rc ? rc : rc1;
858 /* here we simply act as a thin layer to glue it with
859 * llu_setattr_raw(), which is copy from kernel
861 static int llu_iop_setattr(struct pnode *pno,
864 struct intnl_stat *stbuf)
870 liblustre_wait_event(0);
872 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
873 SETATTR_UID | SETATTR_GID |
874 SETATTR_LEN | SETATTR_MODE)));
875 memset(&iattr, 0, sizeof(iattr));
877 if (mask & SETATTR_MODE) {
878 iattr.ia_mode = stbuf->st_mode;
879 iattr.ia_valid |= ATTR_MODE;
881 if (mask & SETATTR_MTIME) {
882 iattr.ia_mtime = stbuf->st_mtime;
883 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
885 if (mask & SETATTR_ATIME) {
886 iattr.ia_atime = stbuf->st_atime;
887 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
889 if (mask & SETATTR_UID) {
890 iattr.ia_uid = stbuf->st_uid;
891 iattr.ia_valid |= ATTR_UID;
893 if (mask & SETATTR_GID) {
894 iattr.ia_gid = stbuf->st_gid;
895 iattr.ia_valid |= ATTR_GID;
897 if (mask & SETATTR_LEN) {
898 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
899 iattr.ia_valid |= ATTR_SIZE;
902 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
903 iattr.ia_ctime = CFS_CURRENT_TIME;
905 rc = llu_setattr_raw(ino, &iattr);
906 liblustre_wait_idle();
910 #define EXT2_LINK_MAX 32000
912 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
914 struct inode *dir = pno->p_base->pb_parent->pb_ino;
915 struct qstr *qstr = &pno->p_base->pb_name;
916 const char *name = qstr->name;
918 struct ptlrpc_request *request = NULL;
919 struct llu_sb_info *sbi = llu_i2sbi(dir);
920 struct md_op_data op_data = {{ 0 }};
924 liblustre_wait_event(0);
925 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
928 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
931 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
932 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
933 cfs_curproc_cap_pack(), 0, &request);
934 ptlrpc_req_finished(request);
935 liblustre_wait_event(0);
939 static int llu_readlink_internal(struct inode *inode,
940 struct ptlrpc_request **request,
943 struct llu_inode_info *lli = llu_i2info(inode);
944 struct llu_sb_info *sbi = llu_i2sbi(inode);
945 struct mdt_body *body;
946 struct intnl_stat *st = llu_i2stat(inode);
947 struct md_op_data op_data = {{ 0 }};
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 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, symlen,
962 op_data.op_valid = OBD_MD_LINKNAME;
964 rc = md_getattr(sbi->ll_md_exp, &op_data, request);
966 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
970 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
971 LASSERT(body != NULL);
973 if ((body->valid & OBD_MD_LINKNAME) == 0) {
974 CERROR ("OBD_MD_LINKNAME not set on reply\n");
975 GOTO (failed, rc = -EPROTO);
978 LASSERT(symlen != 0);
979 if (body->eadatasize != symlen) {
980 CERROR("inode %llu: symlink length %d not expected %d\n",
981 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
982 GOTO(failed, rc = -EPROTO);
985 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
986 if (*symname == NULL ||
987 strnlen(*symname, symlen) != symlen - 1) {
988 /* not full/NULL terminated */
989 CERROR("inode %llu: symlink not NULL terminated string"
990 "of length %d\n", (long long)st->st_ino, symlen - 1);
991 GOTO(failed, rc = -EPROTO);
994 OBD_ALLOC(lli->lli_symlink_name, symlen);
995 /* do not return an error if we cannot cache the symlink locally */
996 if (lli->lli_symlink_name)
997 memcpy(lli->lli_symlink_name, *symname, symlen);
1002 ptlrpc_req_finished (*request);
1006 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1008 struct inode *inode = pno->p_base->pb_ino;
1009 struct ptlrpc_request *request;
1014 liblustre_wait_event(0);
1015 rc = llu_readlink_internal(inode, &request, &symname);
1020 strncpy(data, symname, bufsize);
1021 rc = strlen(symname);
1023 ptlrpc_req_finished(request);
1025 liblustre_wait_event(0);
1029 static int llu_iop_mknod_raw(struct pnode *pno,
1033 struct ptlrpc_request *request = NULL;
1034 struct inode *dir = pno->p_parent->p_base->pb_ino;
1035 struct llu_sb_info *sbi = llu_i2sbi(dir);
1036 struct md_op_data op_data = {{ 0 }};
1040 liblustre_wait_event(0);
1041 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1042 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1043 (long long)llu_i2stat(dir)->st_ino);
1045 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1048 switch (mode & S_IFMT) {
1051 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1056 llu_prep_md_op_data(&op_data, dir, NULL,
1057 pno->p_base->pb_name.name,
1058 pno->p_base->pb_name.len, 0,
1061 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1062 current->fsuid, current->fsgid,
1063 cfs_curproc_cap_pack(), dev, &request);
1064 ptlrpc_req_finished(request);
1072 liblustre_wait_event(0);
1076 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1078 struct inode *src = old->p_base->pb_ino;
1079 struct inode *dir = new->p_parent->p_base->pb_ino;
1080 const char *name = new->p_base->pb_name.name;
1081 int namelen = new->p_base->pb_name.len;
1082 struct ptlrpc_request *request = NULL;
1083 struct md_op_data op_data = {{ 0 }};
1090 liblustre_wait_event(0);
1091 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1093 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1094 ptlrpc_req_finished(request);
1095 liblustre_wait_event(0);
1101 * libsysio will clear the inode immediately after return
1103 static int llu_iop_unlink_raw(struct pnode *pno)
1105 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1106 struct qstr *qstr = &pno->p_base->pb_name;
1107 const char *name = qstr->name;
1108 int len = qstr->len;
1109 struct inode *target = pno->p_base->pb_ino;
1110 struct ptlrpc_request *request = NULL;
1111 struct md_op_data op_data = { { 0 } };
1117 liblustre_wait_event(0);
1118 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1120 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1122 rc = llu_objects_destroy(request, dir);
1123 ptlrpc_req_finished(request);
1124 liblustre_wait_idle();
1129 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1131 struct inode *src = old->p_parent->p_base->pb_ino;
1132 struct inode *tgt = new->p_parent->p_base->pb_ino;
1133 const char *oldname = old->p_base->pb_name.name;
1134 int oldnamelen = old->p_base->pb_name.len;
1135 const char *newname = new->p_base->pb_name.name;
1136 int newnamelen = new->p_base->pb_name.len;
1137 struct ptlrpc_request *request = NULL;
1138 struct md_op_data op_data = { { 0 } };
1145 liblustre_wait_event(0);
1146 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1148 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1149 oldname, oldnamelen, newname, newnamelen,
1152 rc = llu_objects_destroy(request, src);
1155 ptlrpc_req_finished(request);
1156 liblustre_wait_idle();
1161 #ifdef _HAVE_STATVFS
1162 static int llu_statfs_internal(struct llu_sb_info *sbi,
1163 struct obd_statfs *osfs, __u64 max_age)
1165 struct obd_statfs obd_osfs;
1169 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1171 CERROR("md_statfs fails: rc = %d\n", rc);
1175 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1176 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1178 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1179 &obd_statfs, max_age, 0);
1181 CERROR("obd_statfs fails: rc = %d\n", rc);
1185 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1186 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1189 osfs->os_blocks = obd_osfs.os_blocks;
1190 osfs->os_bfree = obd_osfs.os_bfree;
1191 osfs->os_bavail = obd_osfs.os_bavail;
1193 /* If we don't have as many objects free on the OST as inodes
1194 * on the MDS, we reduce the total number of inodes to
1195 * compensate, so that the "inodes in use" number is correct.
1197 if (obd_osfs.os_ffree < osfs->os_ffree) {
1198 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1200 osfs->os_ffree = obd_osfs.os_ffree;
1206 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1208 struct obd_statfs osfs;
1211 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1213 /* For now we will always get up-to-date statfs values, but in the
1214 * future we may allow some amount of caching on the client (e.g.
1215 * from QOS or lprocfs updates). */
1216 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - CFS_HZ);
1220 statfs_unpack(sfs, &osfs);
1222 if (sizeof(sfs->f_blocks) == 4) {
1223 while (osfs.os_blocks > ~0UL) {
1226 osfs.os_blocks >>= 1;
1227 osfs.os_bfree >>= 1;
1228 osfs.os_bavail >>= 1;
1232 sfs->f_blocks = osfs.os_blocks;
1233 sfs->f_bfree = osfs.os_bfree;
1234 sfs->f_bavail = osfs.os_bavail;
1239 static int llu_iop_statvfs(struct pnode *pno,
1241 struct intnl_statvfs *buf)
1247 liblustre_wait_event(0);
1250 LASSERT(pno->p_base->pb_ino);
1251 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1255 /* from native driver */
1256 buf->f_bsize = fs.f_bsize; /* file system block size */
1257 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1258 buf->f_blocks = fs.f_blocks;
1259 buf->f_bfree = fs.f_bfree;
1260 buf->f_bavail = fs.f_bavail;
1261 buf->f_files = fs.f_files; /* Total number serial numbers */
1262 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1263 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1264 buf->f_fsid = fs.f_fsid.__val[1];
1265 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1266 buf->f_namemax = fs.f_namelen;
1269 liblustre_wait_event(0);
1272 #endif /* _HAVE_STATVFS */
1274 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1276 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1277 struct qstr *qstr = &pno->p_base->pb_name;
1278 const char *name = qstr->name;
1279 int len = qstr->len;
1280 struct ptlrpc_request *request = NULL;
1281 struct intnl_stat *st = llu_i2stat(dir);
1282 struct md_op_data op_data = {{ 0 }};
1286 liblustre_wait_event(0);
1287 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1288 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1290 if (st->st_nlink >= EXT2_LINK_MAX)
1293 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1296 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1297 mode | S_IFDIR, current->fsuid, current->fsgid,
1298 cfs_curproc_cap_pack(), 0, &request);
1299 ptlrpc_req_finished(request);
1300 liblustre_wait_event(0);
1304 static int llu_iop_rmdir_raw(struct pnode *pno)
1306 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1307 struct qstr *qstr = &pno->p_base->pb_name;
1308 const char *name = qstr->name;
1309 int len = qstr->len;
1310 struct ptlrpc_request *request = NULL;
1311 struct md_op_data op_data = {{ 0 }};
1315 liblustre_wait_event(0);
1316 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1317 (long long)llu_i2stat(dir)->st_ino,
1318 llu_i2info(dir)->lli_st_generation, dir);
1320 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1322 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1323 ptlrpc_req_finished(request);
1325 liblustre_wait_event(0);
1330 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1332 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1334 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1336 /* refer to ll_file_flock() for details */
1337 static int llu_file_flock(struct inode *ino,
1339 struct file_lock *file_lock)
1341 struct llu_inode_info *lli = llu_i2info(ino);
1342 struct intnl_stat *st = llu_i2stat(ino);
1343 struct ldlm_res_id res_id =
1344 { .name = {fid_seq(&lli->lli_fid),
1345 fid_oid(&lli->lli_fid),
1346 fid_ver(&lli->lli_fid),
1348 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1349 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1351 struct lustre_handle lockh = {0};
1352 ldlm_policy_data_t flock;
1356 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1357 (unsigned long long)st->st_ino, file_lock);
1359 flock.l_flock.pid = file_lock->fl_pid;
1360 flock.l_flock.start = file_lock->fl_start;
1361 flock.l_flock.end = file_lock->fl_end;
1363 switch (file_lock->fl_type) {
1365 einfo.ei_mode = LCK_PR;
1368 einfo.ei_mode = LCK_NL;
1371 einfo.ei_mode = LCK_PW;
1374 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1381 #if F_SETLKW64 != F_SETLKW
1389 #if F_SETLK64 != F_SETLK
1393 flags = LDLM_FL_BLOCK_NOWAIT;
1397 #if F_GETLK64 != F_GETLK
1401 flags = LDLM_FL_TEST_LOCK;
1402 file_lock->fl_type = einfo.ei_mode;
1405 CERROR("unknown fcntl cmd: %d\n", cmd);
1409 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1410 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1411 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1415 struct lmv_obd *lmv;
1416 struct obd_device *lmv_obd;
1417 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1418 lmv = &lmv_obd->u.lmv;
1420 if (lmv->desc.ld_tgt_count < 1)
1421 RETURN(rc = -ENODEV);
1423 if (lmv->tgts[0].ltd_exp != NULL)
1424 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1425 &flock, &flags, NULL, 0, &lockh, 0);
1432 static int assign_type(struct file_lock *fl, int type)
1445 static int flock_to_posix_lock(struct inode *ino,
1446 struct file_lock *fl,
1449 switch (l->l_whence) {
1450 /* XXX: only SEEK_SET is supported in lustre */
1458 fl->fl_end = l->l_len - 1;
1462 fl->fl_end = OFFSET_MAX;
1464 fl->fl_pid = getpid();
1465 fl->fl_flags = FL_POSIX;
1466 fl->fl_notify = NULL;
1467 fl->fl_insert = NULL;
1468 fl->fl_remove = NULL;
1469 /* XXX: these fields can't be filled with suitable values,
1470 but I think lustre doesn't use them.
1472 fl->fl_owner = NULL;
1475 return assign_type(fl, l->l_type);
1478 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1480 struct file_lock fl;
1484 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1487 error = flock_to_posix_lock(ino, &fl, flock);
1491 error = llu_file_flock(ino, F_GETLK, &fl);
1495 flock->l_type = F_UNLCK;
1496 if (fl.fl_type != F_UNLCK) {
1497 flock->l_pid = fl.fl_pid;
1498 flock->l_start = fl.fl_start;
1499 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1500 fl.fl_end - fl.fl_start + 1;
1501 flock->l_whence = SEEK_SET;
1502 flock->l_type = fl.fl_type;
1509 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1511 struct file_lock fl;
1512 int flags = llu_i2info(ino)->lli_open_flags + 1;
1515 error = flock_to_posix_lock(ino, &fl, flock);
1518 if (cmd == F_SETLKW)
1519 fl.fl_flags |= FL_SLEEP;
1522 switch (flock->l_type) {
1524 if (!(flags & FMODE_READ))
1528 if (!(flags & FMODE_WRITE))
1538 error = llu_file_flock(ino, cmd, &fl);
1546 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1548 struct llu_inode_info *lli = llu_i2info(ino);
1550 struct flock *flock;
1553 liblustre_wait_event(0);
1556 *rtn = lli->lli_open_flags;
1559 flags = va_arg(ap, long);
1560 flags &= FCNTL_FLMASK;
1561 if (flags & FCNTL_FLMASK_INVALID) {
1562 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1563 "the O_NONBLOCK or O_ASYNC flags. "
1564 "Please fix your application.\n");
1569 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1570 (lli->lli_open_flags & ~FCNTL_FLMASK);
1575 #if F_GETLK64 != F_GETLK
1579 flock = va_arg(ap, struct flock *);
1580 err = llu_fcntl_getlk(ino, flock);
1585 #if F_SETLKW64 != F_SETLKW
1591 #if F_SETLK64 != F_SETLK
1595 flock = va_arg(ap, struct flock *);
1596 err = llu_fcntl_setlk(ino, cmd, flock);
1600 CERROR("unsupported fcntl cmd %x\n", cmd);
1606 liblustre_wait_event(0);
1610 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1612 struct llu_inode_info *lli = llu_i2info(inode);
1613 struct ll_file_data *fd = lli->lli_file_data;
1615 struct ccc_grouplock grouplock;
1618 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1621 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1624 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1626 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1627 arg, (lli->lli_open_flags & O_NONBLOCK),
1633 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1634 fd->fd_grouplock = grouplock;
1639 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1641 struct llu_inode_info *lli = llu_i2info(inode);
1642 struct ll_file_data *fd = lli->lli_file_data;
1643 struct ccc_grouplock grouplock;
1646 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1649 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1651 if (fd->fd_grouplock.cg_gid != arg)
1654 grouplock = fd->fd_grouplock;
1655 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1656 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1658 cl_put_grouplock(&grouplock);
1663 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1665 struct llu_sb_info *sbi = llu_i2sbi(ino);
1666 struct ptlrpc_request *request = NULL;
1667 struct md_op_data op_data = {{ 0 }};
1668 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1671 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1674 LASSERT(sizeof(lum) == sizeof(*lump));
1675 LASSERT(sizeof(lum.lmm_objects[0]) ==
1676 sizeof(lump->lmm_objects[0]));
1677 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1680 switch (lum.lmm_magic) {
1681 case LOV_USER_MAGIC_V1: {
1682 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1683 lustre_swab_lov_user_md_v1(&lum);
1686 case LOV_USER_MAGIC_V3: {
1687 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1688 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1692 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1693 " %#08x != %#08x nor %#08x\n",
1694 lum.lmm_magic, LOV_USER_MAGIC_V1,
1700 /* swabbing is done in lov_setstripe() on server side */
1701 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1702 sizeof(lum), NULL, 0, &request, NULL);
1704 ptlrpc_req_finished(request);
1705 if (rc != -EPERM && rc != -EACCES)
1706 CERROR("md_setattr fails: rc = %d\n", rc);
1709 ptlrpc_req_finished(request);
1714 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1715 struct lov_user_md *lum, int lum_size)
1717 struct llu_sb_info *sbi = llu_i2sbi(ino);
1718 struct llu_inode_info *lli = llu_i2info(ino);
1719 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1720 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1721 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1722 struct ptlrpc_request *req = NULL;
1723 struct lustre_md md;
1724 struct md_op_data data = {{ 0 }};
1725 struct lustre_handle lockh;
1730 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1731 PFID(&lli->lli_fid));
1735 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1737 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1738 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1742 req = oit.d.lustre.it_data;
1743 rc = it_open_error(DISP_IT_EXECD, &oit);
1749 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1755 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1756 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1760 llu_update_inode(ino, &md);
1761 llu_local_open(lli, &oit);
1762 /* release intent */
1763 if (lustre_handle_is_used(&lockh))
1764 ldlm_lock_decref(&lockh, LCK_CR);
1765 ptlrpc_req_finished(req);
1767 rc = llu_file_release(ino);
1772 ptlrpc_req_finished(req);
1776 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1778 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1780 int flags = FMODE_WRITE;
1783 LASSERT(sizeof(lum) == sizeof(*lump));
1784 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1785 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1788 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1792 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1794 struct intnl_stat *st = llu_i2stat(ino);
1795 if (S_ISREG(st->st_mode))
1796 return llu_lov_file_setstripe(ino, arg);
1797 if (S_ISDIR(st->st_mode))
1798 return llu_lov_dir_setstripe(ino, arg);
1803 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1805 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1810 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1814 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1820 liblustre_wait_event(0);
1823 case LL_IOC_GROUP_LOCK:
1824 arg = va_arg(ap, unsigned long);
1825 rc = llu_get_grouplock(ino, arg);
1827 case LL_IOC_GROUP_UNLOCK:
1828 arg = va_arg(ap, unsigned long);
1829 rc = llu_put_grouplock(ino, arg);
1831 case LL_IOC_LOV_SETSTRIPE:
1832 arg = va_arg(ap, unsigned long);
1833 rc = llu_lov_setstripe(ino, arg);
1835 case LL_IOC_LOV_GETSTRIPE:
1836 arg = va_arg(ap, unsigned long);
1837 rc = llu_lov_getstripe(ino, arg);
1840 CERROR("did not support ioctl cmd %lx\n", request);
1845 liblustre_wait_event(0);
1850 * we already do syncronous read/write
1852 static int llu_iop_sync(struct inode *inode)
1854 liblustre_wait_event(0);
1858 static int llu_iop_datasync(struct inode *inode)
1860 liblustre_wait_event(0);
1864 struct filesys_ops llu_filesys_ops =
1866 fsop_gone: llu_fsop_gone,
1869 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1871 struct inode *inode;
1873 struct file_identifier fileid = {&fid, sizeof(fid)};
1875 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1876 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1877 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1879 return ERR_PTR(-EPERM);
1882 /* try to find existing inode */
1883 fid = md->body->fid1;
1885 inode = _sysio_i_find(fs, &fileid);
1887 if (inode->i_zombie/* ||
1888 lli->lli_st_generation != md->body->generation*/) {
1892 llu_update_inode(inode, md);
1897 inode = llu_new_inode(fs, &fid);
1899 llu_update_inode(inode, md);
1905 llu_fsswop_mount(const char *source,
1907 const void *data __IS_UNUSED,
1908 struct pnode *tocover,
1909 struct mount **mntp)
1913 struct pnode_base *rootpb;
1914 struct obd_device *obd;
1915 struct llu_sb_info *sbi;
1916 struct obd_statfs osfs;
1917 static struct qstr noname = { NULL, 0, 0 };
1918 struct ptlrpc_request *request = NULL;
1919 struct lustre_md md;
1921 struct config_llog_instance cfg = {0, };
1922 char ll_instance[sizeof(sbi) * 2 + 1];
1923 struct lustre_profile *lprof;
1924 char *zconf_mgsnid, *zconf_profile;
1925 char *osc = NULL, *mdc = NULL;
1926 int async = 1, err = -EINVAL;
1927 struct obd_connect_data ocd = {0,};
1928 struct md_op_data op_data = {{0}};
1932 if (ll_parse_mount_target(source,
1935 CERROR("mal-formed target %s\n", source);
1938 if (!zconf_mgsnid || !zconf_profile) {
1939 printf("Liblustre: invalid target %s\n", source);
1942 /* allocate & initialize sbi */
1943 OBD_ALLOC(sbi, sizeof(*sbi));
1947 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1948 ll_generate_random_uuid(uuid);
1949 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1951 /* generate a string unique to this super, let's try
1952 the address of the super itself.*/
1953 sprintf(ll_instance, "%p", sbi);
1955 /* retrive & parse config log */
1956 cfg.cfg_instance = ll_instance;
1957 cfg.cfg_uuid = sbi->ll_sb_uuid;
1958 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1960 CERROR("Unable to process log: %s\n", zconf_profile);
1961 GOTO(out_free, err);
1964 lprof = class_get_profile(zconf_profile);
1965 if (lprof == NULL) {
1966 CERROR("No profile found: %s\n", zconf_profile);
1967 GOTO(out_free, err = -EINVAL);
1969 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1970 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
1972 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
1973 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
1977 GOTO(out_free, err = -EINVAL);
1981 GOTO(out_free, err = -EINVAL);
1984 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1990 obd = class_name2obd(mdc);
1992 CERROR("MDC %s: not setup or attached\n", mdc);
1993 GOTO(out_free, err = -EINVAL);
1995 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1996 sizeof(async), &async, NULL);
1998 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1999 OBD_CONNECT_FID | OBD_CONNECT_AT |
2000 OBD_CONNECT_VBR | OBD_CONNECT_SOM;
2001 #ifdef LIBLUSTRE_POSIX_ACL
2002 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2004 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2005 ocd.ocd_version = LUSTRE_VERSION_CODE;
2008 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2010 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2011 GOTO(out_free, err);
2014 err = obd_statfs(obd, &osfs, 100000000, 0);
2019 * FIXME fill fs stat data into sbi here!!! FIXME
2023 obd = class_name2obd(osc);
2025 CERROR("OSC %s: not setup or attached\n", osc);
2026 GOTO(out_md, err = -EINVAL);
2028 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2029 sizeof(async), &async, NULL);
2031 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2032 obd->obd_upcall.onu_upcall = cl_ocd_update;
2034 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2035 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2036 OBD_CONNECT_FID | OBD_CONNECT_AT |
2039 ocd.ocd_version = LUSTRE_VERSION_CODE;
2040 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2042 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2045 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2046 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
2047 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
2049 fid_zero(&sbi->ll_root_fid);
2050 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
2052 CERROR("cannot mds_connect: rc = %d\n", err);
2053 GOTO(out_lock_cn_cb, err);
2055 if (!fid_is_sane(&sbi->ll_root_fid)) {
2056 CERROR("Invalid root fid during mount\n");
2057 GOTO(out_lock_cn_cb, err = -EINVAL);
2059 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
2061 op_data.op_fid1 = sbi->ll_root_fid;
2062 op_data.op_valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS;
2063 /* fetch attr of root inode */
2064 err = md_getattr(sbi->ll_md_exp, &op_data, &request);
2066 CERROR("md_getattr failed for root: rc = %d\n", err);
2067 GOTO(out_lock_cn_cb, err);
2070 err = md_get_lustre_md(sbi->ll_md_exp, request,
2071 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2073 CERROR("failed to understand root inode md: rc = %d\n",err);
2074 GOTO(out_request, err);
2077 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2079 root = llu_iget(fs, &md);
2080 if (!root || IS_ERR(root)) {
2081 CERROR("fail to generate root inode\n");
2082 GOTO(out_request, err = -EBADF);
2086 * Generate base path-node for root.
2088 rootpb = _sysio_pb_new(&noname, NULL, root);
2094 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2096 _sysio_pb_gone(rootpb);
2102 ptlrpc_req_finished(request);
2104 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2105 liblustre_wait_idle();
2110 _sysio_i_gone(root);
2112 ptlrpc_req_finished(request);
2114 obd_disconnect(sbi->ll_dt_exp);
2116 obd_disconnect(sbi->ll_md_exp);
2119 OBD_FREE(osc, strlen(osc) + 1);
2121 OBD_FREE(mdc, strlen(mdc) + 1);
2122 OBD_FREE(sbi, sizeof(*sbi));
2123 liblustre_wait_idle();
2127 struct fssw_ops llu_fssw_ops = {
2131 static struct inode_ops llu_inode_ops = {
2132 inop_lookup: llu_iop_lookup,
2133 inop_getattr: llu_iop_getattr,
2134 inop_setattr: llu_iop_setattr,
2135 inop_filldirentries: llu_iop_filldirentries,
2136 inop_mkdir: llu_iop_mkdir_raw,
2137 inop_rmdir: llu_iop_rmdir_raw,
2138 inop_symlink: llu_iop_symlink_raw,
2139 inop_readlink: llu_iop_readlink,
2140 inop_open: llu_iop_open,
2141 inop_close: llu_iop_close,
2142 inop_link: llu_iop_link_raw,
2143 inop_unlink: llu_iop_unlink_raw,
2144 inop_rename: llu_iop_rename_raw,
2145 inop_pos: llu_iop_pos,
2146 inop_read: llu_iop_read,
2147 inop_write: llu_iop_write,
2148 inop_iodone: llu_iop_iodone,
2149 inop_fcntl: llu_iop_fcntl,
2150 inop_sync: llu_iop_sync,
2151 inop_datasync: llu_iop_datasync,
2152 inop_ioctl: llu_iop_ioctl,
2153 inop_mknod: llu_iop_mknod_raw,
2154 #ifdef _HAVE_STATVFS
2155 inop_statvfs: llu_iop_statvfs,
2157 inop_gone: llu_iop_gone,