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>
70 #include "llite_lib.h"
78 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
80 static int ll_permission(struct inode *inode, int mask)
82 struct intnl_stat *st = llu_i2stat(inode);
83 mode_t mode = st->st_mode;
85 if (current->fsuid == st->st_uid)
87 else if (in_group_p(st->st_gid))
90 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
93 if ((mask & (MAY_READ|MAY_WRITE)) ||
94 (st->st_mode & S_IXUGO))
95 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
98 if (mask == MAY_READ ||
99 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
100 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
107 static void llu_fsop_gone(struct filesys *fs)
109 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
110 struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
114 list_del(&sbi->ll_conn_chain);
115 obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
116 llu_extent_lock_cancel_cb);
117 obd_disconnect(sbi->ll_dt_exp);
118 obd_disconnect(sbi->ll_md_exp);
120 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
121 class_manual_cleanup(obd);
123 OBD_FREE(sbi, sizeof(*sbi));
125 liblustre_wait_idle();
129 static struct inode_ops llu_inode_ops;
131 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
132 struct lustre_handle *lockh)
134 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
140 fid = &llu_i2info(inode)->lli_fid;
141 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
143 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
144 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
145 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
149 void llu_update_inode(struct inode *inode, struct mdt_body *body,
150 struct lov_stripe_md *lsm)
152 struct llu_inode_info *lli = llu_i2info(inode);
153 struct intnl_stat *st = llu_i2stat(inode);
155 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
157 if (lli->lli_smd == NULL) {
159 lli->lli_maxbytes = lsm->lsm_maxbytes;
160 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
161 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
163 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
164 CERROR("lsm mismatch for inode %lld\n",
165 (long long)st->st_ino);
171 if (body->valid & OBD_MD_FLMTIME &&
172 body->mtime > LTIME_S(st->st_mtime))
173 LTIME_S(st->st_mtime) = body->mtime;
174 if (body->valid & OBD_MD_FLATIME &&
175 body->atime > LTIME_S(st->st_atime))
176 LTIME_S(st->st_atime) = body->atime;
178 /* mtime is always updated with ctime, but can be set in past.
179 As write and utime(2) may happen within 1 second, and utime's
180 mtime has a priority over write's one, so take mtime from mds
181 for the same ctimes. */
182 if (body->valid & OBD_MD_FLCTIME &&
183 body->ctime >= LTIME_S(st->st_ctime)) {
184 LTIME_S(st->st_ctime) = body->ctime;
185 if (body->valid & OBD_MD_FLMTIME)
186 LTIME_S(st->st_mtime) = body->mtime;
188 if (body->valid & OBD_MD_FLMODE)
189 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
190 if (body->valid & OBD_MD_FLTYPE)
191 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
192 if (S_ISREG(st->st_mode))
193 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
195 st->st_blksize = 4096;
196 if (body->valid & OBD_MD_FLUID)
197 st->st_uid = body->uid;
198 if (body->valid & OBD_MD_FLGID)
199 st->st_gid = body->gid;
200 if (body->valid & OBD_MD_FLNLINK)
201 st->st_nlink = body->nlink;
202 if (body->valid & OBD_MD_FLRDEV)
203 st->st_rdev = body->rdev;
204 if (body->valid & OBD_MD_FLFLAGS)
205 lli->lli_st_flags = body->flags;
206 if (body->valid & OBD_MD_FLSIZE) {
207 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
208 S_ISREG(st->st_mode) && lli->lli_smd) {
209 struct lustre_handle lockh;
212 /* As it is possible a blocking ast has been processed
213 * by this time, we need to check there is an UPDATE
214 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
216 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
219 st->st_size = body->size;
220 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
221 ldlm_lock_decref(&lockh, mode);
224 st->st_size = body->size;
227 if (body->valid & OBD_MD_FLBLOCKS)
228 st->st_blocks = body->blocks;
232 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
234 struct llu_inode_info *lli = llu_i2info(dst);
235 struct intnl_stat *st = llu_i2stat(dst);
237 valid &= src->o_valid;
239 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
240 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
243 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
244 (long)src->o_mtime, (long)src->o_ctime);
246 if (valid & OBD_MD_FLATIME)
247 LTIME_S(st->st_atime) = src->o_atime;
248 if (valid & OBD_MD_FLMTIME)
249 LTIME_S(st->st_mtime) = src->o_mtime;
250 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
251 LTIME_S(st->st_ctime) = src->o_ctime;
252 if (valid & OBD_MD_FLSIZE)
253 st->st_size = src->o_size;
254 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
255 st->st_blocks = src->o_blocks;
256 if (valid & OBD_MD_FLBLKSZ)
257 st->st_blksize = src->o_blksize;
258 if (valid & OBD_MD_FLTYPE)
259 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
260 if (valid & OBD_MD_FLMODE)
261 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
262 if (valid & OBD_MD_FLUID)
263 st->st_uid = src->o_uid;
264 if (valid & OBD_MD_FLGID)
265 st->st_gid = src->o_gid;
266 if (valid & OBD_MD_FLFLAGS)
267 lli->lli_st_flags = src->o_flags;
270 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
271 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
273 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
275 struct llu_inode_info *lli = llu_i2info(src);
276 struct intnl_stat *st = llu_i2stat(src);
277 obd_flag newvalid = 0;
279 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
280 CDEBUG(D_INODE, "valid %x, new time "CFS_TIME_T"/"CFS_TIME_T"\n",
281 valid, LTIME_S(st->st_mtime),
282 LTIME_S(st->st_ctime));
284 if (valid & OBD_MD_FLATIME) {
285 dst->o_atime = LTIME_S(st->st_atime);
286 newvalid |= OBD_MD_FLATIME;
288 if (valid & OBD_MD_FLMTIME) {
289 dst->o_mtime = LTIME_S(st->st_mtime);
290 newvalid |= OBD_MD_FLMTIME;
292 if (valid & OBD_MD_FLCTIME) {
293 dst->o_ctime = LTIME_S(st->st_ctime);
294 newvalid |= OBD_MD_FLCTIME;
296 if (valid & OBD_MD_FLSIZE) {
297 dst->o_size = st->st_size;
298 newvalid |= OBD_MD_FLSIZE;
300 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
301 dst->o_blocks = st->st_blocks;
302 newvalid |= OBD_MD_FLBLOCKS;
304 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
305 dst->o_blksize = st->st_blksize;
306 newvalid |= OBD_MD_FLBLKSZ;
308 if (valid & OBD_MD_FLTYPE) {
309 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
310 newvalid |= OBD_MD_FLTYPE;
312 if (valid & OBD_MD_FLMODE) {
313 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
314 newvalid |= OBD_MD_FLMODE;
316 if (valid & OBD_MD_FLUID) {
317 dst->o_uid = st->st_uid;
318 newvalid |= OBD_MD_FLUID;
320 if (valid & OBD_MD_FLGID) {
321 dst->o_gid = st->st_gid;
322 newvalid |= OBD_MD_FLGID;
324 if (valid & OBD_MD_FLFLAGS) {
325 dst->o_flags = lli->lli_st_flags;
326 newvalid |= OBD_MD_FLFLAGS;
328 if (valid & OBD_MD_FLGENER) {
329 dst->o_generation = lli->lli_st_generation;
330 newvalid |= OBD_MD_FLGENER;
332 if (valid & OBD_MD_FLFID) {
333 dst->o_fid = st->st_ino;
334 newvalid |= OBD_MD_FLFID;
337 dst->o_valid |= newvalid;
341 * really does the getattr on the inode and updates its fields
343 int llu_inode_getattr(struct inode *inode, struct obdo *obdo)
345 struct llu_inode_info *lli = llu_i2info(inode);
346 struct ptlrpc_request_set *set;
347 struct lov_stripe_md *lsm = lli->lli_smd;
348 struct obd_info oinfo = { { { 0 } } };
356 oinfo.oi_oa->o_id = lsm->lsm_object_id;
357 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
358 oinfo.oi_oa->o_mode = S_IFREG;
359 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
360 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
361 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
362 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
364 set = ptlrpc_prep_set();
366 CERROR ("ENOMEM allocing request set\n");
369 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
371 rc = ptlrpc_set_wait(set);
372 ptlrpc_set_destroy(set);
377 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
378 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
381 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
382 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
383 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
384 (long long unsigned)llu_i2stat(inode)->st_size,
385 (long long unsigned)llu_i2stat(inode)->st_blocks,
386 (long long unsigned)llu_i2stat(inode)->st_blksize);
390 static struct inode* llu_new_inode(struct filesys *fs,
394 struct llu_inode_info *lli;
395 struct intnl_stat st = {
398 #ifndef AUTOMOUNT_FILE_NAME
399 .st_mode = fid->f_type & S_IFMT,
401 .st_mode = fid->f_type /* all of the bits! */
404 /* FIXME: fix this later */
411 OBD_ALLOC(lli, sizeof(*lli));
415 /* initialize lli here */
416 lli->lli_sbi = llu_fs2sbi(fs);
418 lli->lli_symlink_name = NULL;
420 lli->lli_maxbytes = (__u64)(~0UL);
421 lli->lli_file_data = NULL;
423 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
424 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
427 /* file identifier is needed by functions like _sysio_i_find() */
428 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
429 &st, 0, &llu_inode_ops, lli);
432 OBD_FREE(lli, sizeof(*lli));
437 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
439 struct lustre_handle lockh;
440 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
447 fid = &llu_i2info(inode)->lli_fid;
448 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
450 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
451 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
452 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
458 static int llu_inode_revalidate(struct inode *inode)
460 struct lov_stripe_md *lsm = NULL;
464 CERROR("REPORT THIS LINE TO PETER\n");
468 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
470 struct ptlrpc_request *req = NULL;
471 struct llu_sb_info *sbi = llu_i2sbi(inode);
472 unsigned long valid = OBD_MD_FLGETATTR;
475 /* Why don't we update all valid MDS fields here, if we're
476 * doing an RPC anyways? -phil */
477 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
478 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
479 valid |= OBD_MD_FLEASIZE;
481 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
482 NULL, valid, ealen, &req);
484 CERROR("failure %d inode %llu\n", rc,
485 (long long)llu_i2stat(inode)->st_ino);
488 rc = md_get_lustre_md(sbi->ll_md_exp, req,
489 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
491 /* XXX Too paranoid? */
492 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
493 !((md.body->valid & OBD_MD_FLNLINK) &&
494 (md.body->nlink == 0))) {
495 CERROR("Asked for %s eadata but got %s (%d)\n",
496 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
497 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
498 md.body->eadatasize);
501 ptlrpc_req_finished(req);
506 llu_update_inode(inode, md.body, md.lsm);
507 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
508 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
509 ptlrpc_req_finished(req);
512 lsm = llu_i2info(inode)->lli_smd;
513 if (!lsm) /* object not yet allocated, don't validate size */
516 /* ll_glimpse_size will prefer locally cached writes if they extend
518 RETURN(llu_glimpse_size(inode));
521 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
523 *b = *llu_i2stat(ino);
526 static int llu_iop_getattr(struct pnode *pno,
528 struct intnl_stat *b)
533 liblustre_wait_event(0);
537 LASSERT(pno->p_base->pb_ino);
538 ino = pno->p_base->pb_ino;
540 LASSERT(!pno || pno->p_base->pb_ino == ino);
543 /* libsysio might call us directly without intent lock,
544 * we must re-fetch the attrs here
546 rc = llu_inode_revalidate(ino);
548 copy_stat_buf(ino, b);
549 LASSERT(!llu_i2info(ino)->lli_it);
552 liblustre_wait_event(0);
556 static int null_if_equal(struct ldlm_lock *lock, void *data)
558 if (data == lock->l_ast_data) {
559 lock->l_ast_data = NULL;
561 if (lock->l_req_mode != lock->l_granted_mode)
562 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
565 return LDLM_ITER_CONTINUE;
568 void llu_clear_inode(struct inode *inode)
570 struct llu_inode_info *lli = llu_i2info(inode);
571 struct llu_sb_info *sbi = llu_i2sbi(inode);
574 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
575 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
578 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
579 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
580 null_if_equal, inode);
583 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
584 null_if_equal, inode);
587 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
591 if (lli->lli_symlink_name) {
592 OBD_FREE(lli->lli_symlink_name,
593 strlen(lli->lli_symlink_name) + 1);
594 lli->lli_symlink_name = NULL;
600 void llu_iop_gone(struct inode *inode)
602 struct llu_inode_info *lli = llu_i2info(inode);
605 liblustre_wait_event(0);
606 llu_clear_inode(inode);
608 OBD_FREE(lli, sizeof(*lli));
612 static int inode_setattr(struct inode * inode, struct iattr * attr)
614 unsigned int ia_valid = attr->ia_valid;
615 struct intnl_stat *st = llu_i2stat(inode);
619 * inode_setattr() is only ever invoked with ATTR_SIZE (by
620 * llu_setattr_raw()) when file has no bodies. Check this.
622 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
624 if (ia_valid & ATTR_SIZE)
625 st->st_size = attr->ia_size;
626 if (ia_valid & ATTR_UID)
627 st->st_uid = attr->ia_uid;
628 if (ia_valid & ATTR_GID)
629 st->st_gid = attr->ia_gid;
630 if (ia_valid & ATTR_ATIME)
631 st->st_atime = attr->ia_atime;
632 if (ia_valid & ATTR_MTIME)
633 st->st_mtime = attr->ia_mtime;
634 if (ia_valid & ATTR_CTIME)
635 st->st_ctime = attr->ia_ctime;
636 if (ia_valid & ATTR_MODE) {
637 st->st_mode = attr->ia_mode;
638 if (!in_group_p(st->st_gid) && !cfs_capable(CFS_CAP_FSETID))
639 st->st_mode &= ~S_ISGID;
641 /* mark_inode_dirty(inode); */
645 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
646 struct md_open_data **mod)
649 struct llu_sb_info *sbi = llu_i2sbi(inode);
650 struct ptlrpc_request *request = NULL;
654 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
655 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
659 ptlrpc_req_finished(request);
660 if (rc != -EPERM && rc != -EACCES)
661 CERROR("md_setattr fails: rc = %d\n", rc);
665 rc = md_get_lustre_md(sbi->ll_md_exp, request,
666 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
668 ptlrpc_req_finished(request);
672 /* We call inode_setattr to adjust timestamps.
673 * If there is at least some data in file, we cleared ATTR_SIZE
674 * above to avoid invoking vmtruncate, otherwise it is important
675 * to call vmtruncate in inode_setattr to update inode->i_size
677 inode_setattr(inode, &op_data->op_attr);
678 llu_update_inode(inode, md.body, md.lsm);
679 ptlrpc_req_finished(request);
684 /* Close IO epoch and send Size-on-MDS attribute update. */
685 static int llu_setattr_done_writing(struct inode *inode,
686 struct md_op_data *op_data,
687 struct md_open_data *mod)
689 struct llu_inode_info *lli = llu_i2info(inode);
690 struct intnl_stat *st = llu_i2stat(inode);
694 LASSERT(op_data != NULL);
695 if (!S_ISREG(st->st_mode))
698 /* XXX: pass och here for the recovery purpose. */
699 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
700 op_data->op_ioepoch, PFID(&lli->lli_fid));
702 op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
703 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
705 /* MDS has instructed us to obtain Size-on-MDS attribute
706 * from OSTs and send setattr to back to MDS. */
707 rc = llu_sizeonmds_update(inode, mod, &op_data->op_handle,
708 op_data->op_ioepoch);
710 CERROR("inode %llu mdc truncate failed: rc = %d\n",
711 (unsigned long long)st->st_ino, rc);
716 /* If this inode has objects allocated to it (lsm != NULL), then the OST
717 * object(s) determine the file size and mtime. Otherwise, the MDS will
718 * keep these values until such a time that objects are allocated for it.
719 * We do the MDS operations first, as it is checking permissions for us.
720 * We don't to the MDS RPC if there is nothing that we want to store there,
721 * otherwise there is no harm in updating mtime/atime on the MDS if we are
722 * going to do an RPC anyways.
724 * If we are doing a truncate, we will send the mtime and ctime updates
725 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
726 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
729 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
731 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
732 struct llu_sb_info *sbi = llu_i2sbi(inode);
733 struct intnl_stat *st = llu_i2stat(inode);
734 int ia_valid = attr->ia_valid;
735 struct md_op_data op_data = { { 0 } };
736 struct md_open_data *mod = NULL;
740 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
742 if (ia_valid & ATTR_SIZE) {
743 if (attr->ia_size > ll_file_maxbytes(inode)) {
744 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
745 (long long)attr->ia_size,
746 ll_file_maxbytes(inode));
750 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
753 /* We mark all of the fields "set" so MDS/OST does not re-set them */
754 if (attr->ia_valid & ATTR_CTIME) {
755 attr->ia_ctime = CURRENT_TIME;
756 attr->ia_valid |= ATTR_CTIME_SET;
758 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
759 attr->ia_atime = CURRENT_TIME;
760 attr->ia_valid |= ATTR_ATIME_SET;
762 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
763 attr->ia_mtime = CURRENT_TIME;
764 attr->ia_valid |= ATTR_MTIME_SET;
766 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
767 /* To avoid stale mtime on mds, obtain it from ost and send
769 rc = llu_glimpse_size(inode);
773 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
774 attr->ia_mtime = inode->i_stbuf.st_mtime;
777 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
778 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
779 ", now = "CFS_TIME_T"\n",
780 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
781 LTIME_S(CURRENT_TIME));
783 /* NB: ATTR_SIZE will only be set after this point if the size
784 * resides on the MDS, ie, this file has no objects. */
786 attr->ia_valid &= ~ATTR_SIZE;
788 /* If only OST attributes being set on objects, don't do MDS RPC.
789 * In that case, we need to check permissions and update the local
790 * inode ourselves so we can call obdo_from_inode() always. */
791 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
792 memcpy(&op_data.op_attr, attr, sizeof(*attr));
794 /* Open epoch for truncate. */
795 if (ia_valid & ATTR_SIZE)
796 op_data.op_flags = MF_EPOCH_OPEN;
797 rc = llu_md_setattr(inode, &op_data, &mod);
801 if (op_data.op_ioepoch)
802 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for "
803 "truncate\n", op_data.op_ioepoch,
804 PFID(&llu_i2info(inode)->lli_fid));
806 if (!lsm || !S_ISREG(st->st_mode)) {
807 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
811 /* The OST doesn't check permissions, but the alternative is
812 * a gratuitous RPC to the MDS. We already rely on the client
813 * to do read/write/truncate permission checks, so is mtime OK?
815 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
816 /* from sys_utime() */
817 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
818 if (current->fsuid != st->st_uid &&
819 (rc = ll_permission(inode, MAY_WRITE)) != 0)
822 /* from inode_change_ok() */
823 if (current->fsuid != st->st_uid &&
824 !cfs_capable(CFS_CAP_FOWNER))
830 /* Won't invoke llu_vmtruncate(), as we already cleared
832 inode_setattr(inode, attr);
835 if (ia_valid & ATTR_SIZE) {
836 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
838 struct lustre_handle lockh = { 0, };
839 struct lustre_handle match_lockh = { 0, };
842 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
846 /* check that there are no matching locks */
847 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
848 LCK_PW, &flags, inode, &match_lockh) <= 0);
850 /* XXX when we fix the AST intents to pass the discard-range
851 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
853 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
855 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
857 obd_flags = OBD_FL_TRUNCLOCK;
858 CDEBUG(D_INODE, "delegating locking to the OST");
864 /* with lock_mode == LK_NL no lock is taken. */
865 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
867 if (rc != ELDLM_OK) {
869 GOTO(out, rc = -ENOLCK);
872 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
874 /* unlock now as we don't mind others file lockers racing with
875 * the mds updates below? */
876 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
878 CERROR("llu_extent_unlock failed: %d\n", err);
882 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
883 struct obd_info oinfo = { { { 0 } } };
886 CDEBUG(D_INODE, "set mtime on OST inode %llu to "CFS_TIME_T"\n",
887 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
888 oa.o_id = lsm->lsm_object_id;
889 oa.o_gr = lsm->lsm_object_gr;
890 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
892 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
893 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
898 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
900 CERROR("obd_setattr_async fails: rc=%d\n", rc);
904 if (op_data.op_ioepoch)
905 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
906 return rc ? rc : rc1;
909 /* here we simply act as a thin layer to glue it with
910 * llu_setattr_raw(), which is copy from kernel
912 static int llu_iop_setattr(struct pnode *pno,
915 struct intnl_stat *stbuf)
921 liblustre_wait_event(0);
923 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
924 SETATTR_UID | SETATTR_GID |
925 SETATTR_LEN | SETATTR_MODE)));
926 memset(&iattr, 0, sizeof(iattr));
928 if (mask & SETATTR_MODE) {
929 iattr.ia_mode = stbuf->st_mode;
930 iattr.ia_valid |= ATTR_MODE;
932 if (mask & SETATTR_MTIME) {
933 iattr.ia_mtime = stbuf->st_mtime;
934 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
936 if (mask & SETATTR_ATIME) {
937 iattr.ia_atime = stbuf->st_atime;
938 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
940 if (mask & SETATTR_UID) {
941 iattr.ia_uid = stbuf->st_uid;
942 iattr.ia_valid |= ATTR_UID;
944 if (mask & SETATTR_GID) {
945 iattr.ia_gid = stbuf->st_gid;
946 iattr.ia_valid |= ATTR_GID;
948 if (mask & SETATTR_LEN) {
949 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
950 iattr.ia_valid |= ATTR_SIZE;
953 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
954 iattr.ia_ctime = CURRENT_TIME;
956 rc = llu_setattr_raw(ino, &iattr);
957 liblustre_wait_idle();
961 #define EXT2_LINK_MAX 32000
963 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
965 struct inode *dir = pno->p_base->pb_parent->pb_ino;
966 struct qstr *qstr = &pno->p_base->pb_name;
967 const char *name = qstr->name;
969 struct ptlrpc_request *request = NULL;
970 struct llu_sb_info *sbi = llu_i2sbi(dir);
971 struct md_op_data op_data = {{ 0 }};
975 liblustre_wait_event(0);
976 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
979 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
982 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
983 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
984 cfs_curproc_cap_pack(), 0, &request);
985 ptlrpc_req_finished(request);
986 liblustre_wait_event(0);
990 static int llu_readlink_internal(struct inode *inode,
991 struct ptlrpc_request **request,
994 struct llu_inode_info *lli = llu_i2info(inode);
995 struct llu_sb_info *sbi = llu_i2sbi(inode);
996 struct mdt_body *body;
997 struct intnl_stat *st = llu_i2stat(inode);
998 int rc, symlen = st->st_size + 1;
1004 if (lli->lli_symlink_name) {
1005 *symname = lli->lli_symlink_name;
1006 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
1010 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
1011 OBD_MD_LINKNAME, symlen, request);
1013 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
1017 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
1018 LASSERT(body != NULL);
1020 if ((body->valid & OBD_MD_LINKNAME) == 0) {
1021 CERROR ("OBD_MD_LINKNAME not set on reply\n");
1022 GOTO (failed, rc = -EPROTO);
1025 LASSERT(symlen != 0);
1026 if (body->eadatasize != symlen) {
1027 CERROR("inode %llu: symlink length %d not expected %d\n",
1028 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1029 GOTO(failed, rc = -EPROTO);
1032 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
1033 if (*symname == NULL ||
1034 strnlen(*symname, symlen) != symlen - 1) {
1035 /* not full/NULL terminated */
1036 CERROR("inode %llu: symlink not NULL terminated string"
1037 "of length %d\n", (long long)st->st_ino, symlen - 1);
1038 GOTO(failed, rc = -EPROTO);
1041 OBD_ALLOC(lli->lli_symlink_name, symlen);
1042 /* do not return an error if we cannot cache the symlink locally */
1043 if (lli->lli_symlink_name)
1044 memcpy(lli->lli_symlink_name, *symname, symlen);
1049 ptlrpc_req_finished (*request);
1053 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1055 struct inode *inode = pno->p_base->pb_ino;
1056 struct ptlrpc_request *request;
1061 liblustre_wait_event(0);
1062 rc = llu_readlink_internal(inode, &request, &symname);
1067 strncpy(data, symname, bufsize);
1068 rc = strlen(symname);
1070 ptlrpc_req_finished(request);
1072 liblustre_wait_event(0);
1076 static int llu_iop_mknod_raw(struct pnode *pno,
1080 struct ptlrpc_request *request = NULL;
1081 struct inode *dir = pno->p_parent->p_base->pb_ino;
1082 struct llu_sb_info *sbi = llu_i2sbi(dir);
1083 struct md_op_data op_data = {{ 0 }};
1087 liblustre_wait_event(0);
1088 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1089 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1090 (long long)llu_i2stat(dir)->st_ino);
1092 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1095 switch (mode & S_IFMT) {
1098 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1103 llu_prep_md_op_data(&op_data, dir, NULL,
1104 pno->p_base->pb_name.name,
1105 pno->p_base->pb_name.len, 0,
1108 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1109 current->fsuid, current->fsgid,
1110 cfs_curproc_cap_pack(), dev, &request);
1111 ptlrpc_req_finished(request);
1119 liblustre_wait_event(0);
1123 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1125 struct inode *src = old->p_base->pb_ino;
1126 struct inode *dir = new->p_parent->p_base->pb_ino;
1127 const char *name = new->p_base->pb_name.name;
1128 int namelen = new->p_base->pb_name.len;
1129 struct ptlrpc_request *request = NULL;
1130 struct md_op_data op_data = {{ 0 }};
1137 liblustre_wait_event(0);
1138 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1140 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1141 ptlrpc_req_finished(request);
1142 liblustre_wait_event(0);
1148 * libsysio will clear the inode immediately after return
1150 static int llu_iop_unlink_raw(struct pnode *pno)
1152 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1153 struct qstr *qstr = &pno->p_base->pb_name;
1154 const char *name = qstr->name;
1155 int len = qstr->len;
1156 struct inode *target = pno->p_base->pb_ino;
1157 struct ptlrpc_request *request = NULL;
1158 struct md_op_data op_data = { { 0 } };
1164 liblustre_wait_event(0);
1165 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1167 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1169 rc = llu_objects_destroy(request, dir);
1170 ptlrpc_req_finished(request);
1171 liblustre_wait_idle();
1176 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1178 struct inode *src = old->p_parent->p_base->pb_ino;
1179 struct inode *tgt = new->p_parent->p_base->pb_ino;
1180 const char *oldname = old->p_base->pb_name.name;
1181 int oldnamelen = old->p_base->pb_name.len;
1182 const char *newname = new->p_base->pb_name.name;
1183 int newnamelen = new->p_base->pb_name.len;
1184 struct ptlrpc_request *request = NULL;
1185 struct md_op_data op_data = { { 0 } };
1192 liblustre_wait_event(0);
1193 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1195 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1196 oldname, oldnamelen, newname, newnamelen,
1199 rc = llu_objects_destroy(request, src);
1202 ptlrpc_req_finished(request);
1203 liblustre_wait_idle();
1208 #ifdef _HAVE_STATVFS
1209 static int llu_statfs_internal(struct llu_sb_info *sbi,
1210 struct obd_statfs *osfs, __u64 max_age)
1212 struct obd_statfs obd_osfs;
1216 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1218 CERROR("md_statfs fails: rc = %d\n", rc);
1222 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1223 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1225 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1226 &obd_statfs, max_age, 0);
1228 CERROR("obd_statfs fails: rc = %d\n", rc);
1232 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1233 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1236 osfs->os_blocks = obd_osfs.os_blocks;
1237 osfs->os_bfree = obd_osfs.os_bfree;
1238 osfs->os_bavail = obd_osfs.os_bavail;
1240 /* If we don't have as many objects free on the OST as inodes
1241 * on the MDS, we reduce the total number of inodes to
1242 * compensate, so that the "inodes in use" number is correct.
1244 if (obd_osfs.os_ffree < osfs->os_ffree) {
1245 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1247 osfs->os_ffree = obd_osfs.os_ffree;
1253 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1255 struct obd_statfs osfs;
1258 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1260 /* For now we will always get up-to-date statfs values, but in the
1261 * future we may allow some amount of caching on the client (e.g.
1262 * from QOS or lprocfs updates). */
1263 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1267 statfs_unpack(sfs, &osfs);
1269 if (sizeof(sfs->f_blocks) == 4) {
1270 while (osfs.os_blocks > ~0UL) {
1273 osfs.os_blocks >>= 1;
1274 osfs.os_bfree >>= 1;
1275 osfs.os_bavail >>= 1;
1279 sfs->f_blocks = osfs.os_blocks;
1280 sfs->f_bfree = osfs.os_bfree;
1281 sfs->f_bavail = osfs.os_bavail;
1286 static int llu_iop_statvfs(struct pnode *pno,
1288 struct intnl_statvfs *buf)
1294 liblustre_wait_event(0);
1297 LASSERT(pno->p_base->pb_ino);
1298 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1302 /* from native driver */
1303 buf->f_bsize = fs.f_bsize; /* file system block size */
1304 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1305 buf->f_blocks = fs.f_blocks;
1306 buf->f_bfree = fs.f_bfree;
1307 buf->f_bavail = fs.f_bavail;
1308 buf->f_files = fs.f_files; /* Total number serial numbers */
1309 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1310 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1311 buf->f_fsid = fs.f_fsid.__val[1];
1312 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1313 buf->f_namemax = fs.f_namelen;
1316 liblustre_wait_event(0);
1319 #endif /* _HAVE_STATVFS */
1321 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1323 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1324 struct qstr *qstr = &pno->p_base->pb_name;
1325 const char *name = qstr->name;
1326 int len = qstr->len;
1327 struct ptlrpc_request *request = NULL;
1328 struct intnl_stat *st = llu_i2stat(dir);
1329 struct md_op_data op_data = {{ 0 }};
1333 liblustre_wait_event(0);
1334 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1335 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1337 if (st->st_nlink >= EXT2_LINK_MAX)
1340 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1343 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1344 mode | S_IFDIR, current->fsuid, current->fsgid,
1345 cfs_curproc_cap_pack(), 0, &request);
1346 ptlrpc_req_finished(request);
1347 liblustre_wait_event(0);
1351 static int llu_iop_rmdir_raw(struct pnode *pno)
1353 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1354 struct qstr *qstr = &pno->p_base->pb_name;
1355 const char *name = qstr->name;
1356 int len = qstr->len;
1357 struct ptlrpc_request *request = NULL;
1358 struct md_op_data op_data = {{ 0 }};
1362 liblustre_wait_event(0);
1363 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1364 (long long)llu_i2stat(dir)->st_ino,
1365 llu_i2info(dir)->lli_st_generation, dir);
1367 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1369 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1370 ptlrpc_req_finished(request);
1372 liblustre_wait_event(0);
1377 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1379 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1381 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1383 /* refer to ll_file_flock() for details */
1384 static int llu_file_flock(struct inode *ino,
1386 struct file_lock *file_lock)
1388 struct llu_inode_info *lli = llu_i2info(ino);
1389 struct intnl_stat *st = llu_i2stat(ino);
1390 struct ldlm_res_id res_id =
1391 { .name = {fid_seq(&lli->lli_fid),
1392 fid_oid(&lli->lli_fid),
1393 fid_ver(&lli->lli_fid),
1395 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1396 ldlm_flock_completion_ast, NULL, file_lock };
1398 struct lustre_handle lockh = {0};
1399 ldlm_policy_data_t flock;
1403 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1404 (unsigned long long)st->st_ino, file_lock);
1406 flock.l_flock.pid = file_lock->fl_pid;
1407 flock.l_flock.start = file_lock->fl_start;
1408 flock.l_flock.end = file_lock->fl_end;
1410 switch (file_lock->fl_type) {
1412 einfo.ei_mode = LCK_PR;
1415 einfo.ei_mode = LCK_NL;
1418 einfo.ei_mode = LCK_PW;
1421 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1428 #if F_SETLKW64 != F_SETLKW
1436 #if F_SETLK64 != F_SETLK
1440 flags = LDLM_FL_BLOCK_NOWAIT;
1444 #if F_GETLK64 != F_GETLK
1448 flags = LDLM_FL_TEST_LOCK;
1449 file_lock->fl_type = einfo.ei_mode;
1452 CERROR("unknown fcntl cmd: %d\n", cmd);
1456 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1457 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1458 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1462 struct lmv_obd *lmv;
1463 struct obd_device *lmv_obd;
1464 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1465 lmv = &lmv_obd->u.lmv;
1467 if (lmv->desc.ld_tgt_count < 1)
1468 RETURN(rc = -ENODEV);
1470 if (lmv->tgts[0].ltd_exp != NULL)
1471 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1472 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1479 static int assign_type(struct file_lock *fl, int type)
1492 static int flock_to_posix_lock(struct inode *ino,
1493 struct file_lock *fl,
1496 switch (l->l_whence) {
1497 /* XXX: only SEEK_SET is supported in lustre */
1505 fl->fl_end = l->l_len - 1;
1509 fl->fl_end = OFFSET_MAX;
1511 fl->fl_pid = getpid();
1512 fl->fl_flags = FL_POSIX;
1513 fl->fl_notify = NULL;
1514 fl->fl_insert = NULL;
1515 fl->fl_remove = NULL;
1516 /* XXX: these fields can't be filled with suitable values,
1517 but I think lustre doesn't use them.
1519 fl->fl_owner = NULL;
1522 return assign_type(fl, l->l_type);
1525 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1527 struct file_lock fl;
1531 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1534 error = flock_to_posix_lock(ino, &fl, flock);
1538 error = llu_file_flock(ino, F_GETLK, &fl);
1542 flock->l_type = F_UNLCK;
1543 if (fl.fl_type != F_UNLCK) {
1544 flock->l_pid = fl.fl_pid;
1545 flock->l_start = fl.fl_start;
1546 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1547 fl.fl_end - fl.fl_start + 1;
1548 flock->l_whence = SEEK_SET;
1549 flock->l_type = fl.fl_type;
1556 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1558 struct file_lock fl;
1559 int flags = llu_i2info(ino)->lli_open_flags + 1;
1562 error = flock_to_posix_lock(ino, &fl, flock);
1565 if (cmd == F_SETLKW)
1566 fl.fl_flags |= FL_SLEEP;
1569 switch (flock->l_type) {
1571 if (!(flags & FMODE_READ))
1575 if (!(flags & FMODE_WRITE))
1585 error = llu_file_flock(ino, cmd, &fl);
1593 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1595 struct llu_inode_info *lli = llu_i2info(ino);
1597 struct flock *flock;
1600 liblustre_wait_event(0);
1603 *rtn = lli->lli_open_flags;
1606 flags = va_arg(ap, long);
1607 flags &= FCNTL_FLMASK;
1608 if (flags & FCNTL_FLMASK_INVALID) {
1609 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1610 "the O_NONBLOCK or O_ASYNC flags. "
1611 "Please fix your application.\n");
1616 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1617 (lli->lli_open_flags & ~FCNTL_FLMASK);
1622 #if F_GETLK64 != F_GETLK
1626 flock = va_arg(ap, struct flock *);
1627 err = llu_fcntl_getlk(ino, flock);
1632 #if F_SETLKW64 != F_SETLKW
1638 #if F_SETLK64 != F_SETLK
1642 flock = va_arg(ap, struct flock *);
1643 err = llu_fcntl_setlk(ino, cmd, flock);
1647 CERROR("unsupported fcntl cmd %x\n", cmd);
1653 liblustre_wait_event(0);
1657 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1659 struct llu_inode_info *lli = llu_i2info(inode);
1660 struct ll_file_data *fd = lli->lli_file_data;
1661 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1662 .end = OBD_OBJECT_EOF}};
1663 struct lustre_handle lockh = { 0 };
1664 struct lov_stripe_md *lsm = lli->lli_smd;
1669 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1673 policy.l_extent.gid = arg;
1674 if (lli->lli_open_flags & O_NONBLOCK)
1675 flags = LDLM_FL_BLOCK_NOWAIT;
1677 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1682 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1684 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1689 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1691 struct llu_inode_info *lli = llu_i2info(inode);
1692 struct ll_file_data *fd = lli->lli_file_data;
1693 struct lov_stripe_md *lsm = lli->lli_smd;
1697 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1700 if (fd->fd_gid != arg)
1703 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1705 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1710 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1715 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1717 struct llu_sb_info *sbi = llu_i2sbi(ino);
1718 struct ptlrpc_request *request = NULL;
1719 struct md_op_data op_data = {{ 0 }};
1720 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1723 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1726 LASSERT(sizeof(lum) == sizeof(*lump));
1727 LASSERT(sizeof(lum.lmm_objects[0]) ==
1728 sizeof(lump->lmm_objects[0]));
1729 rc = copy_from_user(&lum, lump, sizeof(lum));
1733 switch (lum.lmm_magic) {
1734 case LOV_USER_MAGIC_V1: {
1735 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1736 lustre_swab_lov_user_md_v1(&lum);
1739 case LOV_USER_MAGIC_V3: {
1740 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1741 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1745 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1746 " %#08x != %#08x nor %#08x\n",
1747 lum.lmm_magic, LOV_USER_MAGIC_V1,
1753 /* swabbing is done in lov_setstripe() on server side */
1754 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1755 sizeof(lum), NULL, 0, &request, NULL);
1757 ptlrpc_req_finished(request);
1758 if (rc != -EPERM && rc != -EACCES)
1759 CERROR("md_setattr fails: rc = %d\n", rc);
1762 ptlrpc_req_finished(request);
1767 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1768 struct lov_user_md *lum, int lum_size)
1770 struct llu_sb_info *sbi = llu_i2sbi(ino);
1771 struct llu_inode_info *lli = llu_i2info(ino);
1772 struct llu_inode_info *lli2 = NULL;
1773 struct lov_stripe_md *lsm;
1774 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1775 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1776 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1778 struct ptlrpc_request *req = NULL;
1779 struct lustre_md md;
1780 struct md_op_data data = {{ 0 }};
1781 struct lustre_handle lockh;
1787 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1788 PFID(&lli->lli_fid));
1792 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1796 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1797 lli2->lli_open_count = 0;
1798 lli2->lli_it = NULL;
1799 lli2->lli_file_data = NULL;
1800 lli2->lli_smd = NULL;
1801 lli2->lli_symlink_name = NULL;
1802 ino->i_private = lli2;
1804 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1807 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1808 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1812 req = oit.d.lustre.it_data;
1813 rc = it_open_error(DISP_IT_EXECD, &oit);
1819 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1825 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1826 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1830 llu_update_inode(ino, md.body, md.lsm);
1831 lli->lli_smd = lli2->lli_smd;
1832 lli2->lli_smd = NULL;
1834 llu_local_open(lli2, &oit);
1836 /* release intent */
1837 if (lustre_handle_is_used(&lockh))
1838 ldlm_lock_decref(&lockh, LCK_CR);
1840 ptlrpc_req_finished(req);
1843 rc = llu_file_release(ino);
1845 ino->i_private = lli;
1847 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1849 ptlrpc_req_finished(req);
1853 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1855 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1857 int flags = FMODE_WRITE;
1860 LASSERT(sizeof(lum) == sizeof(*lump));
1861 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1862 rc = copy_from_user(&lum, lump, sizeof(lum));
1866 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1870 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1872 struct intnl_stat *st = llu_i2stat(ino);
1873 if (S_ISREG(st->st_mode))
1874 return llu_lov_file_setstripe(ino, arg);
1875 if (S_ISDIR(st->st_mode))
1876 return llu_lov_dir_setstripe(ino, arg);
1881 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1883 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1888 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1892 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1898 liblustre_wait_event(0);
1901 case LL_IOC_GROUP_LOCK:
1902 arg = va_arg(ap, unsigned long);
1903 rc = llu_get_grouplock(ino, arg);
1905 case LL_IOC_GROUP_UNLOCK:
1906 arg = va_arg(ap, unsigned long);
1907 rc = llu_put_grouplock(ino, arg);
1909 case LL_IOC_LOV_SETSTRIPE:
1910 arg = va_arg(ap, unsigned long);
1911 rc = llu_lov_setstripe(ino, arg);
1913 case LL_IOC_LOV_GETSTRIPE:
1914 arg = va_arg(ap, unsigned long);
1915 rc = llu_lov_getstripe(ino, arg);
1918 CERROR("did not support ioctl cmd %lx\n", request);
1923 liblustre_wait_event(0);
1928 * we already do syncronous read/write
1930 static int llu_iop_sync(struct inode *inode)
1932 liblustre_wait_event(0);
1936 static int llu_iop_datasync(struct inode *inode)
1938 liblustre_wait_event(0);
1942 struct filesys_ops llu_filesys_ops =
1944 fsop_gone: llu_fsop_gone,
1947 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1949 struct inode *inode;
1951 struct file_identifier fileid = {&fid, sizeof(fid)};
1953 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1954 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1955 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1957 return ERR_PTR(-EPERM);
1960 /* try to find existing inode */
1961 fid = md->body->fid1;
1963 inode = _sysio_i_find(fs, &fileid);
1965 if (inode->i_zombie/* ||
1966 lli->lli_st_generation != md->body->generation*/) {
1970 llu_update_inode(inode, md->body, md->lsm);
1975 inode = llu_new_inode(fs, &fid);
1977 llu_update_inode(inode, md->body, md->lsm);
1983 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1985 /* even if default lov is LOV_MAGIC_V1 we use LOV_MAGIC_V3
1986 * to be sure buffer are large enough */
1987 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 };
1988 __u32 valsize = sizeof(struct lov_desc);
1989 int rc, easize, def_easize, cookiesize;
1990 struct lov_desc desc;
1994 rc = obd_get_info(dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
1995 &valsize, &desc, NULL);
1999 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
2000 lsm.lsm_stripe_count = stripes;
2001 easize = obd_size_diskmd(dt_exp, &lsm);
2003 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
2004 def_easize = obd_size_diskmd(dt_exp, &lsm);
2006 cookiesize = stripes * sizeof(struct llog_cookie);
2008 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
2009 easize, cookiesize);
2011 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
2016 llu_fsswop_mount(const char *source,
2018 const void *data __IS_UNUSED,
2019 struct pnode *tocover,
2020 struct mount **mntp)
2024 struct pnode_base *rootpb;
2025 struct obd_device *obd;
2026 struct lu_fid rootfid;
2027 struct llu_sb_info *sbi;
2028 struct obd_statfs osfs;
2029 static struct qstr noname = { NULL, 0, 0 };
2030 struct ptlrpc_request *request = NULL;
2031 struct lustre_handle md_conn = {0, };
2032 struct lustre_handle dt_conn = {0, };
2033 struct lustre_md md;
2035 struct config_llog_instance cfg = {0, };
2036 char ll_instance[sizeof(sbi) * 2 + 1];
2037 struct lustre_profile *lprof;
2038 char *zconf_mgsnid, *zconf_profile;
2039 char *osc = NULL, *mdc = NULL;
2040 int async = 1, err = -EINVAL;
2041 struct obd_connect_data ocd = {0,};
2045 if (ll_parse_mount_target(source,
2048 CERROR("mal-formed target %s\n", source);
2051 if (!zconf_mgsnid || !zconf_profile) {
2052 printf("Liblustre: invalid target %s\n", source);
2055 /* allocate & initialize sbi */
2056 OBD_ALLOC(sbi, sizeof(*sbi));
2060 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
2061 ll_generate_random_uuid(uuid);
2062 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2064 /* generate a string unique to this super, let's try
2065 the address of the super itself.*/
2066 sprintf(ll_instance, "%p", sbi);
2068 /* retrive & parse config log */
2069 cfg.cfg_instance = ll_instance;
2070 cfg.cfg_uuid = sbi->ll_sb_uuid;
2071 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2073 CERROR("Unable to process log: %s\n", zconf_profile);
2074 GOTO(out_free, err);
2077 lprof = class_get_profile(zconf_profile);
2078 if (lprof == NULL) {
2079 CERROR("No profile found: %s\n", zconf_profile);
2080 GOTO(out_free, err = -EINVAL);
2082 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2083 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2085 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2086 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2090 GOTO(out_free, err = -EINVAL);
2094 GOTO(out_free, err = -EINVAL);
2097 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2103 obd = class_name2obd(mdc);
2105 CERROR("MDC %s: not setup or attached\n", mdc);
2106 GOTO(out_free, err = -EINVAL);
2108 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2109 sizeof(async), &async, NULL);
2111 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2112 OBD_CONNECT_FID | OBD_CONNECT_AT;
2113 #ifdef LIBLUSTRE_POSIX_ACL
2114 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2116 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2117 ocd.ocd_version = LUSTRE_VERSION_CODE;
2120 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2122 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2123 GOTO(out_free, err);
2125 sbi->ll_md_exp = class_conn2export(&md_conn);
2127 err = obd_statfs(obd, &osfs, 100000000, 0);
2132 * FIXME fill fs stat data into sbi here!!! FIXME
2136 obd = class_name2obd(osc);
2138 CERROR("OSC %s: not setup or attached\n", osc);
2139 GOTO(out_md, err = -EINVAL);
2141 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2142 sizeof(async), &async, NULL);
2144 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2145 obd->obd_upcall.onu_upcall = ll_ocd_update;
2147 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2148 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2149 OBD_CONNECT_FID | OBD_CONNECT_AT;
2150 ocd.ocd_version = LUSTRE_VERSION_CODE;
2151 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2153 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2156 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2157 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2159 err = obd_register_lock_cancel_cb(sbi->ll_dt_exp,
2160 llu_extent_lock_cancel_cb);
2162 CERROR("cannot register lock cancel callback: rc = %d\n", err);
2166 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2168 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2170 CERROR("cannot mds_connect: rc = %d\n", err);
2171 GOTO(out_lock_cn_cb, err);
2173 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2174 sbi->ll_root_fid = rootfid;
2176 /* fetch attr of root inode */
2177 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2178 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2180 CERROR("md_getattr failed for root: rc = %d\n", err);
2181 GOTO(out_lock_cn_cb, err);
2184 err = md_get_lustre_md(sbi->ll_md_exp, request,
2185 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2187 CERROR("failed to understand root inode md: rc = %d\n",err);
2188 GOTO(out_request, err);
2191 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2193 root = llu_iget(fs, &md);
2194 if (!root || IS_ERR(root)) {
2195 CERROR("fail to generate root inode\n");
2196 GOTO(out_request, err = -EBADF);
2200 * Generate base path-node for root.
2202 rootpb = _sysio_pb_new(&noname, NULL, root);
2208 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2210 _sysio_pb_gone(rootpb);
2214 ptlrpc_req_finished(request);
2216 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2217 liblustre_wait_idle();
2222 _sysio_i_gone(root);
2224 ptlrpc_req_finished(request);
2226 obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
2227 llu_extent_lock_cancel_cb);
2229 obd_disconnect(sbi->ll_dt_exp);
2231 obd_disconnect(sbi->ll_md_exp);
2234 OBD_FREE(osc, strlen(osc) + 1);
2236 OBD_FREE(mdc, strlen(mdc) + 1);
2237 OBD_FREE(sbi, sizeof(*sbi));
2238 liblustre_wait_idle();
2242 struct fssw_ops llu_fssw_ops = {
2246 static struct inode_ops llu_inode_ops = {
2247 inop_lookup: llu_iop_lookup,
2248 inop_getattr: llu_iop_getattr,
2249 inop_setattr: llu_iop_setattr,
2250 inop_filldirentries: llu_iop_filldirentries,
2251 inop_mkdir: llu_iop_mkdir_raw,
2252 inop_rmdir: llu_iop_rmdir_raw,
2253 inop_symlink: llu_iop_symlink_raw,
2254 inop_readlink: llu_iop_readlink,
2255 inop_open: llu_iop_open,
2256 inop_close: llu_iop_close,
2257 inop_link: llu_iop_link_raw,
2258 inop_unlink: llu_iop_unlink_raw,
2259 inop_rename: llu_iop_rename_raw,
2260 inop_pos: llu_iop_pos,
2261 inop_read: llu_iop_read,
2262 inop_write: llu_iop_write,
2263 inop_iodone: llu_iop_iodone,
2264 inop_fcntl: llu_iop_fcntl,
2265 inop_sync: llu_iop_sync,
2266 inop_datasync: llu_iop_datasync,
2267 inop_ioctl: llu_iop_ioctl,
2268 inop_mknod: llu_iop_mknod_raw,
2269 #ifdef _HAVE_STATVFS
2270 inop_statvfs: llu_iop_statvfs,
2272 inop_gone: llu_iop_gone,