4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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>
57 #include "llite_lib.h"
65 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
67 static int ll_permission(struct inode *inode, int mask)
69 struct intnl_stat *st = llu_i2stat(inode);
70 mode_t mode = st->st_mode;
72 if (current->fsuid == st->st_uid)
74 else if (cfs_curproc_is_in_groups(st->st_gid))
77 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
80 if ((mask & (MAY_READ|MAY_WRITE)) ||
81 (st->st_mode & S_IXUGO))
82 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
85 if (mask == MAY_READ ||
86 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
87 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
94 static void llu_fsop_gone(struct filesys *fs)
96 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
97 struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
101 cfs_list_del(&sbi->ll_conn_chain);
103 obd_disconnect(sbi->ll_dt_exp);
104 obd_disconnect(sbi->ll_md_exp);
106 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
107 class_manual_cleanup(obd);
109 OBD_FREE(sbi, sizeof(*sbi));
111 liblustre_wait_idle();
115 static struct inode_ops llu_inode_ops;
117 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
118 struct lustre_handle *lockh)
120 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
126 fid = &llu_i2info(inode)->lli_fid;
127 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
129 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
130 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
131 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
135 void llu_update_inode(struct inode *inode, struct lustre_md *md)
137 struct llu_inode_info *lli = llu_i2info(inode);
138 struct mdt_body *body = md->body;
139 struct lov_stripe_md *lsm = md->lsm;
140 struct intnl_stat *st = llu_i2stat(inode);
142 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
144 if (body->valid & OBD_MD_FLMODE)
145 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
146 if (body->valid & OBD_MD_FLTYPE)
147 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
150 if (!lli->lli_has_smd) {
151 cl_file_inode_init(inode, md);
152 lli->lli_has_smd = true;
153 lli->lli_maxbytes = lsm->lsm_maxbytes;
154 if (lli->lli_maxbytes > MAX_LFS_FILESIZE)
155 lli->lli_maxbytes = MAX_LFS_FILESIZE;
158 obd_free_memmd(llu_i2obdexp(inode), &md->lsm);
161 if (body->valid & OBD_MD_FLATIME) {
162 if (body->atime > LTIME_S(st->st_atime))
163 LTIME_S(st->st_atime) = body->atime;
164 lli->lli_lvb.lvb_atime = body->atime;
166 if (body->valid & OBD_MD_FLMTIME) {
167 if (body->mtime > LTIME_S(st->st_mtime))
168 LTIME_S(st->st_mtime) = body->mtime;
169 lli->lli_lvb.lvb_mtime = body->mtime;
171 if (body->valid & OBD_MD_FLCTIME) {
172 if (body->ctime > LTIME_S(st->st_ctime))
173 LTIME_S(st->st_ctime) = body->ctime;
174 lli->lli_lvb.lvb_ctime = body->ctime;
176 if (S_ISREG(st->st_mode))
177 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
179 st->st_blksize = 4096;
180 if (body->valid & OBD_MD_FLUID)
181 st->st_uid = body->uid;
182 if (body->valid & OBD_MD_FLGID)
183 st->st_gid = body->gid;
184 if (body->valid & OBD_MD_FLNLINK)
185 st->st_nlink = body->nlink;
186 if (body->valid & OBD_MD_FLRDEV)
187 st->st_rdev = body->rdev;
188 if (body->valid & OBD_MD_FLFLAGS)
189 lli->lli_st_flags = body->flags;
190 if (body->valid & OBD_MD_FLSIZE) {
191 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
192 S_ISREG(st->st_mode) && lli->lli_has_smd) {
193 struct lustre_handle lockh;
196 /* As it is possible a blocking ast has been processed
197 * by this time, we need to check there is an UPDATE
198 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
200 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
203 st->st_size = body->size;
204 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
205 ldlm_lock_decref(&lockh, mode);
208 st->st_size = body->size;
211 if (body->valid & OBD_MD_FLBLOCKS)
212 st->st_blocks = body->blocks;
216 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
218 struct llu_inode_info *lli = llu_i2info(dst);
219 struct intnl_stat *st = llu_i2stat(dst);
221 valid &= src->o_valid;
223 LASSERTF(!(valid & (OBD_MD_FLTYPE | OBD_MD_FLGENER | OBD_MD_FLFID |
224 OBD_MD_FLID | OBD_MD_FLGROUP)),
225 "object "LPU64"/"LPU64", valid %x\n",
226 src->o_id, src->o_seq, valid);
228 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
229 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
232 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
233 (long)src->o_mtime, (long)src->o_ctime);
235 if (valid & OBD_MD_FLATIME)
236 LTIME_S(st->st_atime) = src->o_atime;
237 if (valid & OBD_MD_FLMTIME)
238 LTIME_S(st->st_mtime) = src->o_mtime;
239 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
240 LTIME_S(st->st_ctime) = src->o_ctime;
241 if (valid & OBD_MD_FLSIZE)
242 st->st_size = src->o_size;
243 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
244 st->st_blocks = src->o_blocks;
245 if (valid & OBD_MD_FLBLKSZ)
246 st->st_blksize = src->o_blksize;
247 if (valid & OBD_MD_FLTYPE)
248 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
249 if (valid & OBD_MD_FLMODE)
250 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
251 if (valid & OBD_MD_FLUID)
252 st->st_uid = src->o_uid;
253 if (valid & OBD_MD_FLGID)
254 st->st_gid = src->o_gid;
255 if (valid & OBD_MD_FLFLAGS)
256 lli->lli_st_flags = src->o_flags;
260 * Performs the getattr on the inode and updates its fields.
261 * If @sync != 0, perform the getattr under the server-side lock.
263 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
264 __u64 ioepoch, int sync)
266 struct ptlrpc_request_set *set;
267 struct lov_stripe_md *lsm = NULL;
268 struct obd_info oinfo = { { { 0 } } };
272 lsm = ccc_inode_lsm_get(inode);
277 oinfo.oi_oa->o_id = lsm->lsm_object_id;
278 oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
279 oinfo.oi_oa->o_mode = S_IFREG;
280 oinfo.oi_oa->o_ioepoch = ioepoch;
281 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
282 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
283 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
284 OBD_MD_FLCTIME | OBD_MD_FLGROUP |
285 OBD_MD_FLATIME | OBD_MD_FLEPOCH;
286 obdo_set_parent_fid(oinfo.oi_oa, &llu_i2info(inode)->lli_fid);
288 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
289 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
292 set = ptlrpc_prep_set();
294 CERROR ("ENOMEM allocing request set\n");
297 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
299 rc = ptlrpc_set_wait(set);
300 ptlrpc_set_destroy(set);
302 ccc_inode_lsm_put(inode, lsm);
306 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
307 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
310 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
311 CDEBUG(D_INODE, "objid "LPX64" size %llu, blocks %llu, "
312 "blksize %llu\n", oinfo.oi_oa->o_id,
313 (long long unsigned)llu_i2stat(inode)->st_size,
314 (long long unsigned)llu_i2stat(inode)->st_blocks,
315 (long long unsigned)llu_i2stat(inode)->st_blksize);
319 static struct inode* llu_new_inode(struct filesys *fs,
323 struct llu_inode_info *lli;
324 struct intnl_stat st = {
327 #ifndef AUTOMOUNT_FILE_NAME
328 .st_mode = fid->f_type & S_IFMT,
330 .st_mode = fid->f_type /* all of the bits! */
333 /* FIXME: fix this later */
340 OBD_ALLOC(lli, sizeof(*lli));
344 /* initialize lli here */
345 lli->lli_sbi = llu_fs2sbi(fs);
346 lli->lli_has_smd = false;
347 lli->lli_symlink_name = NULL;
349 lli->lli_maxbytes = (__u64)(~0UL);
350 lli->lli_file_data = NULL;
352 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
353 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
356 /* file identifier is needed by functions like _sysio_i_find() */
357 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
358 &st, 0, &llu_inode_ops, lli);
361 OBD_FREE(lli, sizeof(*lli));
366 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
368 struct lustre_handle lockh;
369 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
376 fid = &llu_i2info(inode)->lli_fid;
377 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
379 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
380 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
381 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
387 static int llu_inode_revalidate(struct inode *inode)
389 struct llu_inode_info *lli = llu_i2info(inode);
390 struct intnl_stat *st = llu_i2stat(inode);
394 CERROR("REPORT THIS LINE TO PETER\n");
398 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
400 struct ptlrpc_request *req = NULL;
401 struct llu_sb_info *sbi = llu_i2sbi(inode);
402 struct md_op_data op_data = { { 0 } };
403 unsigned long valid = OBD_MD_FLGETATTR;
406 /* Why don't we update all valid MDS fields here, if we're
407 * doing an RPC anyways? -phil */
408 if (S_ISREG(st->st_mode)) {
409 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
410 valid |= OBD_MD_FLEASIZE;
413 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, ealen,
415 op_data.op_valid = valid;
417 rc = md_getattr(sbi->ll_md_exp, &op_data, &req);
419 CERROR("failure %d inode %llu\n", rc,
420 (long long)st->st_ino);
423 rc = md_get_lustre_md(sbi->ll_md_exp, req,
424 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
426 /* XXX Too paranoid? */
427 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
428 !((md.body->valid & OBD_MD_FLNLINK) &&
429 (md.body->nlink == 0))) {
430 CERROR("Asked for %s eadata but got %s (%d)\n",
431 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
432 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
433 md.body->eadatasize);
436 ptlrpc_req_finished(req);
441 llu_update_inode(inode, &md);
443 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
444 ptlrpc_req_finished(req);
447 if (!lli->lli_has_smd) {
448 /* object not yet allocated, don't validate size */
449 st->st_atime = lli->lli_lvb.lvb_atime;
450 st->st_mtime = lli->lli_lvb.lvb_mtime;
451 st->st_ctime = lli->lli_lvb.lvb_ctime;
455 /* ll_glimpse_size will prefer locally cached writes if they extend
457 RETURN(cl_glimpse_size(inode));
460 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
462 *b = *llu_i2stat(ino);
465 static int llu_iop_getattr(struct pnode *pno,
467 struct intnl_stat *b)
472 liblustre_wait_event(0);
476 LASSERT(pno->p_base->pb_ino);
477 ino = pno->p_base->pb_ino;
479 LASSERT(!pno || pno->p_base->pb_ino == ino);
482 /* libsysio might call us directly without intent lock,
483 * we must re-fetch the attrs here
485 rc = llu_inode_revalidate(ino);
487 copy_stat_buf(ino, b);
488 LASSERT(!llu_i2info(ino)->lli_it);
491 liblustre_wait_event(0);
495 static int null_if_equal(struct ldlm_lock *lock, void *data)
497 if (data == lock->l_ast_data) {
498 lock->l_ast_data = NULL;
500 if (lock->l_req_mode != lock->l_granted_mode)
501 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
504 return LDLM_ITER_CONTINUE;
507 void llu_clear_inode(struct inode *inode)
509 struct llu_inode_info *lli = llu_i2info(inode);
510 struct llu_sb_info *sbi = llu_i2sbi(inode);
511 struct lov_stripe_md *lsm;
514 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
515 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
518 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
519 md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
521 lsm = ccc_inode_lsm_get(inode);
523 obd_change_cbdata(sbi->ll_dt_exp, lsm, null_if_equal, inode);
524 ccc_inode_lsm_put(inode, lsm);
526 cl_inode_fini(inode);
527 lli->lli_has_smd = false;
529 if (lli->lli_symlink_name) {
530 OBD_FREE(lli->lli_symlink_name,
531 strlen(lli->lli_symlink_name) + 1);
532 lli->lli_symlink_name = NULL;
538 void llu_iop_gone(struct inode *inode)
540 struct llu_inode_info *lli = llu_i2info(inode);
543 liblustre_wait_event(0);
544 llu_clear_inode(inode);
546 OBD_FREE(lli, sizeof(*lli));
550 static int inode_setattr(struct inode * inode, struct iattr * attr)
552 unsigned int ia_valid = attr->ia_valid;
553 struct intnl_stat *st = llu_i2stat(inode);
557 * inode_setattr() is only ever invoked with ATTR_SIZE (by
558 * llu_setattr_raw()) when file has no bodies. Check this.
560 LASSERT(ergo(ia_valid & ATTR_SIZE, !llu_i2info(inode)->lli_has_smd));
562 if (ia_valid & ATTR_SIZE)
563 st->st_size = attr->ia_size;
564 if (ia_valid & ATTR_UID)
565 st->st_uid = attr->ia_uid;
566 if (ia_valid & ATTR_GID)
567 st->st_gid = attr->ia_gid;
568 if (ia_valid & ATTR_ATIME)
569 st->st_atime = attr->ia_atime;
570 if (ia_valid & ATTR_MTIME)
571 st->st_mtime = attr->ia_mtime;
572 if (ia_valid & ATTR_CTIME)
573 st->st_ctime = attr->ia_ctime;
574 if (ia_valid & ATTR_MODE) {
575 st->st_mode = attr->ia_mode;
576 if (!cfs_curproc_is_in_groups(st->st_gid) &&
577 !cfs_capable(CFS_CAP_FSETID))
578 st->st_mode &= ~S_ISGID;
580 /* mark_inode_dirty(inode); */
584 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
585 struct md_open_data **mod)
588 struct llu_sb_info *sbi = llu_i2sbi(inode);
589 struct ptlrpc_request *request = NULL;
593 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
594 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
598 ptlrpc_req_finished(request);
599 if (rc != -EPERM && rc != -EACCES)
600 CERROR("md_setattr fails: rc = %d\n", rc);
604 rc = md_get_lustre_md(sbi->ll_md_exp, request,
605 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
607 ptlrpc_req_finished(request);
611 /* We call inode_setattr to adjust timestamps.
612 * If there is at least some data in file, we cleared ATTR_SIZE
613 * above to avoid invoking vmtruncate, otherwise it is important
614 * to call vmtruncate in inode_setattr to update inode->i_size
616 inode_setattr(inode, &op_data->op_attr);
617 llu_update_inode(inode, &md);
618 ptlrpc_req_finished(request);
623 /* Close IO epoch and send Size-on-MDS attribute update. */
624 static int llu_setattr_done_writing(struct inode *inode,
625 struct md_op_data *op_data,
626 struct md_open_data *mod)
628 struct llu_inode_info *lli = llu_i2info(inode);
629 struct intnl_stat *st = llu_i2stat(inode);
633 LASSERT(op_data != NULL);
634 if (!S_ISREG(st->st_mode))
637 /* XXX: pass och here for the recovery purpose. */
638 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
639 op_data->op_ioepoch, PFID(&lli->lli_fid));
641 op_data->op_flags = MF_EPOCH_CLOSE;
642 llu_done_writing_attr(inode, op_data);
643 llu_pack_inode2opdata(inode, op_data, NULL);
645 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
647 /* MDS has instructed us to obtain Size-on-MDS attribute
648 * from OSTs and send setattr to back to MDS. */
649 rc = llu_som_update(inode, op_data);
651 CERROR("inode %llu mdc truncate failed: rc = %d\n",
652 (unsigned long long)st->st_ino, rc);
657 /* If this inode has objects allocated to it (lsm != NULL), then the OST
658 * object(s) determine the file size and mtime. Otherwise, the MDS will
659 * keep these values until such a time that objects are allocated for it.
660 * We do the MDS operations first, as it is checking permissions for us.
661 * We don't to the MDS RPC if there is nothing that we want to store there,
662 * otherwise there is no harm in updating mtime/atime on the MDS if we are
663 * going to do an RPC anyways.
665 * If we are doing a truncate, we will send the mtime and ctime updates
666 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
667 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
670 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
672 int has_lsm = llu_i2info(inode)->lli_has_smd;
673 struct intnl_stat *st = llu_i2stat(inode);
674 int ia_valid = attr->ia_valid;
675 struct md_op_data op_data = { { 0 } };
676 struct md_open_data *mod = NULL;
680 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
682 if (ia_valid & ATTR_SIZE) {
683 if (attr->ia_size > ll_file_maxbytes(inode)) {
684 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
685 (long long)attr->ia_size,
686 ll_file_maxbytes(inode));
690 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
693 /* We mark all of the fields "set" so MDS/OST does not re-set them */
694 if (attr->ia_valid & ATTR_CTIME) {
695 attr->ia_ctime = CFS_CURRENT_TIME;
696 attr->ia_valid |= ATTR_CTIME_SET;
698 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
699 attr->ia_atime = CFS_CURRENT_TIME;
700 attr->ia_valid |= ATTR_ATIME_SET;
702 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
703 attr->ia_mtime = CFS_CURRENT_TIME;
704 attr->ia_valid |= ATTR_MTIME_SET;
707 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
708 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
709 ", now = "CFS_TIME_T"\n",
710 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
711 LTIME_S(CFS_CURRENT_TIME));
713 /* NB: ATTR_SIZE will only be set after this point if the size
714 * resides on the MDS, ie, this file has no objects. */
716 attr->ia_valid &= ~ATTR_SIZE;
718 /* If only OST attributes being set on objects, don't do MDS RPC.
719 * In that case, we need to check permissions and update the local
720 * inode ourselves so we can call obdo_from_inode() always. */
721 if (ia_valid & (has_lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
722 memcpy(&op_data.op_attr, attr, sizeof(*attr));
724 /* Open epoch for truncate. */
725 if (exp_connect_som(llu_i2mdexp(inode)) &&
726 (ia_valid & ATTR_SIZE))
727 op_data.op_flags = MF_EPOCH_OPEN;
728 rc = llu_md_setattr(inode, &op_data, &mod);
732 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
733 if (!has_lsm || !S_ISREG(st->st_mode)) {
734 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
738 /* The OST doesn't check permissions, but the alternative is
739 * a gratuitous RPC to the MDS. We already rely on the client
740 * to do read/write/truncate permission checks, so is mtime OK?
742 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
743 /* from sys_utime() */
744 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
745 if (current->fsuid != st->st_uid &&
746 (rc = ll_permission(inode, MAY_WRITE)) != 0)
749 /* from inode_change_ok() */
750 if (current->fsuid != st->st_uid &&
751 !cfs_capable(CFS_CAP_FOWNER))
757 /* Won't invoke llu_vmtruncate(), as we already cleared
759 inode_setattr(inode, attr);
762 if (ia_valid & ATTR_SIZE)
763 attr->ia_valid |= ATTR_SIZE;
764 if (ia_valid & (ATTR_SIZE |
765 ATTR_ATIME | ATTR_ATIME_SET |
766 ATTR_MTIME | ATTR_MTIME_SET))
767 /* on truncate and utimes send attributes to osts, setting
768 * mtime/atime to past will be performed under PW 0:EOF extent
769 * lock (new_size:EOF for truncate)
770 * it may seem excessive to send mtime/atime updates to osts
771 * when not setting times to past, but it is necessary due to
772 * possible time de-synchronization */
773 rc = cl_setattr_ost(inode, attr, NULL);
776 if (op_data.op_ioepoch)
777 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
778 return rc ? rc : rc1;
781 /* here we simply act as a thin layer to glue it with
782 * llu_setattr_raw(), which is copy from kernel
784 static int llu_iop_setattr(struct pnode *pno,
787 struct intnl_stat *stbuf)
793 liblustre_wait_event(0);
795 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
796 SETATTR_UID | SETATTR_GID |
797 SETATTR_LEN | SETATTR_MODE)));
798 memset(&iattr, 0, sizeof(iattr));
800 if (mask & SETATTR_MODE) {
801 iattr.ia_mode = stbuf->st_mode;
802 iattr.ia_valid |= ATTR_MODE;
804 if (mask & SETATTR_MTIME) {
805 iattr.ia_mtime = stbuf->st_mtime;
806 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
808 if (mask & SETATTR_ATIME) {
809 iattr.ia_atime = stbuf->st_atime;
810 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
812 if (mask & SETATTR_UID) {
813 iattr.ia_uid = stbuf->st_uid;
814 iattr.ia_valid |= ATTR_UID;
816 if (mask & SETATTR_GID) {
817 iattr.ia_gid = stbuf->st_gid;
818 iattr.ia_valid |= ATTR_GID;
820 if (mask & SETATTR_LEN) {
821 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
822 iattr.ia_valid |= ATTR_SIZE;
825 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
826 iattr.ia_ctime = CFS_CURRENT_TIME;
828 rc = llu_setattr_raw(ino, &iattr);
829 liblustre_wait_idle();
833 #define EXT2_LINK_MAX 32000
835 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
837 struct inode *dir = pno->p_base->pb_parent->pb_ino;
838 struct qstr *qstr = &pno->p_base->pb_name;
839 const char *name = qstr->name;
841 struct ptlrpc_request *request = NULL;
842 struct llu_sb_info *sbi = llu_i2sbi(dir);
843 struct md_op_data op_data = {{ 0 }};
847 liblustre_wait_event(0);
848 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
851 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
854 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
855 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
856 cfs_curproc_cap_pack(), 0, &request);
857 ptlrpc_req_finished(request);
858 liblustre_wait_event(0);
862 static int llu_readlink_internal(struct inode *inode,
863 struct ptlrpc_request **request,
866 struct llu_inode_info *lli = llu_i2info(inode);
867 struct llu_sb_info *sbi = llu_i2sbi(inode);
868 struct mdt_body *body;
869 struct intnl_stat *st = llu_i2stat(inode);
870 struct md_op_data op_data = {{ 0 }};
871 int rc, symlen = st->st_size + 1;
877 if (lli->lli_symlink_name) {
878 *symname = lli->lli_symlink_name;
879 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
883 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, symlen,
885 op_data.op_valid = OBD_MD_LINKNAME;
887 rc = md_getattr(sbi->ll_md_exp, &op_data, request);
889 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
893 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
894 LASSERT(body != NULL);
896 if ((body->valid & OBD_MD_LINKNAME) == 0) {
897 CERROR ("OBD_MD_LINKNAME not set on reply\n");
898 GOTO (failed, rc = -EPROTO);
901 LASSERT(symlen != 0);
902 if (body->eadatasize != symlen) {
903 CERROR("inode %llu: symlink length %d not expected %d\n",
904 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
905 GOTO(failed, rc = -EPROTO);
908 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
909 if (*symname == NULL ||
910 strnlen(*symname, symlen) != symlen - 1) {
911 /* not full/NULL terminated */
912 CERROR("inode %llu: symlink not NULL terminated string"
913 "of length %d\n", (long long)st->st_ino, symlen - 1);
914 GOTO(failed, rc = -EPROTO);
917 OBD_ALLOC(lli->lli_symlink_name, symlen);
918 /* do not return an error if we cannot cache the symlink locally */
919 if (lli->lli_symlink_name)
920 memcpy(lli->lli_symlink_name, *symname, symlen);
925 ptlrpc_req_finished (*request);
929 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
931 struct inode *inode = pno->p_base->pb_ino;
932 struct ptlrpc_request *request;
937 liblustre_wait_event(0);
938 rc = llu_readlink_internal(inode, &request, &symname);
943 strncpy(data, symname, bufsize);
944 rc = strlen(symname);
946 ptlrpc_req_finished(request);
948 liblustre_wait_event(0);
952 static int llu_iop_mknod_raw(struct pnode *pno,
956 struct ptlrpc_request *request = NULL;
957 struct inode *dir = pno->p_parent->p_base->pb_ino;
958 struct llu_sb_info *sbi = llu_i2sbi(dir);
959 struct md_op_data op_data = {{ 0 }};
963 liblustre_wait_event(0);
964 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
965 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
966 (long long)llu_i2stat(dir)->st_ino);
968 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
971 switch (mode & S_IFMT) {
974 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
979 llu_prep_md_op_data(&op_data, dir, NULL,
980 pno->p_base->pb_name.name,
981 pno->p_base->pb_name.len, 0,
984 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
985 current->fsuid, current->fsgid,
986 cfs_curproc_cap_pack(), dev, &request);
987 ptlrpc_req_finished(request);
995 liblustre_wait_event(0);
999 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1001 struct inode *src = old->p_base->pb_ino;
1002 struct inode *dir = new->p_parent->p_base->pb_ino;
1003 const char *name = new->p_base->pb_name.name;
1004 int namelen = new->p_base->pb_name.len;
1005 struct ptlrpc_request *request = NULL;
1006 struct md_op_data op_data = {{ 0 }};
1013 liblustre_wait_event(0);
1014 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1016 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1017 ptlrpc_req_finished(request);
1018 liblustre_wait_event(0);
1024 * libsysio will clear the inode immediately after return
1026 static int llu_iop_unlink_raw(struct pnode *pno)
1028 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1029 struct qstr *qstr = &pno->p_base->pb_name;
1030 const char *name = qstr->name;
1031 int len = qstr->len;
1032 struct inode *target = pno->p_base->pb_ino;
1033 struct ptlrpc_request *request = NULL;
1034 struct md_op_data op_data = { { 0 } };
1040 liblustre_wait_event(0);
1041 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1043 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1045 rc = llu_objects_destroy(request, dir);
1046 ptlrpc_req_finished(request);
1047 liblustre_wait_idle();
1052 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1054 struct inode *src = old->p_parent->p_base->pb_ino;
1055 struct inode *tgt = new->p_parent->p_base->pb_ino;
1056 const char *oldname = old->p_base->pb_name.name;
1057 int oldnamelen = old->p_base->pb_name.len;
1058 const char *newname = new->p_base->pb_name.name;
1059 int newnamelen = new->p_base->pb_name.len;
1060 struct ptlrpc_request *request = NULL;
1061 struct md_op_data op_data = { { 0 } };
1068 liblustre_wait_event(0);
1069 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1071 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1072 oldname, oldnamelen, newname, newnamelen,
1075 rc = llu_objects_destroy(request, src);
1078 ptlrpc_req_finished(request);
1079 liblustre_wait_idle();
1084 #ifdef _HAVE_STATVFS
1085 static int llu_statfs_internal(struct llu_sb_info *sbi,
1086 struct obd_statfs *osfs, __u64 max_age)
1088 struct obd_statfs obd_osfs;
1092 rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, 0);
1094 CERROR("md_statfs fails: rc = %d\n", rc);
1098 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1099 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1101 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1102 &obd_statfs, max_age, 0);
1104 CERROR("obd_statfs fails: rc = %d\n", rc);
1108 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1109 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1112 osfs->os_blocks = obd_osfs.os_blocks;
1113 osfs->os_bfree = obd_osfs.os_bfree;
1114 osfs->os_bavail = obd_osfs.os_bavail;
1116 /* If we don't have as many objects free on the OST as inodes
1117 * on the MDS, we reduce the total number of inodes to
1118 * compensate, so that the "inodes in use" number is correct.
1120 if (obd_osfs.os_ffree < osfs->os_ffree) {
1121 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1123 osfs->os_ffree = obd_osfs.os_ffree;
1129 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1131 struct obd_statfs osfs;
1134 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1136 /* For now we will always get up-to-date statfs values, but in the
1137 * future we may allow some amount of caching on the client (e.g.
1138 * from QOS or lprocfs updates). */
1139 rc = llu_statfs_internal(sbi, &osfs,
1140 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS));
1144 statfs_unpack(sfs, &osfs);
1146 if (sizeof(sfs->f_blocks) == 4) {
1147 while (osfs.os_blocks > ~0UL) {
1150 osfs.os_blocks >>= 1;
1151 osfs.os_bfree >>= 1;
1152 osfs.os_bavail >>= 1;
1156 sfs->f_blocks = osfs.os_blocks;
1157 sfs->f_bfree = osfs.os_bfree;
1158 sfs->f_bavail = osfs.os_bavail;
1163 static int llu_iop_statvfs(struct pnode *pno,
1165 struct intnl_statvfs *buf)
1171 liblustre_wait_event(0);
1174 LASSERT(pno->p_base->pb_ino);
1175 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1179 /* from native driver */
1180 buf->f_bsize = fs.f_bsize; /* file system block size */
1181 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1182 buf->f_blocks = fs.f_blocks;
1183 buf->f_bfree = fs.f_bfree;
1184 buf->f_bavail = fs.f_bavail;
1185 buf->f_files = fs.f_files; /* Total number serial numbers */
1186 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1187 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1188 buf->f_fsid = fs.f_fsid.__val[1];
1189 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1190 buf->f_namemax = fs.f_namelen;
1193 liblustre_wait_event(0);
1196 #endif /* _HAVE_STATVFS */
1198 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1200 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1201 struct qstr *qstr = &pno->p_base->pb_name;
1202 const char *name = qstr->name;
1203 int len = qstr->len;
1204 struct ptlrpc_request *request = NULL;
1205 struct intnl_stat *st = llu_i2stat(dir);
1206 struct md_op_data op_data = {{ 0 }};
1210 liblustre_wait_event(0);
1211 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1212 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1214 if (st->st_nlink >= EXT2_LINK_MAX)
1217 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1220 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1221 mode | S_IFDIR, current->fsuid, current->fsgid,
1222 cfs_curproc_cap_pack(), 0, &request);
1223 ptlrpc_req_finished(request);
1224 liblustre_wait_event(0);
1228 static int llu_iop_rmdir_raw(struct pnode *pno)
1230 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1231 struct qstr *qstr = &pno->p_base->pb_name;
1232 const char *name = qstr->name;
1233 int len = qstr->len;
1234 struct ptlrpc_request *request = NULL;
1235 struct md_op_data op_data = {{ 0 }};
1239 liblustre_wait_event(0);
1240 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1241 (long long)llu_i2stat(dir)->st_ino,
1242 llu_i2info(dir)->lli_st_generation, dir);
1244 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1246 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1247 ptlrpc_req_finished(request);
1249 liblustre_wait_event(0);
1254 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1256 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1258 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1260 /* refer to ll_file_flock() for details */
1261 static int llu_file_flock(struct inode *ino,
1263 struct file_lock *file_lock)
1265 struct llu_inode_info *lli = llu_i2info(ino);
1266 struct intnl_stat *st = llu_i2stat(ino);
1267 struct ldlm_res_id res_id =
1268 { .name = {fid_seq(&lli->lli_fid),
1269 fid_oid(&lli->lli_fid),
1270 fid_ver(&lli->lli_fid),
1272 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1273 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1275 struct lustre_handle lockh = {0};
1276 ldlm_policy_data_t flock;
1280 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1281 (unsigned long long)st->st_ino, file_lock);
1283 flock.l_flock.pid = file_lock->fl_pid;
1284 flock.l_flock.start = file_lock->fl_start;
1285 flock.l_flock.end = file_lock->fl_end;
1287 switch (file_lock->fl_type) {
1289 einfo.ei_mode = LCK_PR;
1292 einfo.ei_mode = LCK_NL;
1295 einfo.ei_mode = LCK_PW;
1298 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1305 #if F_SETLKW64 != F_SETLKW
1313 #if F_SETLK64 != F_SETLK
1317 flags = LDLM_FL_BLOCK_NOWAIT;
1321 #if F_GETLK64 != F_GETLK
1325 flags = LDLM_FL_TEST_LOCK;
1326 file_lock->fl_type = einfo.ei_mode;
1329 CERROR("unknown fcntl cmd: %d\n", cmd);
1333 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#llx, mode=%u, "
1334 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1335 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1339 struct lmv_obd *lmv;
1340 struct obd_device *lmv_obd;
1341 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1342 lmv = &lmv_obd->u.lmv;
1344 if (lmv->desc.ld_tgt_count < 1)
1345 RETURN(rc = -ENODEV);
1347 if (lmv->tgts[0] && lmv->tgts[0]->ltd_exp != NULL)
1348 rc = ldlm_cli_enqueue(lmv->tgts[0]->ltd_exp, NULL,
1349 &einfo, &res_id, &flock, &flags,
1350 NULL, 0, LVB_T_NONE, &lockh, 0);
1357 static int assign_type(struct file_lock *fl, int type)
1370 static int flock_to_posix_lock(struct inode *ino,
1371 struct file_lock *fl,
1374 switch (l->l_whence) {
1375 /* XXX: only SEEK_SET is supported in lustre */
1383 fl->fl_end = l->l_len - 1;
1387 fl->fl_end = OFFSET_MAX;
1389 fl->fl_pid = getpid();
1390 fl->fl_flags = FL_POSIX;
1391 fl->fl_notify = NULL;
1392 fl->fl_insert = NULL;
1393 fl->fl_remove = NULL;
1394 /* XXX: these fields can't be filled with suitable values,
1395 but I think lustre doesn't use them.
1397 fl->fl_owner = NULL;
1400 return assign_type(fl, l->l_type);
1403 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1405 struct file_lock fl;
1409 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1412 error = flock_to_posix_lock(ino, &fl, flock);
1416 error = llu_file_flock(ino, F_GETLK, &fl);
1420 flock->l_type = F_UNLCK;
1421 if (fl.fl_type != F_UNLCK) {
1422 flock->l_pid = fl.fl_pid;
1423 flock->l_start = fl.fl_start;
1424 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1425 fl.fl_end - fl.fl_start + 1;
1426 flock->l_whence = SEEK_SET;
1427 flock->l_type = fl.fl_type;
1434 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1436 struct file_lock fl;
1437 int flags = llu_i2info(ino)->lli_open_flags + 1;
1440 error = flock_to_posix_lock(ino, &fl, flock);
1443 if (cmd == F_SETLKW)
1444 fl.fl_flags |= FL_SLEEP;
1447 switch (flock->l_type) {
1449 if (!(flags & FMODE_READ))
1453 if (!(flags & FMODE_WRITE))
1463 error = llu_file_flock(ino, cmd, &fl);
1471 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1473 struct llu_inode_info *lli = llu_i2info(ino);
1475 struct flock *flock;
1478 liblustre_wait_event(0);
1481 *rtn = lli->lli_open_flags;
1484 flags = va_arg(ap, long);
1485 flags &= FCNTL_FLMASK;
1486 if (flags & FCNTL_FLMASK_INVALID) {
1487 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1488 "the O_NONBLOCK or O_ASYNC flags. "
1489 "Please fix your application.\n");
1494 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1495 (lli->lli_open_flags & ~FCNTL_FLMASK);
1500 #if F_GETLK64 != F_GETLK
1504 flock = va_arg(ap, struct flock *);
1505 err = llu_fcntl_getlk(ino, flock);
1510 #if F_SETLKW64 != F_SETLKW
1516 #if F_SETLK64 != F_SETLK
1520 flock = va_arg(ap, struct flock *);
1521 err = llu_fcntl_setlk(ino, cmd, flock);
1525 CERROR("unsupported fcntl cmd %x\n", cmd);
1531 liblustre_wait_event(0);
1535 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1537 struct llu_inode_info *lli = llu_i2info(inode);
1538 struct ll_file_data *fd = lli->lli_file_data;
1540 struct ccc_grouplock grouplock;
1543 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1546 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1549 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1551 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1552 arg, (lli->lli_open_flags & O_NONBLOCK),
1558 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1559 fd->fd_grouplock = grouplock;
1564 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1566 struct llu_inode_info *lli = llu_i2info(inode);
1567 struct ll_file_data *fd = lli->lli_file_data;
1568 struct ccc_grouplock grouplock;
1571 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1574 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1576 if (fd->fd_grouplock.cg_gid != arg)
1579 grouplock = fd->fd_grouplock;
1580 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1581 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1583 cl_put_grouplock(&grouplock);
1588 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1590 struct llu_sb_info *sbi = llu_i2sbi(ino);
1591 struct ptlrpc_request *request = NULL;
1592 struct md_op_data op_data = {{ 0 }};
1593 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1596 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1599 LASSERT(sizeof(lum) == sizeof(*lump));
1600 LASSERT(sizeof(lum.lmm_objects[0]) ==
1601 sizeof(lump->lmm_objects[0]));
1602 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1605 switch (lum.lmm_magic) {
1606 case LOV_USER_MAGIC_V1: {
1607 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1608 lustre_swab_lov_user_md_v1(&lum);
1611 case LOV_USER_MAGIC_V3: {
1612 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1613 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1617 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1618 " %#08x != %#08x nor %#08x\n",
1619 lum.lmm_magic, LOV_USER_MAGIC_V1,
1625 /* swabbing is done in lov_setstripe() on server side */
1626 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1627 sizeof(lum), NULL, 0, &request, NULL);
1629 ptlrpc_req_finished(request);
1630 if (rc != -EPERM && rc != -EACCES)
1631 CERROR("md_setattr fails: rc = %d\n", rc);
1634 ptlrpc_req_finished(request);
1639 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1640 struct lov_user_md *lum, int lum_size)
1642 struct llu_sb_info *sbi = llu_i2sbi(ino);
1643 struct llu_inode_info *lli = llu_i2info(ino);
1644 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1645 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1646 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1647 struct ptlrpc_request *req = NULL;
1648 struct lustre_md md;
1649 struct md_op_data data = {{ 0 }};
1650 struct lustre_handle lockh;
1654 if (lli->lli_has_smd) {
1655 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1656 PFID(&lli->lli_fid));
1660 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1662 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1663 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1667 req = oit.d.lustre.it_data;
1668 rc = it_open_error(DISP_IT_EXECD, &oit);
1674 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1680 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1681 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1685 llu_update_inode(ino, &md);
1686 llu_local_open(lli, &oit);
1687 /* release intent */
1688 if (lustre_handle_is_used(&lockh))
1689 ldlm_lock_decref(&lockh, LCK_CR);
1690 ptlrpc_req_finished(req);
1692 rc = llu_file_release(ino);
1697 ptlrpc_req_finished(req);
1701 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1703 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1705 int flags = FMODE_WRITE;
1708 LASSERT(sizeof(lum) == sizeof(*lump));
1709 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1710 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1713 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1717 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1719 struct intnl_stat *st = llu_i2stat(ino);
1720 if (S_ISREG(st->st_mode))
1721 return llu_lov_file_setstripe(ino, arg);
1722 if (S_ISDIR(st->st_mode))
1723 return llu_lov_dir_setstripe(ino, arg);
1728 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1730 struct lov_stripe_md *lsm = NULL;
1733 lsm = ccc_inode_lsm_get(ino);
1735 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1737 ccc_inode_lsm_put(ino, lsm);
1741 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1747 liblustre_wait_event(0);
1750 case LL_IOC_GROUP_LOCK:
1751 arg = va_arg(ap, unsigned long);
1752 rc = llu_get_grouplock(ino, arg);
1754 case LL_IOC_GROUP_UNLOCK:
1755 arg = va_arg(ap, unsigned long);
1756 rc = llu_put_grouplock(ino, arg);
1758 case LL_IOC_LOV_SETSTRIPE:
1759 arg = va_arg(ap, unsigned long);
1760 rc = llu_lov_setstripe(ino, arg);
1762 case LL_IOC_LOV_GETSTRIPE:
1763 arg = va_arg(ap, unsigned long);
1764 rc = llu_lov_getstripe(ino, arg);
1767 CERROR("did not support ioctl cmd %lx\n", request);
1772 liblustre_wait_event(0);
1777 * we already do syncronous read/write
1779 static int llu_iop_sync(struct inode *inode)
1781 liblustre_wait_event(0);
1785 static int llu_iop_datasync(struct inode *inode)
1787 liblustre_wait_event(0);
1791 struct filesys_ops llu_filesys_ops =
1793 fsop_gone: llu_fsop_gone,
1796 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1798 struct inode *inode;
1800 struct file_identifier fileid = {&fid, sizeof(fid)};
1802 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1803 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1804 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1806 return ERR_PTR(-EPERM);
1809 /* try to find existing inode */
1810 fid = md->body->fid1;
1812 inode = _sysio_i_find(fs, &fileid);
1814 if (inode->i_zombie/* ||
1815 lli->lli_st_generation != md->body->generation*/) {
1819 llu_update_inode(inode, md);
1824 inode = llu_new_inode(fs, &fid);
1826 llu_update_inode(inode, md);
1832 llu_fsswop_mount(const char *source,
1834 const void *data __IS_UNUSED,
1835 struct pnode *tocover,
1836 struct mount **mntp)
1840 struct pnode_base *rootpb;
1841 struct obd_device *obd;
1842 struct llu_sb_info *sbi;
1843 struct obd_statfs osfs;
1844 static struct qstr noname = { NULL, 0, 0 };
1845 struct ptlrpc_request *request = NULL;
1846 struct lustre_md md;
1848 struct config_llog_instance cfg = {0, };
1849 struct lustre_profile *lprof;
1850 char *zconf_mgsnid, *zconf_profile;
1851 char *osc = NULL, *mdc = NULL;
1852 int async = 1, err = -EINVAL;
1853 struct obd_connect_data ocd = {0,};
1854 struct md_op_data op_data = {{0}};
1855 /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1856 const int instlen = sizeof(cfg.cfg_instance) * 2 + 2;
1860 if (ll_parse_mount_target(source,
1863 CERROR("mal-formed target %s\n", source);
1866 if (!zconf_mgsnid || !zconf_profile) {
1867 printf("Liblustre: invalid target %s\n", source);
1870 /* allocate & initialize sbi */
1871 OBD_ALLOC(sbi, sizeof(*sbi));
1875 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1876 ll_generate_random_uuid(uuid);
1877 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1879 /* generate a string unique to this super, let's try
1880 the address of the super itself.*/
1881 cfg.cfg_instance = sbi;
1883 /* retrive & parse config log */
1884 cfg.cfg_uuid = sbi->ll_sb_uuid;
1885 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1887 CERROR("Unable to process log: %s\n", zconf_profile);
1888 GOTO(out_free, err);
1891 lprof = class_get_profile(zconf_profile);
1892 if (lprof == NULL) {
1893 CERROR("No profile found: %s\n", zconf_profile);
1894 GOTO(out_free, err = -EINVAL);
1896 OBD_ALLOC(osc, strlen(lprof->lp_dt) + instlen + 2);
1897 sprintf(osc, "%s-%p", lprof->lp_dt, cfg.cfg_instance);
1899 OBD_ALLOC(mdc, strlen(lprof->lp_md) + instlen + 2);
1900 sprintf(mdc, "%s-%p", lprof->lp_md, cfg.cfg_instance);
1904 GOTO(out_free, err = -EINVAL);
1908 GOTO(out_free, err = -EINVAL);
1911 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1917 obd = class_name2obd(mdc);
1919 CERROR("MDC %s: not setup or attached\n", mdc);
1920 GOTO(out_free, err = -EINVAL);
1922 obd_set_info_async(NULL, obd->obd_self_export, sizeof(KEY_ASYNC),
1923 KEY_ASYNC, sizeof(async), &async, NULL);
1925 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1926 OBD_CONNECT_FID | OBD_CONNECT_AT |
1927 OBD_CONNECT_VBR | OBD_CONNECT_FULL20 |
1928 OBD_CONNECT_LVB_TYPE;
1930 #ifdef LIBLUSTRE_POSIX_ACL
1931 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
1933 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
1934 ocd.ocd_version = LUSTRE_VERSION_CODE;
1937 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1939 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1940 GOTO(out_free, err);
1943 err = obd_statfs(NULL, sbi->ll_md_exp, &osfs, 100000000, 0);
1948 * FIXME fill fs stat data into sbi here!!! FIXME
1952 obd = class_name2obd(osc);
1954 CERROR("OSC %s: not setup or attached\n", osc);
1955 GOTO(out_md, err = -EINVAL);
1957 obd_set_info_async(NULL, obd->obd_self_export, sizeof(KEY_ASYNC),
1958 KEY_ASYNC, sizeof(async), &async, NULL);
1960 obd->obd_upcall.onu_owner = &sbi->ll_lco;
1961 obd->obd_upcall.onu_upcall = cl_ocd_update;
1963 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
1964 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
1965 OBD_CONNECT_FID | OBD_CONNECT_AT |
1966 OBD_CONNECT_FULL20 | OBD_CONNECT_EINPROGRESS |
1967 OBD_CONNECT_LVB_TYPE;
1969 ocd.ocd_version = LUSTRE_VERSION_CODE;
1970 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1972 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1975 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
1976 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
1977 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
1979 fid_zero(&sbi->ll_root_fid);
1980 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
1982 CERROR("cannot mds_connect: rc = %d\n", err);
1983 GOTO(out_lock_cn_cb, err);
1985 if (!fid_is_sane(&sbi->ll_root_fid)) {
1986 CERROR("Invalid root fid during mount\n");
1987 GOTO(out_lock_cn_cb, err = -EINVAL);
1989 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
1991 op_data.op_fid1 = sbi->ll_root_fid;
1992 op_data.op_valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS;
1993 /* fetch attr of root inode */
1994 err = md_getattr(sbi->ll_md_exp, &op_data, &request);
1996 CERROR("md_getattr failed for root: rc = %d\n", err);
1997 GOTO(out_lock_cn_cb, err);
2000 err = md_get_lustre_md(sbi->ll_md_exp, request,
2001 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2003 CERROR("failed to understand root inode md: rc = %d\n",err);
2004 GOTO(out_request, err);
2007 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2009 root = llu_iget(fs, &md);
2010 if (!root || IS_ERR(root)) {
2011 CERROR("fail to generate root inode\n");
2012 GOTO(out_request, err = -EBADF);
2016 * Generate base path-node for root.
2018 rootpb = _sysio_pb_new(&noname, NULL, root);
2024 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2026 _sysio_pb_gone(rootpb);
2032 ptlrpc_req_finished(request);
2034 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2039 _sysio_i_gone(root);
2041 ptlrpc_req_finished(request);
2043 obd_disconnect(sbi->ll_dt_exp);
2045 obd_disconnect(sbi->ll_md_exp);
2048 OBD_FREE(osc, strlen(lprof->lp_dt) + instlen + 2);
2050 OBD_FREE(mdc, strlen(lprof->lp_md) + instlen + 2);
2051 OBD_FREE(sbi, sizeof(*sbi));
2052 liblustre_wait_idle();
2056 struct fssw_ops llu_fssw_ops = {
2060 static struct inode_ops llu_inode_ops = {
2061 inop_lookup: llu_iop_lookup,
2062 inop_getattr: llu_iop_getattr,
2063 inop_setattr: llu_iop_setattr,
2064 inop_filldirentries: llu_iop_filldirentries,
2065 inop_mkdir: llu_iop_mkdir_raw,
2066 inop_rmdir: llu_iop_rmdir_raw,
2067 inop_symlink: llu_iop_symlink_raw,
2068 inop_readlink: llu_iop_readlink,
2069 inop_open: llu_iop_open,
2070 inop_close: llu_iop_close,
2071 inop_link: llu_iop_link_raw,
2072 inop_unlink: llu_iop_unlink_raw,
2073 inop_rename: llu_iop_rename_raw,
2074 inop_pos: llu_iop_pos,
2075 inop_read: llu_iop_read,
2076 inop_write: llu_iop_write,
2077 inop_iodone: llu_iop_iodone,
2078 inop_fcntl: llu_iop_fcntl,
2079 inop_sync: llu_iop_sync,
2080 inop_datasync: llu_iop_datasync,
2081 inop_ioctl: llu_iop_ioctl,
2082 inop_mknod: llu_iop_mknod_raw,
2083 #ifdef _HAVE_STATVFS
2084 inop_statvfs: llu_iop_statvfs,
2086 inop_gone: llu_iop_gone,