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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #ifdef HAVE_UIDGID_HEADER
46 # include <linux/uidgid.h>
48 #include <lustre/ll_fiemap.h>
50 #include <lustre_ioctl.h>
51 #include <lustre_swab.h>
53 #include "cl_object.h"
54 #include "llite_internal.h"
55 #include "vvp_internal.h"
58 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
60 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
63 static enum llioc_iter
64 ll_iocontrol_call(struct inode *inode, struct file *file,
65 unsigned int cmd, unsigned long arg, int *rcp);
67 static struct ll_file_data *ll_file_data_get(void)
69 struct ll_file_data *fd;
71 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
75 fd->fd_write_failed = false;
80 static void ll_file_data_put(struct ll_file_data *fd)
83 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
87 * Packs all the attributes into @op_data for the CLOSE rpc.
89 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
90 struct obd_client_handle *och)
94 ll_prep_md_op_data(op_data, inode, NULL, NULL,
95 0, 0, LUSTRE_OPC_ANY, NULL);
97 op_data->op_attr.ia_mode = inode->i_mode;
98 op_data->op_attr.ia_atime = inode->i_atime;
99 op_data->op_attr.ia_mtime = inode->i_mtime;
100 op_data->op_attr.ia_ctime = inode->i_ctime;
101 op_data->op_attr.ia_size = i_size_read(inode);
102 op_data->op_attr.ia_valid |= ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
103 ATTR_MTIME | ATTR_MTIME_SET |
104 ATTR_CTIME | ATTR_CTIME_SET;
105 op_data->op_attr_blocks = inode->i_blocks;
106 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
107 op_data->op_handle = och->och_fh;
109 if (och->och_flags & FMODE_WRITE &&
110 ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
111 /* For HSM: if inode data has been modified, pack it so that
112 * MDT can set data dirty flag in the archive. */
113 op_data->op_bias |= MDS_DATA_MODIFIED;
119 * Perform a close, possibly with a bias.
120 * The meaning of "data" depends on the value of "bias".
122 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
123 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
126 static int ll_close_inode_openhandle(struct inode *inode,
127 struct obd_client_handle *och,
128 enum mds_op_bias bias, void *data)
130 struct obd_export *md_exp = ll_i2mdexp(inode);
131 const struct ll_inode_info *lli = ll_i2info(inode);
132 struct md_op_data *op_data;
133 struct ptlrpc_request *req = NULL;
137 if (class_exp2obd(md_exp) == NULL) {
138 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
139 ll_get_fsname(inode->i_sb, NULL, 0),
140 PFID(&lli->lli_fid));
144 OBD_ALLOC_PTR(op_data);
145 /* We leak openhandle and request here on error, but not much to be
146 * done in OOM case since app won't retry close on error either. */
148 GOTO(out, rc = -ENOMEM);
150 ll_prepare_close(inode, op_data, och);
152 case MDS_CLOSE_LAYOUT_SWAP:
153 LASSERT(data != NULL);
154 op_data->op_bias |= MDS_CLOSE_LAYOUT_SWAP;
155 op_data->op_data_version = 0;
156 op_data->op_lease_handle = och->och_lease_handle;
157 op_data->op_fid2 = *ll_inode2fid(data);
160 case MDS_HSM_RELEASE:
161 LASSERT(data != NULL);
162 op_data->op_bias |= MDS_HSM_RELEASE;
163 op_data->op_data_version = *(__u64 *)data;
164 op_data->op_lease_handle = och->och_lease_handle;
165 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
169 LASSERT(data == NULL);
173 rc = md_close(md_exp, op_data, och->och_mod, &req);
174 if (rc != 0 && rc != -EINTR)
175 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
176 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
179 op_data->op_bias & (MDS_HSM_RELEASE | MDS_CLOSE_LAYOUT_SWAP)) {
180 struct mdt_body *body;
182 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
183 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
187 ll_finish_md_op_data(op_data);
191 md_clear_open_replay_data(md_exp, och);
192 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
195 ptlrpc_req_finished(req); /* This is close request */
199 int ll_md_real_close(struct inode *inode, fmode_t fmode)
201 struct ll_inode_info *lli = ll_i2info(inode);
202 struct obd_client_handle **och_p;
203 struct obd_client_handle *och;
208 if (fmode & FMODE_WRITE) {
209 och_p = &lli->lli_mds_write_och;
210 och_usecount = &lli->lli_open_fd_write_count;
211 } else if (fmode & FMODE_EXEC) {
212 och_p = &lli->lli_mds_exec_och;
213 och_usecount = &lli->lli_open_fd_exec_count;
215 LASSERT(fmode & FMODE_READ);
216 och_p = &lli->lli_mds_read_och;
217 och_usecount = &lli->lli_open_fd_read_count;
220 mutex_lock(&lli->lli_och_mutex);
221 if (*och_usecount > 0) {
222 /* There are still users of this handle, so skip
224 mutex_unlock(&lli->lli_och_mutex);
230 mutex_unlock(&lli->lli_och_mutex);
233 /* There might be a race and this handle may already
235 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
241 static int ll_md_close(struct inode *inode, struct file *file)
243 union ldlm_policy_data policy = {
244 .l_inodebits = { MDS_INODELOCK_OPEN },
246 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
247 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
248 struct ll_inode_info *lli = ll_i2info(inode);
249 struct lustre_handle lockh;
250 enum ldlm_mode lockmode;
254 /* clear group lock, if present */
255 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
256 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
258 if (fd->fd_lease_och != NULL) {
261 /* Usually the lease is not released when the
262 * application crashed, we need to release here. */
263 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
264 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
265 PFID(&lli->lli_fid), rc, lease_broken);
267 fd->fd_lease_och = NULL;
270 if (fd->fd_och != NULL) {
271 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
276 /* Let's see if we have good enough OPEN lock on the file and if
277 we can skip talking to MDS */
278 mutex_lock(&lli->lli_och_mutex);
279 if (fd->fd_omode & FMODE_WRITE) {
281 LASSERT(lli->lli_open_fd_write_count);
282 lli->lli_open_fd_write_count--;
283 } else if (fd->fd_omode & FMODE_EXEC) {
285 LASSERT(lli->lli_open_fd_exec_count);
286 lli->lli_open_fd_exec_count--;
289 LASSERT(lli->lli_open_fd_read_count);
290 lli->lli_open_fd_read_count--;
292 mutex_unlock(&lli->lli_och_mutex);
294 if (!md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
295 LDLM_IBITS, &policy, lockmode, &lockh))
296 rc = ll_md_real_close(inode, fd->fd_omode);
299 LUSTRE_FPRIVATE(file) = NULL;
300 ll_file_data_put(fd);
305 /* While this returns an error code, fput() the caller does not, so we need
306 * to make every effort to clean up all of our state here. Also, applications
307 * rarely check close errors and even if an error is returned they will not
308 * re-try the close call.
310 int ll_file_release(struct inode *inode, struct file *file)
312 struct ll_file_data *fd;
313 struct ll_sb_info *sbi = ll_i2sbi(inode);
314 struct ll_inode_info *lli = ll_i2info(inode);
318 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
319 PFID(ll_inode2fid(inode)), inode);
321 if (inode->i_sb->s_root != file_dentry(file))
322 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
323 fd = LUSTRE_FPRIVATE(file);
326 /* The last ref on @file, maybe not the the owner pid of statahead,
327 * because parent and child process can share the same file handle. */
328 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
329 ll_deauthorize_statahead(inode, fd);
331 if (inode->i_sb->s_root == file_dentry(file)) {
332 LUSTRE_FPRIVATE(file) = NULL;
333 ll_file_data_put(fd);
337 if (!S_ISDIR(inode->i_mode)) {
338 if (lli->lli_clob != NULL)
339 lov_read_and_clear_async_rc(lli->lli_clob);
340 lli->lli_async_rc = 0;
343 rc = ll_md_close(inode, file);
345 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
346 libcfs_debug_dumplog();
351 static int ll_intent_file_open(struct file *file, void *lmm, int lmmsize,
352 struct lookup_intent *itp)
354 struct dentry *de = file_dentry(file);
355 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
356 struct dentry *parent = de->d_parent;
357 const char *name = NULL;
359 struct md_op_data *op_data;
360 struct ptlrpc_request *req = NULL;
364 LASSERT(parent != NULL);
365 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
367 /* if server supports open-by-fid, or file name is invalid, don't pack
368 * name in open request */
369 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
370 lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
371 name = de->d_name.name;
372 len = de->d_name.len;
375 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
376 name, len, 0, LUSTRE_OPC_ANY, NULL);
378 RETURN(PTR_ERR(op_data));
379 op_data->op_data = lmm;
380 op_data->op_data_size = lmmsize;
382 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
383 &ll_md_blocking_ast, 0);
384 ll_finish_md_op_data(op_data);
386 /* reason for keep own exit path - don`t flood log
387 * with messages with -ESTALE errors.
389 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
390 it_open_error(DISP_OPEN_OPEN, itp))
392 ll_release_openhandle(de, itp);
396 if (it_disposition(itp, DISP_LOOKUP_NEG))
397 GOTO(out, rc = -ENOENT);
399 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
400 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
401 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
405 rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
406 if (!rc && itp->it_lock_mode)
407 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
410 ptlrpc_req_finished(req);
411 ll_intent_drop_lock(itp);
413 /* We did open by fid, but by the time we got to the server,
414 * the object disappeared. If this is a create, we cannot really
415 * tell the userspace that the file it was trying to create
416 * does not exist. Instead let's return -ESTALE, and the VFS will
417 * retry the create with LOOKUP_REVAL that we are going to catch
418 * in ll_revalidate_dentry() and use lookup then.
420 if (rc == -ENOENT && itp->it_op & IT_CREAT)
426 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
427 struct obd_client_handle *och)
429 struct mdt_body *body;
431 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
432 och->och_fh = body->mbo_handle;
433 och->och_fid = body->mbo_fid1;
434 och->och_lease_handle.cookie = it->it_lock_handle;
435 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
436 och->och_flags = it->it_flags;
438 return md_set_open_replay_data(md_exp, och, it);
441 static int ll_local_open(struct file *file, struct lookup_intent *it,
442 struct ll_file_data *fd, struct obd_client_handle *och)
444 struct inode *inode = file_inode(file);
447 LASSERT(!LUSTRE_FPRIVATE(file));
454 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
459 LUSTRE_FPRIVATE(file) = fd;
460 ll_readahead_init(inode, &fd->fd_ras);
461 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
463 /* ll_cl_context initialize */
464 rwlock_init(&fd->fd_lock);
465 INIT_LIST_HEAD(&fd->fd_lccs);
470 /* Open a file, and (for the very first open) create objects on the OSTs at
471 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
472 * creation or open until ll_lov_setstripe() ioctl is called.
474 * If we already have the stripe MD locally then we don't request it in
475 * md_open(), by passing a lmm_size = 0.
477 * It is up to the application to ensure no other processes open this file
478 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
479 * used. We might be able to avoid races of that sort by getting lli_open_sem
480 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
481 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
483 int ll_file_open(struct inode *inode, struct file *file)
485 struct ll_inode_info *lli = ll_i2info(inode);
486 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
487 .it_flags = file->f_flags };
488 struct obd_client_handle **och_p = NULL;
489 __u64 *och_usecount = NULL;
490 struct ll_file_data *fd;
494 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
495 PFID(ll_inode2fid(inode)), inode, file->f_flags);
497 it = file->private_data; /* XXX: compat macro */
498 file->private_data = NULL; /* prevent ll_local_open assertion */
500 fd = ll_file_data_get();
502 GOTO(out_openerr, rc = -ENOMEM);
505 if (S_ISDIR(inode->i_mode))
506 ll_authorize_statahead(inode, fd);
508 if (inode->i_sb->s_root == file_dentry(file)) {
509 LUSTRE_FPRIVATE(file) = fd;
513 if (!it || !it->it_disposition) {
514 /* Convert f_flags into access mode. We cannot use file->f_mode,
515 * because everything but O_ACCMODE mask was stripped from
517 if ((oit.it_flags + 1) & O_ACCMODE)
519 if (file->f_flags & O_TRUNC)
520 oit.it_flags |= FMODE_WRITE;
522 /* kernel only call f_op->open in dentry_open. filp_open calls
523 * dentry_open after call to open_namei that checks permissions.
524 * Only nfsd_open call dentry_open directly without checking
525 * permissions and because of that this code below is safe. */
526 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
527 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
529 /* We do not want O_EXCL here, presumably we opened the file
530 * already? XXX - NFS implications? */
531 oit.it_flags &= ~O_EXCL;
533 /* bug20584, if "it_flags" contains O_CREAT, the file will be
534 * created if necessary, then "IT_CREAT" should be set to keep
535 * consistent with it */
536 if (oit.it_flags & O_CREAT)
537 oit.it_op |= IT_CREAT;
543 /* Let's see if we have file open on MDS already. */
544 if (it->it_flags & FMODE_WRITE) {
545 och_p = &lli->lli_mds_write_och;
546 och_usecount = &lli->lli_open_fd_write_count;
547 } else if (it->it_flags & FMODE_EXEC) {
548 och_p = &lli->lli_mds_exec_och;
549 och_usecount = &lli->lli_open_fd_exec_count;
551 och_p = &lli->lli_mds_read_och;
552 och_usecount = &lli->lli_open_fd_read_count;
555 mutex_lock(&lli->lli_och_mutex);
556 if (*och_p) { /* Open handle is present */
557 if (it_disposition(it, DISP_OPEN_OPEN)) {
558 /* Well, there's extra open request that we do not need,
559 let's close it somehow. This will decref request. */
560 rc = it_open_error(DISP_OPEN_OPEN, it);
562 mutex_unlock(&lli->lli_och_mutex);
563 GOTO(out_openerr, rc);
566 ll_release_openhandle(file_dentry(file), it);
570 rc = ll_local_open(file, it, fd, NULL);
573 mutex_unlock(&lli->lli_och_mutex);
574 GOTO(out_openerr, rc);
577 LASSERT(*och_usecount == 0);
578 if (!it->it_disposition) {
579 struct ll_dentry_data *ldd = ll_d2d(file->f_path.dentry);
580 /* We cannot just request lock handle now, new ELC code
581 means that one of other OPEN locks for this file
582 could be cancelled, and since blocking ast handler
583 would attempt to grab och_mutex as well, that would
584 result in a deadlock */
585 mutex_unlock(&lli->lli_och_mutex);
587 * Normally called under two situations:
589 * 2. A race/condition on MDS resulting in no open
590 * handle to be returned from LOOKUP|OPEN request,
591 * for example if the target entry was a symlink.
593 * Only fetch MDS_OPEN_LOCK if this is in NFS path,
594 * marked by a bit set in ll_iget_for_nfs. Clear the
595 * bit so that it's not confusing later callers.
597 * NB; when ldd is NULL, it must have come via normal
598 * lookup path only, since ll_iget_for_nfs always calls
601 if (ldd && ldd->lld_nfs_dentry) {
602 ldd->lld_nfs_dentry = 0;
603 it->it_flags |= MDS_OPEN_LOCK;
607 * Always specify MDS_OPEN_BY_FID because we don't want
608 * to get file with different fid.
610 it->it_flags |= MDS_OPEN_BY_FID;
611 rc = ll_intent_file_open(file, NULL, 0, it);
613 GOTO(out_openerr, rc);
617 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
619 GOTO(out_och_free, rc = -ENOMEM);
623 /* md_intent_lock() didn't get a request ref if there was an
624 * open error, so don't do cleanup on the request here
626 /* XXX (green): Should not we bail out on any error here, not
627 * just open error? */
628 rc = it_open_error(DISP_OPEN_OPEN, it);
630 GOTO(out_och_free, rc);
632 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
633 "inode %p: disposition %x, status %d\n", inode,
634 it_disposition(it, ~0), it->it_status);
636 rc = ll_local_open(file, it, fd, *och_p);
638 GOTO(out_och_free, rc);
640 mutex_unlock(&lli->lli_och_mutex);
643 /* Must do this outside lli_och_mutex lock to prevent deadlock where
644 different kind of OPEN lock for this same inode gets cancelled
645 by ldlm_cancel_lru */
646 if (!S_ISREG(inode->i_mode))
647 GOTO(out_och_free, rc);
649 cl_lov_delay_create_clear(&file->f_flags);
650 GOTO(out_och_free, rc);
654 if (och_p && *och_p) {
655 OBD_FREE(*och_p, sizeof (struct obd_client_handle));
656 *och_p = NULL; /* OBD_FREE writes some magic there */
659 mutex_unlock(&lli->lli_och_mutex);
662 if (lli->lli_opendir_key == fd)
663 ll_deauthorize_statahead(inode, fd);
665 ll_file_data_put(fd);
667 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
670 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
671 ptlrpc_req_finished(it->it_request);
672 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
678 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
679 struct ldlm_lock_desc *desc, void *data, int flag)
682 struct lustre_handle lockh;
686 case LDLM_CB_BLOCKING:
687 ldlm_lock2handle(lock, &lockh);
688 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
690 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
694 case LDLM_CB_CANCELING:
702 * When setting a lease on a file, we take ownership of the lli_mds_*_och
703 * and save it as fd->fd_och so as to force client to reopen the file even
704 * if it has an open lock in cache already.
706 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
707 struct lustre_handle *old_handle)
709 struct ll_inode_info *lli = ll_i2info(inode);
710 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
711 struct obd_client_handle **och_p;
716 /* Get the openhandle of the file */
717 mutex_lock(&lli->lli_och_mutex);
718 if (fd->fd_lease_och != NULL)
719 GOTO(out_unlock, rc = -EBUSY);
721 if (fd->fd_och == NULL) {
722 if (file->f_mode & FMODE_WRITE) {
723 LASSERT(lli->lli_mds_write_och != NULL);
724 och_p = &lli->lli_mds_write_och;
725 och_usecount = &lli->lli_open_fd_write_count;
727 LASSERT(lli->lli_mds_read_och != NULL);
728 och_p = &lli->lli_mds_read_och;
729 och_usecount = &lli->lli_open_fd_read_count;
732 if (*och_usecount > 1)
733 GOTO(out_unlock, rc = -EBUSY);
740 *old_handle = fd->fd_och->och_fh;
744 mutex_unlock(&lli->lli_och_mutex);
749 * Release ownership on lli_mds_*_och when putting back a file lease.
751 static int ll_lease_och_release(struct inode *inode, struct file *file)
753 struct ll_inode_info *lli = ll_i2info(inode);
754 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
755 struct obd_client_handle **och_p;
756 struct obd_client_handle *old_och = NULL;
761 mutex_lock(&lli->lli_och_mutex);
762 if (file->f_mode & FMODE_WRITE) {
763 och_p = &lli->lli_mds_write_och;
764 och_usecount = &lli->lli_open_fd_write_count;
766 och_p = &lli->lli_mds_read_och;
767 och_usecount = &lli->lli_open_fd_read_count;
770 /* The file may have been open by another process (broken lease) so
771 * *och_p is not NULL. In this case we should simply increase usecount
774 if (*och_p != NULL) {
775 old_och = fd->fd_och;
782 mutex_unlock(&lli->lli_och_mutex);
785 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
791 * Acquire a lease and open the file.
793 static struct obd_client_handle *
794 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
797 struct lookup_intent it = { .it_op = IT_OPEN };
798 struct ll_sb_info *sbi = ll_i2sbi(inode);
799 struct md_op_data *op_data;
800 struct ptlrpc_request *req = NULL;
801 struct lustre_handle old_handle = { 0 };
802 struct obd_client_handle *och = NULL;
807 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
808 RETURN(ERR_PTR(-EINVAL));
811 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
812 RETURN(ERR_PTR(-EPERM));
814 rc = ll_lease_och_acquire(inode, file, &old_handle);
821 RETURN(ERR_PTR(-ENOMEM));
823 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
824 LUSTRE_OPC_ANY, NULL);
826 GOTO(out, rc = PTR_ERR(op_data));
828 /* To tell the MDT this openhandle is from the same owner */
829 op_data->op_handle = old_handle;
831 it.it_flags = fmode | open_flags;
832 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
833 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
834 &ll_md_blocking_lease_ast,
835 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
836 * it can be cancelled which may mislead applications that the lease is
838 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
839 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
840 * doesn't deal with openhandle, so normal openhandle will be leaked. */
841 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
842 ll_finish_md_op_data(op_data);
843 ptlrpc_req_finished(req);
845 GOTO(out_release_it, rc);
847 if (it_disposition(&it, DISP_LOOKUP_NEG))
848 GOTO(out_release_it, rc = -ENOENT);
850 rc = it_open_error(DISP_OPEN_OPEN, &it);
852 GOTO(out_release_it, rc);
854 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
855 ll_och_fill(sbi->ll_md_exp, &it, och);
857 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
858 GOTO(out_close, rc = -EOPNOTSUPP);
860 /* already get lease, handle lease lock */
861 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
862 if (it.it_lock_mode == 0 ||
863 it.it_lock_bits != MDS_INODELOCK_OPEN) {
864 /* open lock must return for lease */
865 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
866 PFID(ll_inode2fid(inode)), it.it_lock_mode,
868 GOTO(out_close, rc = -EPROTO);
871 ll_intent_release(&it);
875 /* Cancel open lock */
876 if (it.it_lock_mode != 0) {
877 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
880 och->och_lease_handle.cookie = 0ULL;
882 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
884 CERROR("%s: error closing file "DFID": %d\n",
885 ll_get_fsname(inode->i_sb, NULL, 0),
886 PFID(&ll_i2info(inode)->lli_fid), rc2);
887 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
889 ll_intent_release(&it);
897 * Check whether a layout swap can be done between two inodes.
899 * \param[in] inode1 First inode to check
900 * \param[in] inode2 Second inode to check
902 * \retval 0 on success, layout swap can be performed between both inodes
903 * \retval negative error code if requirements are not met
905 static int ll_check_swap_layouts_validity(struct inode *inode1,
906 struct inode *inode2)
908 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
911 if (inode_permission(inode1, MAY_WRITE) ||
912 inode_permission(inode2, MAY_WRITE))
915 if (inode1->i_sb != inode2->i_sb)
921 static int ll_swap_layouts_close(struct obd_client_handle *och,
922 struct inode *inode, struct inode *inode2)
924 const struct lu_fid *fid1 = ll_inode2fid(inode);
925 const struct lu_fid *fid2;
929 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
930 ll_get_fsname(inode->i_sb, NULL, 0), PFID(fid1));
932 rc = ll_check_swap_layouts_validity(inode, inode2);
934 GOTO(out_free_och, rc);
936 /* We now know that inode2 is a lustre inode */
937 fid2 = ll_inode2fid(inode2);
939 rc = lu_fid_cmp(fid1, fid2);
941 GOTO(out_free_och, rc = -EINVAL);
943 /* Close the file and swap layouts between inode & inode2.
944 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
945 * because we still need it to pack l_remote_handle to MDT. */
946 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
949 och = NULL; /* freed in ll_close_inode_openhandle() */
959 * Release lease and close the file.
960 * It will check if the lease has ever broken.
962 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
965 struct ldlm_lock *lock;
966 bool cancelled = true;
970 lock = ldlm_handle2lock(&och->och_lease_handle);
972 lock_res_and_lock(lock);
973 cancelled = ldlm_is_cancel(lock);
974 unlock_res_and_lock(lock);
978 CDEBUG(D_INODE, "lease for "DFID" broken? %d\n",
979 PFID(&ll_i2info(inode)->lli_fid), cancelled);
982 ldlm_cli_cancel(&och->och_lease_handle, 0);
984 if (lease_broken != NULL)
985 *lease_broken = cancelled;
987 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
991 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
993 struct ll_inode_info *lli = ll_i2info(inode);
994 struct cl_object *obj = lli->lli_clob;
995 struct cl_attr *attr = vvp_env_thread_attr(env);
1003 ll_inode_size_lock(inode);
1005 /* Merge timestamps the most recently obtained from MDS with
1006 * timestamps obtained from OSTs.
1008 * Do not overwrite atime of inode because it may be refreshed
1009 * by file_accessed() function. If the read was served by cache
1010 * data, there is no RPC to be sent so that atime may not be
1011 * transferred to OSTs at all. MDT only updates atime at close time
1012 * if it's at least 'mdd.*.atime_diff' older.
1013 * All in all, the atime in Lustre does not strictly comply with
1014 * POSIX. Solving this problem needs to send an RPC to MDT for each
1015 * read, this will hurt performance. */
1016 if (LTIME_S(inode->i_atime) < lli->lli_atime)
1017 LTIME_S(inode->i_atime) = lli->lli_atime;
1018 LTIME_S(inode->i_mtime) = lli->lli_mtime;
1019 LTIME_S(inode->i_ctime) = lli->lli_ctime;
1021 atime = LTIME_S(inode->i_atime);
1022 mtime = LTIME_S(inode->i_mtime);
1023 ctime = LTIME_S(inode->i_ctime);
1025 cl_object_attr_lock(obj);
1026 rc = cl_object_attr_get(env, obj, attr);
1027 cl_object_attr_unlock(obj);
1030 GOTO(out_size_unlock, rc);
1032 if (atime < attr->cat_atime)
1033 atime = attr->cat_atime;
1035 if (ctime < attr->cat_ctime)
1036 ctime = attr->cat_ctime;
1038 if (mtime < attr->cat_mtime)
1039 mtime = attr->cat_mtime;
1041 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1042 PFID(&lli->lli_fid), attr->cat_size);
1044 i_size_write(inode, attr->cat_size);
1045 inode->i_blocks = attr->cat_blocks;
1047 LTIME_S(inode->i_atime) = atime;
1048 LTIME_S(inode->i_mtime) = mtime;
1049 LTIME_S(inode->i_ctime) = ctime;
1052 ll_inode_size_unlock(inode);
1057 static bool file_is_noatime(const struct file *file)
1059 const struct vfsmount *mnt = file->f_path.mnt;
1060 const struct inode *inode = file_inode((struct file *)file);
1062 /* Adapted from file_accessed() and touch_atime().*/
1063 if (file->f_flags & O_NOATIME)
1066 if (inode->i_flags & S_NOATIME)
1069 if (IS_NOATIME(inode))
1072 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1075 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1078 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1084 static void ll_io_init(struct cl_io *io, const struct file *file, int write)
1086 struct inode *inode = file_inode((struct file *)file);
1088 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1090 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1091 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC ||
1092 file->f_flags & O_DIRECT ||
1095 io->ci_obj = ll_i2info(inode)->lli_clob;
1096 io->ci_lockreq = CILR_MAYBE;
1097 if (ll_file_nolock(file)) {
1098 io->ci_lockreq = CILR_NEVER;
1099 io->ci_no_srvlock = 1;
1100 } else if (file->f_flags & O_APPEND) {
1101 io->ci_lockreq = CILR_MANDATORY;
1104 io->ci_noatime = file_is_noatime(file);
1108 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1109 struct file *file, enum cl_io_type iot,
1110 loff_t *ppos, size_t count)
1112 struct vvp_io *vio = vvp_env_io(env);
1113 struct inode *inode = file_inode(file);
1114 struct ll_inode_info *lli = ll_i2info(inode);
1115 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1119 struct range_lock range;
1123 CDEBUG(D_VFSTRACE, "file: %s, type: %d ppos: %llu, count: %zu\n",
1124 file_dentry(file)->d_name.name, iot, *ppos, count);
1127 io = vvp_env_thread_io(env);
1128 ll_io_init(io, file, iot == CIT_WRITE);
1130 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1131 bool range_locked = false;
1133 if (file->f_flags & O_APPEND)
1134 range_lock_init(&range, 0, LUSTRE_EOF);
1136 range_lock_init(&range, *ppos, *ppos + count - 1);
1138 vio->vui_fd = LUSTRE_FPRIVATE(file);
1139 vio->vui_io_subtype = args->via_io_subtype;
1141 switch (vio->vui_io_subtype) {
1143 vio->vui_iter = args->u.normal.via_iter;
1144 vio->vui_iocb = args->u.normal.via_iocb;
1145 /* Direct IO reads must also take range lock,
1146 * or multiple reads will try to work on the same pages
1147 * See LU-6227 for details. */
1148 if (((iot == CIT_WRITE) ||
1149 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1150 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1151 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1153 rc = range_lock(&lli->lli_write_tree, &range);
1157 range_locked = true;
1161 vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1162 vio->u.splice.vui_flags = args->u.splice.via_flags;
1165 CERROR("unknown IO subtype %u\n", vio->vui_io_subtype);
1169 ll_cl_add(file, env, io, LCC_RW);
1170 rc = cl_io_loop(env, io);
1171 ll_cl_remove(file, env);
1174 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1176 range_unlock(&lli->lli_write_tree, &range);
1179 /* cl_io_rw_init() handled IO */
1183 if (io->ci_nob > 0) {
1184 result += io->ci_nob;
1185 count -= io->ci_nob;
1186 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1188 /* prepare IO restart */
1189 if (count > 0 && args->via_io_subtype == IO_NORMAL)
1190 args->u.normal.via_iter = vio->vui_iter;
1194 cl_io_fini(env, io);
1196 if ((rc == 0 || rc == -ENODATA) && count > 0 && io->ci_need_restart) {
1198 "%s: restart %s from %lld, count:%zu, result: %zd\n",
1199 file_dentry(file)->d_name.name,
1200 iot == CIT_READ ? "read" : "write",
1201 *ppos, count, result);
1205 if (iot == CIT_READ) {
1207 ll_stats_ops_tally(ll_i2sbi(inode),
1208 LPROC_LL_READ_BYTES, result);
1209 } else if (iot == CIT_WRITE) {
1211 ll_stats_ops_tally(ll_i2sbi(inode),
1212 LPROC_LL_WRITE_BYTES, result);
1213 fd->fd_write_failed = false;
1214 } else if (result == 0 && rc == 0) {
1217 fd->fd_write_failed = true;
1219 fd->fd_write_failed = false;
1220 } else if (rc != -ERESTARTSYS) {
1221 fd->fd_write_failed = true;
1225 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1227 return result > 0 ? result : rc;
1231 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1232 * especially for small I/O.
1234 * To serve a read request, CLIO has to create and initialize a cl_io and
1235 * then request DLM lock. This has turned out to have siginificant overhead
1236 * and affects the performance of small I/O dramatically.
1238 * It's not necessary to create a cl_io for each I/O. Under the help of read
1239 * ahead, most of the pages being read are already in memory cache and we can
1240 * read those pages directly because if the pages exist, the corresponding DLM
1241 * lock must exist so that page content must be valid.
1243 * In fast read implementation, the llite speculatively finds and reads pages
1244 * in memory cache. There are three scenarios for fast read:
1245 * - If the page exists and is uptodate, kernel VM will provide the data and
1246 * CLIO won't be intervened;
1247 * - If the page was brought into memory by read ahead, it will be exported
1248 * and read ahead parameters will be updated;
1249 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1250 * it will go back and invoke normal read, i.e., a cl_io will be created
1251 * and DLM lock will be requested.
1253 * POSIX compliance: posix standard states that read is intended to be atomic.
1254 * Lustre read implementation is in line with Linux kernel read implementation
1255 * and neither of them complies with POSIX standard in this matter. Fast read
1256 * doesn't make the situation worse on single node but it may interleave write
1257 * results from multiple nodes due to short read handling in ll_file_aio_read().
1259 * \param env - lu_env
1260 * \param iocb - kiocb from kernel
1261 * \param iter - user space buffers where the data will be copied
1263 * \retval - number of bytes have been read, or error code if error occurred.
1266 ll_do_fast_read(const struct lu_env *env, struct kiocb *iocb,
1267 struct iov_iter *iter)
1271 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1274 /* NB: we can't do direct IO for fast read because it will need a lock
1275 * to make IO engine happy. */
1276 if (iocb->ki_filp->f_flags & O_DIRECT)
1279 ll_cl_add(iocb->ki_filp, env, NULL, LCC_RW);
1280 result = generic_file_read_iter(iocb, iter);
1281 ll_cl_remove(iocb->ki_filp, env);
1283 /* If the first page is not in cache, generic_file_aio_read() will be
1284 * returned with -ENODATA.
1285 * See corresponding code in ll_readpage(). */
1286 if (result == -ENODATA)
1290 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1291 LPROC_LL_READ_BYTES, result);
1297 * Read from a file (through the page cache).
1299 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1302 struct vvp_io_args *args;
1307 env = cl_env_get(&refcheck);
1309 return PTR_ERR(env);
1311 result = ll_do_fast_read(env, iocb, to);
1312 if (result < 0 || iov_iter_count(to) == 0)
1315 args = ll_env_args(env, IO_NORMAL);
1316 args->u.normal.via_iter = to;
1317 args->u.normal.via_iocb = iocb;
1319 rc2 = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1320 &iocb->ki_pos, iov_iter_count(to));
1323 else if (result == 0)
1327 cl_env_put(env, &refcheck);
1332 * Write to a file (through the page cache).
1334 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1336 struct vvp_io_args *args;
1341 env = cl_env_get(&refcheck);
1343 return PTR_ERR(env);
1345 args = ll_env_args(env, IO_NORMAL);
1346 args->u.normal.via_iter = from;
1347 args->u.normal.via_iocb = iocb;
1349 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1350 &iocb->ki_pos, iov_iter_count(from));
1351 cl_env_put(env, &refcheck);
1355 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1357 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1359 static int ll_file_get_iov_count(const struct iovec *iov,
1360 unsigned long *nr_segs, size_t *count)
1365 for (seg = 0; seg < *nr_segs; seg++) {
1366 const struct iovec *iv = &iov[seg];
1369 * If any segment has a negative length, or the cumulative
1370 * length ever wraps negative then return -EINVAL.
1373 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1375 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1380 cnt -= iv->iov_len; /* This segment is no good */
1387 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1388 unsigned long nr_segs, loff_t pos)
1395 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1399 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1400 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
1401 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1402 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
1403 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1405 result = ll_file_read_iter(iocb, &to);
1410 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
1413 struct iovec iov = { .iov_base = buf, .iov_len = count };
1414 struct kiocb *kiocb;
1418 OBD_ALLOC_PTR(kiocb);
1422 init_sync_kiocb(kiocb, file);
1423 kiocb->ki_pos = *ppos;
1424 #ifdef HAVE_KIOCB_KI_LEFT
1425 kiocb->ki_left = count;
1426 #elif defined(HAVE_KI_NBYTES)
1427 kiocb->ki_nbytes = count;
1430 result = ll_file_aio_read(kiocb, &iov, 1, kiocb->ki_pos);
1431 *ppos = kiocb->ki_pos;
1433 OBD_FREE_PTR(kiocb);
1438 * Write to a file (through the page cache).
1441 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1442 unsigned long nr_segs, loff_t pos)
1444 struct iov_iter from;
1449 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1453 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1454 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
1455 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1456 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
1457 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1459 result = ll_file_write_iter(iocb, &from);
1464 static ssize_t ll_file_write(struct file *file, const char __user *buf,
1465 size_t count, loff_t *ppos)
1468 struct iovec iov = { .iov_base = (void __user *)buf,
1470 struct kiocb *kiocb;
1475 env = cl_env_get(&refcheck);
1477 RETURN(PTR_ERR(env));
1479 kiocb = &ll_env_info(env)->lti_kiocb;
1480 init_sync_kiocb(kiocb, file);
1481 kiocb->ki_pos = *ppos;
1482 #ifdef HAVE_KIOCB_KI_LEFT
1483 kiocb->ki_left = count;
1484 #elif defined(HAVE_KI_NBYTES)
1485 kiocb->ki_nbytes = count;
1488 result = ll_file_aio_write(kiocb, &iov, 1, kiocb->ki_pos);
1489 *ppos = kiocb->ki_pos;
1491 cl_env_put(env, &refcheck);
1494 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
1497 * Send file content (through pagecache) somewhere with helper
1499 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1500 struct pipe_inode_info *pipe, size_t count,
1504 struct vvp_io_args *args;
1509 env = cl_env_get(&refcheck);
1511 RETURN(PTR_ERR(env));
1513 args = ll_env_args(env, IO_SPLICE);
1514 args->u.splice.via_pipe = pipe;
1515 args->u.splice.via_flags = flags;
1517 result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1518 cl_env_put(env, &refcheck);
1522 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1523 __u64 flags, struct lov_user_md *lum,
1526 struct lookup_intent oit = {
1528 .it_flags = flags | MDS_OPEN_BY_FID,
1533 ll_inode_size_lock(inode);
1534 rc = ll_intent_file_open(file, lum, lum_size, &oit);
1536 GOTO(out_unlock, rc);
1538 ll_release_openhandle(file_dentry(file), &oit);
1541 ll_inode_size_unlock(inode);
1542 ll_intent_release(&oit);
1543 cl_lov_delay_create_clear(&file->f_flags);
1548 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1549 struct lov_mds_md **lmmp, int *lmm_size,
1550 struct ptlrpc_request **request)
1552 struct ll_sb_info *sbi = ll_i2sbi(inode);
1553 struct mdt_body *body;
1554 struct lov_mds_md *lmm = NULL;
1555 struct ptlrpc_request *req = NULL;
1556 struct md_op_data *op_data;
1559 rc = ll_get_default_mdsize(sbi, &lmmsize);
1563 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1564 strlen(filename), lmmsize,
1565 LUSTRE_OPC_ANY, NULL);
1566 if (IS_ERR(op_data))
1567 RETURN(PTR_ERR(op_data));
1569 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1570 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1571 ll_finish_md_op_data(op_data);
1573 CDEBUG(D_INFO, "md_getattr_name failed "
1574 "on %s: rc %d\n", filename, rc);
1578 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1579 LASSERT(body != NULL); /* checked by mdc_getattr_name */
1581 lmmsize = body->mbo_eadatasize;
1583 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1585 GOTO(out, rc = -ENODATA);
1588 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1589 LASSERT(lmm != NULL);
1591 if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1592 (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1593 GOTO(out, rc = -EPROTO);
1597 * This is coming from the MDS, so is probably in
1598 * little endian. We convert it to host endian before
1599 * passing it to userspace.
1601 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1604 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
1605 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
1608 /* if function called for directory - we should
1609 * avoid swab not existent lsm objects */
1610 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1611 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1612 if (S_ISREG(body->mbo_mode))
1613 lustre_swab_lov_user_md_objects(
1614 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1616 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1617 lustre_swab_lov_user_md_v3(
1618 (struct lov_user_md_v3 *)lmm);
1619 if (S_ISREG(body->mbo_mode))
1620 lustre_swab_lov_user_md_objects(
1621 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1628 *lmm_size = lmmsize;
1633 static int ll_lov_setea(struct inode *inode, struct file *file,
1636 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1637 struct lov_user_md *lump;
1638 int lum_size = sizeof(struct lov_user_md) +
1639 sizeof(struct lov_user_ost_data);
1643 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1646 OBD_ALLOC_LARGE(lump, lum_size);
1650 if (copy_from_user(lump, (struct lov_user_md __user *)arg, lum_size))
1651 GOTO(out_lump, rc = -EFAULT);
1653 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1656 OBD_FREE_LARGE(lump, lum_size);
1660 static int ll_file_getstripe(struct inode *inode,
1661 struct lov_user_md __user *lum)
1668 env = cl_env_get(&refcheck);
1670 RETURN(PTR_ERR(env));
1672 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum);
1673 cl_env_put(env, &refcheck);
1677 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1680 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
1681 struct lov_user_md *klum;
1683 __u64 flags = FMODE_WRITE;
1686 rc = ll_copy_user_md(lum, &klum);
1691 rc = ll_lov_setstripe_ea_info(inode, file, flags, klum, lum_size);
1695 put_user(0, &lum->lmm_stripe_count);
1697 ll_layout_refresh(inode, &gen);
1698 rc = ll_file_getstripe(inode, (struct lov_user_md __user *)arg);
1701 OBD_FREE(klum, lum_size);
1706 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1708 struct ll_inode_info *lli = ll_i2info(inode);
1709 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1710 struct ll_grouplock grouplock;
1715 CWARN("group id for group lock must not be 0\n");
1719 if (ll_file_nolock(file))
1720 RETURN(-EOPNOTSUPP);
1722 spin_lock(&lli->lli_lock);
1723 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1724 CWARN("group lock already existed with gid %lu\n",
1725 fd->fd_grouplock.lg_gid);
1726 spin_unlock(&lli->lli_lock);
1729 LASSERT(fd->fd_grouplock.lg_lock == NULL);
1730 spin_unlock(&lli->lli_lock);
1732 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
1733 arg, (file->f_flags & O_NONBLOCK), &grouplock);
1737 spin_lock(&lli->lli_lock);
1738 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1739 spin_unlock(&lli->lli_lock);
1740 CERROR("another thread just won the race\n");
1741 cl_put_grouplock(&grouplock);
1745 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1746 fd->fd_grouplock = grouplock;
1747 spin_unlock(&lli->lli_lock);
1749 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1753 static int ll_put_grouplock(struct inode *inode, struct file *file,
1756 struct ll_inode_info *lli = ll_i2info(inode);
1757 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1758 struct ll_grouplock grouplock;
1761 spin_lock(&lli->lli_lock);
1762 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1763 spin_unlock(&lli->lli_lock);
1764 CWARN("no group lock held\n");
1768 LASSERT(fd->fd_grouplock.lg_lock != NULL);
1770 if (fd->fd_grouplock.lg_gid != arg) {
1771 CWARN("group lock %lu doesn't match current id %lu\n",
1772 arg, fd->fd_grouplock.lg_gid);
1773 spin_unlock(&lli->lli_lock);
1777 grouplock = fd->fd_grouplock;
1778 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1779 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1780 spin_unlock(&lli->lli_lock);
1782 cl_put_grouplock(&grouplock);
1783 CDEBUG(D_INFO, "group lock %lu released\n", arg);
1788 * Close inode open handle
1790 * \param dentry [in] dentry which contains the inode
1791 * \param it [in,out] intent which contains open info and result
1794 * \retval <0 failure
1796 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1798 struct inode *inode = dentry->d_inode;
1799 struct obd_client_handle *och;
1805 /* Root ? Do nothing. */
1806 if (dentry->d_inode->i_sb->s_root == dentry)
1809 /* No open handle to close? Move away */
1810 if (!it_disposition(it, DISP_OPEN_OPEN))
1813 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1815 OBD_ALLOC(och, sizeof(*och));
1817 GOTO(out, rc = -ENOMEM);
1819 ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
1821 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
1823 /* this one is in place of ll_file_open */
1824 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1825 ptlrpc_req_finished(it->it_request);
1826 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1832 * Get size for inode for which FIEMAP mapping is requested.
1833 * Make the FIEMAP get_info call and returns the result.
1834 * \param fiemap kernel buffer to hold extens
1835 * \param num_bytes kernel buffer size
1837 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
1843 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
1846 /* Checks for fiemap flags */
1847 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1848 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1852 /* Check for FIEMAP_FLAG_SYNC */
1853 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1854 rc = filemap_fdatawrite(inode->i_mapping);
1859 env = cl_env_get(&refcheck);
1861 RETURN(PTR_ERR(env));
1863 if (i_size_read(inode) == 0) {
1864 rc = ll_glimpse_size(inode);
1869 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1870 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
1871 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
1873 /* If filesize is 0, then there would be no objects for mapping */
1874 if (fmkey.lfik_oa.o_size == 0) {
1875 fiemap->fm_mapped_extents = 0;
1879 fmkey.lfik_fiemap = *fiemap;
1881 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
1882 &fmkey, fiemap, &num_bytes);
1884 cl_env_put(env, &refcheck);
1888 int ll_fid2path(struct inode *inode, void __user *arg)
1890 struct obd_export *exp = ll_i2mdexp(inode);
1891 const struct getinfo_fid2path __user *gfin = arg;
1893 struct getinfo_fid2path *gfout;
1899 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1900 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
1903 /* Only need to get the buflen */
1904 if (get_user(pathlen, &gfin->gf_pathlen))
1907 if (pathlen > PATH_MAX)
1910 outsize = sizeof(*gfout) + pathlen;
1911 OBD_ALLOC(gfout, outsize);
1915 if (copy_from_user(gfout, arg, sizeof(*gfout)))
1916 GOTO(gf_free, rc = -EFAULT);
1917 /* append root FID after gfout to let MDT know the root FID so that it
1918 * can lookup the correct path, this is mainly for fileset.
1919 * old server without fileset mount support will ignore this. */
1920 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
1922 /* Call mdc_iocontrol */
1923 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1927 if (copy_to_user(arg, gfout, outsize))
1931 OBD_FREE(gfout, outsize);
1936 * Read the data_version for inode.
1938 * This value is computed using stripe object version on OST.
1939 * Version is computed using server side locking.
1941 * @param flags if do sync on the OST side;
1943 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
1944 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
1946 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
1948 struct cl_object *obj = ll_i2info(inode)->lli_clob;
1956 /* If no file object initialized, we consider its version is 0. */
1962 env = cl_env_get(&refcheck);
1964 RETURN(PTR_ERR(env));
1966 io = vvp_env_thread_io(env);
1968 io->u.ci_data_version.dv_data_version = 0;
1969 io->u.ci_data_version.dv_flags = flags;
1972 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
1973 result = cl_io_loop(env, io);
1975 result = io->ci_result;
1977 *data_version = io->u.ci_data_version.dv_data_version;
1979 cl_io_fini(env, io);
1981 if (unlikely(io->ci_need_restart))
1984 cl_env_put(env, &refcheck);
1990 * Trigger a HSM release request for the provided inode.
1992 int ll_hsm_release(struct inode *inode)
1995 struct obd_client_handle *och = NULL;
1996 __u64 data_version = 0;
2001 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2002 ll_get_fsname(inode->i_sb, NULL, 0),
2003 PFID(&ll_i2info(inode)->lli_fid));
2005 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2007 GOTO(out, rc = PTR_ERR(och));
2009 /* Grab latest data_version and [am]time values */
2010 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2014 env = cl_env_get(&refcheck);
2016 GOTO(out, rc = PTR_ERR(env));
2018 ll_merge_attr(env, inode);
2019 cl_env_put(env, &refcheck);
2021 /* Release the file.
2022 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2023 * we still need it to pack l_remote_handle to MDT. */
2024 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2030 if (och != NULL && !IS_ERR(och)) /* close the file */
2031 ll_lease_close(och, inode, NULL);
2036 struct ll_swap_stack {
2039 struct inode *inode1;
2040 struct inode *inode2;
2045 static int ll_swap_layouts(struct file *file1, struct file *file2,
2046 struct lustre_swap_layouts *lsl)
2048 struct mdc_swap_layouts msl;
2049 struct md_op_data *op_data;
2052 struct ll_swap_stack *llss = NULL;
2055 OBD_ALLOC_PTR(llss);
2059 llss->inode1 = file_inode(file1);
2060 llss->inode2 = file_inode(file2);
2062 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2066 /* we use 2 bool because it is easier to swap than 2 bits */
2067 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2068 llss->check_dv1 = true;
2070 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2071 llss->check_dv2 = true;
2073 /* we cannot use lsl->sl_dvX directly because we may swap them */
2074 llss->dv1 = lsl->sl_dv1;
2075 llss->dv2 = lsl->sl_dv2;
2077 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2078 if (rc == 0) /* same file, done! */
2081 if (rc < 0) { /* sequentialize it */
2082 swap(llss->inode1, llss->inode2);
2084 swap(llss->dv1, llss->dv2);
2085 swap(llss->check_dv1, llss->check_dv2);
2089 if (gid != 0) { /* application asks to flush dirty cache */
2090 rc = ll_get_grouplock(llss->inode1, file1, gid);
2094 rc = ll_get_grouplock(llss->inode2, file2, gid);
2096 ll_put_grouplock(llss->inode1, file1, gid);
2101 /* ultimate check, before swaping the layouts we check if
2102 * dataversion has changed (if requested) */
2103 if (llss->check_dv1) {
2104 rc = ll_data_version(llss->inode1, &dv, 0);
2107 if (dv != llss->dv1)
2108 GOTO(putgl, rc = -EAGAIN);
2111 if (llss->check_dv2) {
2112 rc = ll_data_version(llss->inode2, &dv, 0);
2115 if (dv != llss->dv2)
2116 GOTO(putgl, rc = -EAGAIN);
2119 /* struct md_op_data is used to send the swap args to the mdt
2120 * only flags is missing, so we use struct mdc_swap_layouts
2121 * through the md_op_data->op_data */
2122 /* flags from user space have to be converted before they are send to
2123 * server, no flag is sent today, they are only used on the client */
2126 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2127 0, LUSTRE_OPC_ANY, &msl);
2128 if (IS_ERR(op_data))
2129 GOTO(free, rc = PTR_ERR(op_data));
2131 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2132 sizeof(*op_data), op_data, NULL);
2133 ll_finish_md_op_data(op_data);
2140 ll_put_grouplock(llss->inode2, file2, gid);
2141 ll_put_grouplock(llss->inode1, file1, gid);
2151 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2153 struct md_op_data *op_data;
2157 /* Detect out-of range masks */
2158 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2161 /* Non-root users are forbidden to set or clear flags which are
2162 * NOT defined in HSM_USER_MASK. */
2163 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2164 !cfs_capable(CFS_CAP_SYS_ADMIN))
2167 /* Detect out-of range archive id */
2168 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2169 (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
2172 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2173 LUSTRE_OPC_ANY, hss);
2174 if (IS_ERR(op_data))
2175 RETURN(PTR_ERR(op_data));
2177 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
2178 sizeof(*op_data), op_data, NULL);
2180 ll_finish_md_op_data(op_data);
2185 static int ll_hsm_import(struct inode *inode, struct file *file,
2186 struct hsm_user_import *hui)
2188 struct hsm_state_set *hss = NULL;
2189 struct iattr *attr = NULL;
2193 if (!S_ISREG(inode->i_mode))
2199 GOTO(out, rc = -ENOMEM);
2201 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2202 hss->hss_archive_id = hui->hui_archive_id;
2203 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2204 rc = ll_hsm_state_set(inode, hss);
2208 OBD_ALLOC_PTR(attr);
2210 GOTO(out, rc = -ENOMEM);
2212 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2213 attr->ia_mode |= S_IFREG;
2214 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2215 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2216 attr->ia_size = hui->hui_size;
2217 attr->ia_mtime.tv_sec = hui->hui_mtime;
2218 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2219 attr->ia_atime.tv_sec = hui->hui_atime;
2220 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2222 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2223 ATTR_UID | ATTR_GID |
2224 ATTR_MTIME | ATTR_MTIME_SET |
2225 ATTR_ATIME | ATTR_ATIME_SET;
2229 rc = ll_setattr_raw(file_dentry(file), attr, true);
2233 inode_unlock(inode);
2245 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2247 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2248 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2251 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2253 struct inode *inode = file_inode(file);
2255 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2256 ATTR_MTIME | ATTR_MTIME_SET |
2257 ATTR_CTIME | ATTR_CTIME_SET,
2259 .tv_sec = lfu->lfu_atime_sec,
2260 .tv_nsec = lfu->lfu_atime_nsec,
2263 .tv_sec = lfu->lfu_mtime_sec,
2264 .tv_nsec = lfu->lfu_mtime_nsec,
2267 .tv_sec = lfu->lfu_ctime_sec,
2268 .tv_nsec = lfu->lfu_ctime_nsec,
2274 if (!capable(CAP_SYS_ADMIN))
2277 if (!S_ISREG(inode->i_mode))
2281 rc = ll_setattr_raw(file_dentry(file), &ia, false);
2282 inode_unlock(inode);
2288 * Give file access advices
2290 * The ladvise interface is similar to Linux fadvise() system call, except it
2291 * forwards the advices directly from Lustre client to server. The server side
2292 * codes will apply appropriate read-ahead and caching techniques for the
2293 * corresponding files.
2295 * A typical workload for ladvise is e.g. a bunch of different clients are
2296 * doing small random reads of a file, so prefetching pages into OSS cache
2297 * with big linear reads before the random IO is a net benefit. Fetching
2298 * all that data into each client cache with fadvise() may not be, due to
2299 * much more data being sent to the client.
2301 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
2302 struct llapi_lu_ladvise *ladvise)
2306 struct cl_ladvise_io *lio;
2311 env = cl_env_get(&refcheck);
2313 RETURN(PTR_ERR(env));
2315 io = vvp_env_thread_io(env);
2316 io->ci_obj = ll_i2info(inode)->lli_clob;
2318 /* initialize parameters for ladvise */
2319 lio = &io->u.ci_ladvise;
2320 lio->li_start = ladvise->lla_start;
2321 lio->li_end = ladvise->lla_end;
2322 lio->li_fid = ll_inode2fid(inode);
2323 lio->li_advice = ladvise->lla_advice;
2324 lio->li_flags = flags;
2326 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
2327 rc = cl_io_loop(env, io);
2331 cl_io_fini(env, io);
2332 cl_env_put(env, &refcheck);
2337 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2339 struct inode *inode = file_inode(file);
2340 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2344 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
2345 PFID(ll_inode2fid(inode)), inode, cmd);
2346 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
2348 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
2349 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
2353 case LL_IOC_GETFLAGS:
2354 /* Get the current value of the file flags */
2355 return put_user(fd->fd_flags, (int __user *)arg);
2356 case LL_IOC_SETFLAGS:
2357 case LL_IOC_CLRFLAGS:
2358 /* Set or clear specific file flags */
2359 /* XXX This probably needs checks to ensure the flags are
2360 * not abused, and to handle any flag side effects.
2362 if (get_user(flags, (int __user *) arg))
2365 if (cmd == LL_IOC_SETFLAGS) {
2366 if ((flags & LL_FILE_IGNORE_LOCK) &&
2367 !(file->f_flags & O_DIRECT)) {
2368 CERROR("%s: unable to disable locking on "
2369 "non-O_DIRECT file\n", current->comm);
2373 fd->fd_flags |= flags;
2375 fd->fd_flags &= ~flags;
2378 case LL_IOC_LOV_SETSTRIPE:
2379 RETURN(ll_lov_setstripe(inode, file, arg));
2380 case LL_IOC_LOV_SETEA:
2381 RETURN(ll_lov_setea(inode, file, arg));
2382 case LL_IOC_LOV_SWAP_LAYOUTS: {
2384 struct lustre_swap_layouts lsl;
2386 if (copy_from_user(&lsl, (char __user *)arg,
2387 sizeof(struct lustre_swap_layouts)))
2390 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
2393 file2 = fget(lsl.sl_fd);
2397 /* O_WRONLY or O_RDWR */
2398 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
2399 GOTO(out, rc = -EPERM);
2401 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
2402 struct inode *inode2;
2403 struct ll_inode_info *lli;
2404 struct obd_client_handle *och = NULL;
2406 if (lsl.sl_flags != SWAP_LAYOUTS_CLOSE)
2407 GOTO(out, rc = -EINVAL);
2409 lli = ll_i2info(inode);
2410 mutex_lock(&lli->lli_och_mutex);
2411 if (fd->fd_lease_och != NULL) {
2412 och = fd->fd_lease_och;
2413 fd->fd_lease_och = NULL;
2415 mutex_unlock(&lli->lli_och_mutex);
2417 GOTO(out, rc = -ENOLCK);
2418 inode2 = file_inode(file2);
2419 rc = ll_swap_layouts_close(och, inode, inode2);
2421 rc = ll_swap_layouts(file, file2, &lsl);
2427 case LL_IOC_LOV_GETSTRIPE:
2428 RETURN(ll_file_getstripe(inode,
2429 (struct lov_user_md __user *)arg));
2430 case FSFILT_IOC_GETFLAGS:
2431 case FSFILT_IOC_SETFLAGS:
2432 RETURN(ll_iocontrol(inode, file, cmd, arg));
2433 case FSFILT_IOC_GETVERSION_OLD:
2434 case FSFILT_IOC_GETVERSION:
2435 RETURN(put_user(inode->i_generation, (int __user *)arg));
2436 case LL_IOC_GROUP_LOCK:
2437 RETURN(ll_get_grouplock(inode, file, arg));
2438 case LL_IOC_GROUP_UNLOCK:
2439 RETURN(ll_put_grouplock(inode, file, arg));
2440 case IOC_OBD_STATFS:
2441 RETURN(ll_obd_statfs(inode, (void __user *)arg));
2443 /* We need to special case any other ioctls we want to handle,
2444 * to send them to the MDS/OST as appropriate and to properly
2445 * network encode the arg field.
2446 case FSFILT_IOC_SETVERSION_OLD:
2447 case FSFILT_IOC_SETVERSION:
2449 case LL_IOC_FLUSHCTX:
2450 RETURN(ll_flush_ctx(inode));
2451 case LL_IOC_PATH2FID: {
2452 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2453 sizeof(struct lu_fid)))
2458 case LL_IOC_GETPARENT:
2459 RETURN(ll_getparent(file, (struct getparent __user *)arg));
2461 case OBD_IOC_FID2PATH:
2462 RETURN(ll_fid2path(inode, (void __user *)arg));
2463 case LL_IOC_DATA_VERSION: {
2464 struct ioc_data_version idv;
2467 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
2470 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
2471 rc = ll_data_version(inode, &idv.idv_version, idv.idv_flags);
2474 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
2480 case LL_IOC_GET_MDTIDX: {
2483 mdtidx = ll_get_mdt_idx(inode);
2487 if (put_user((int)mdtidx, (int __user *)arg))
2492 case OBD_IOC_GETDTNAME:
2493 case OBD_IOC_GETMDNAME:
2494 RETURN(ll_get_obd_name(inode, cmd, arg));
2495 case LL_IOC_HSM_STATE_GET: {
2496 struct md_op_data *op_data;
2497 struct hsm_user_state *hus;
2504 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2505 LUSTRE_OPC_ANY, hus);
2506 if (IS_ERR(op_data)) {
2508 RETURN(PTR_ERR(op_data));
2511 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2514 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
2517 ll_finish_md_op_data(op_data);
2521 case LL_IOC_HSM_STATE_SET: {
2522 struct hsm_state_set *hss;
2529 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
2534 rc = ll_hsm_state_set(inode, hss);
2539 case LL_IOC_HSM_ACTION: {
2540 struct md_op_data *op_data;
2541 struct hsm_current_action *hca;
2548 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2549 LUSTRE_OPC_ANY, hca);
2550 if (IS_ERR(op_data)) {
2552 RETURN(PTR_ERR(op_data));
2555 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2558 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
2561 ll_finish_md_op_data(op_data);
2565 case LL_IOC_SET_LEASE: {
2566 struct ll_inode_info *lli = ll_i2info(inode);
2567 struct obd_client_handle *och = NULL;
2572 case LL_LEASE_WRLCK:
2573 if (!(file->f_mode & FMODE_WRITE))
2575 fmode = FMODE_WRITE;
2577 case LL_LEASE_RDLCK:
2578 if (!(file->f_mode & FMODE_READ))
2582 case LL_LEASE_UNLCK:
2583 mutex_lock(&lli->lli_och_mutex);
2584 if (fd->fd_lease_och != NULL) {
2585 och = fd->fd_lease_och;
2586 fd->fd_lease_och = NULL;
2588 mutex_unlock(&lli->lli_och_mutex);
2593 fmode = och->och_flags;
2594 rc = ll_lease_close(och, inode, &lease_broken);
2598 rc = ll_lease_och_release(inode, file);
2605 RETURN(ll_lease_type_from_fmode(fmode));
2610 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
2612 /* apply for lease */
2613 och = ll_lease_open(inode, file, fmode, 0);
2615 RETURN(PTR_ERR(och));
2618 mutex_lock(&lli->lli_och_mutex);
2619 if (fd->fd_lease_och == NULL) {
2620 fd->fd_lease_och = och;
2623 mutex_unlock(&lli->lli_och_mutex);
2625 /* impossible now that only excl is supported for now */
2626 ll_lease_close(och, inode, &lease_broken);
2631 case LL_IOC_GET_LEASE: {
2632 struct ll_inode_info *lli = ll_i2info(inode);
2633 struct ldlm_lock *lock = NULL;
2636 mutex_lock(&lli->lli_och_mutex);
2637 if (fd->fd_lease_och != NULL) {
2638 struct obd_client_handle *och = fd->fd_lease_och;
2640 lock = ldlm_handle2lock(&och->och_lease_handle);
2642 lock_res_and_lock(lock);
2643 if (!ldlm_is_cancel(lock))
2644 fmode = och->och_flags;
2646 unlock_res_and_lock(lock);
2647 LDLM_LOCK_PUT(lock);
2650 mutex_unlock(&lli->lli_och_mutex);
2652 RETURN(ll_lease_type_from_fmode(fmode));
2654 case LL_IOC_HSM_IMPORT: {
2655 struct hsm_user_import *hui;
2661 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
2666 rc = ll_hsm_import(inode, file, hui);
2671 case LL_IOC_FUTIMES_3: {
2672 struct ll_futimes_3 lfu;
2674 if (copy_from_user(&lfu,
2675 (const struct ll_futimes_3 __user *)arg,
2679 RETURN(ll_file_futimes_3(file, &lfu));
2681 case LL_IOC_LADVISE: {
2682 struct llapi_ladvise_hdr *ladvise_hdr;
2685 int alloc_size = sizeof(*ladvise_hdr);
2688 OBD_ALLOC_PTR(ladvise_hdr);
2689 if (ladvise_hdr == NULL)
2692 if (copy_from_user(ladvise_hdr,
2693 (const struct llapi_ladvise_hdr __user *)arg,
2695 GOTO(out_ladvise, rc = -EFAULT);
2697 if (ladvise_hdr->lah_magic != LADVISE_MAGIC ||
2698 ladvise_hdr->lah_count < 1)
2699 GOTO(out_ladvise, rc = -EINVAL);
2701 num_advise = ladvise_hdr->lah_count;
2702 if (num_advise >= LAH_COUNT_MAX)
2703 GOTO(out_ladvise, rc = -EFBIG);
2705 OBD_FREE_PTR(ladvise_hdr);
2706 alloc_size = offsetof(typeof(*ladvise_hdr),
2707 lah_advise[num_advise]);
2708 OBD_ALLOC(ladvise_hdr, alloc_size);
2709 if (ladvise_hdr == NULL)
2713 * TODO: submit multiple advices to one server in a single RPC
2715 if (copy_from_user(ladvise_hdr,
2716 (const struct llapi_ladvise_hdr __user *)arg,
2718 GOTO(out_ladvise, rc = -EFAULT);
2720 for (i = 0; i < num_advise; i++) {
2721 rc = ll_ladvise(inode, file, ladvise_hdr->lah_flags,
2722 &ladvise_hdr->lah_advise[i]);
2728 OBD_FREE(ladvise_hdr, alloc_size);
2735 ll_iocontrol_call(inode, file, cmd, arg, &err))
2738 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
2739 (void __user *)arg));
2744 #ifndef HAVE_FILE_LLSEEK_SIZE
2745 static inline loff_t
2746 llseek_execute(struct file *file, loff_t offset, loff_t maxsize)
2748 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
2750 if (offset > maxsize)
2753 if (offset != file->f_pos) {
2754 file->f_pos = offset;
2755 file->f_version = 0;
2761 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
2762 loff_t maxsize, loff_t eof)
2764 struct inode *inode = file_inode(file);
2772 * Here we special-case the lseek(fd, 0, SEEK_CUR)
2773 * position-querying operation. Avoid rewriting the "same"
2774 * f_pos value back to the file because a concurrent read(),
2775 * write() or lseek() might have altered it
2780 * f_lock protects against read/modify/write race with other
2781 * SEEK_CURs. Note that parallel writes and reads behave
2785 offset = llseek_execute(file, file->f_pos + offset, maxsize);
2786 inode_unlock(inode);
2790 * In the generic case the entire file is data, so as long as
2791 * offset isn't at the end of the file then the offset is data.
2798 * There is a virtual hole at the end of the file, so as long as
2799 * offset isn't i_size or larger, return i_size.
2807 return llseek_execute(file, offset, maxsize);
2811 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
2813 struct inode *inode = file_inode(file);
2814 loff_t retval, eof = 0;
2817 retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
2818 (origin == SEEK_CUR) ? file->f_pos : 0);
2819 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
2820 PFID(ll_inode2fid(inode)), inode, retval, retval,
2822 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
2824 if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
2825 retval = ll_glimpse_size(inode);
2828 eof = i_size_read(inode);
2831 retval = ll_generic_file_llseek_size(file, offset, origin,
2832 ll_file_maxbytes(inode), eof);
2836 static int ll_flush(struct file *file, fl_owner_t id)
2838 struct inode *inode = file_inode(file);
2839 struct ll_inode_info *lli = ll_i2info(inode);
2840 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2843 LASSERT(!S_ISDIR(inode->i_mode));
2845 /* catch async errors that were recorded back when async writeback
2846 * failed for pages in this mapping. */
2847 rc = lli->lli_async_rc;
2848 lli->lli_async_rc = 0;
2849 if (lli->lli_clob != NULL) {
2850 err = lov_read_and_clear_async_rc(lli->lli_clob);
2855 /* The application has been told write failure already.
2856 * Do not report failure again. */
2857 if (fd->fd_write_failed)
2859 return rc ? -EIO : 0;
2863 * Called to make sure a portion of file has been written out.
2864 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
2866 * Return how many pages have been written.
2868 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
2869 enum cl_fsync_mode mode, int ignore_layout)
2873 struct cl_fsync_io *fio;
2878 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2879 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2882 env = cl_env_get(&refcheck);
2884 RETURN(PTR_ERR(env));
2886 io = vvp_env_thread_io(env);
2887 io->ci_obj = ll_i2info(inode)->lli_clob;
2888 io->ci_ignore_layout = ignore_layout;
2890 /* initialize parameters for sync */
2891 fio = &io->u.ci_fsync;
2892 fio->fi_start = start;
2894 fio->fi_fid = ll_inode2fid(inode);
2895 fio->fi_mode = mode;
2896 fio->fi_nr_written = 0;
2898 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2899 result = cl_io_loop(env, io);
2901 result = io->ci_result;
2903 result = fio->fi_nr_written;
2904 cl_io_fini(env, io);
2905 cl_env_put(env, &refcheck);
2911 * When dentry is provided (the 'else' case), file_dentry() may be
2912 * null and dentry must be used directly rather than pulled from
2913 * file_dentry() as is done otherwise.
2916 #ifdef HAVE_FILE_FSYNC_4ARGS
2917 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2919 struct dentry *dentry = file_dentry(file);
2920 #elif defined(HAVE_FILE_FSYNC_2ARGS)
2921 int ll_fsync(struct file *file, int datasync)
2923 struct dentry *dentry = file_dentry(file);
2925 loff_t end = LLONG_MAX;
2927 int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
2930 loff_t end = LLONG_MAX;
2932 struct inode *inode = dentry->d_inode;
2933 struct ll_inode_info *lli = ll_i2info(inode);
2934 struct ptlrpc_request *req;
2938 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2939 PFID(ll_inode2fid(inode)), inode);
2940 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2942 #ifdef HAVE_FILE_FSYNC_4ARGS
2943 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2946 /* fsync's caller has already called _fdata{sync,write}, we want
2947 * that IO to finish before calling the osc and mdc sync methods */
2948 rc = filemap_fdatawait(inode->i_mapping);
2951 /* catch async errors that were recorded back when async writeback
2952 * failed for pages in this mapping. */
2953 if (!S_ISDIR(inode->i_mode)) {
2954 err = lli->lli_async_rc;
2955 lli->lli_async_rc = 0;
2958 if (lli->lli_clob != NULL) {
2959 err = lov_read_and_clear_async_rc(lli->lli_clob);
2965 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
2969 ptlrpc_req_finished(req);
2971 if (S_ISREG(inode->i_mode)) {
2972 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2974 err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
2975 if (rc == 0 && err < 0)
2978 fd->fd_write_failed = true;
2980 fd->fd_write_failed = false;
2983 #ifdef HAVE_FILE_FSYNC_4ARGS
2984 inode_unlock(inode);
2990 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2992 struct inode *inode = file_inode(file);
2993 struct ll_sb_info *sbi = ll_i2sbi(inode);
2994 struct ldlm_enqueue_info einfo = {
2995 .ei_type = LDLM_FLOCK,
2996 .ei_cb_cp = ldlm_flock_completion_ast,
2997 .ei_cbdata = file_lock,
2999 struct md_op_data *op_data;
3000 struct lustre_handle lockh = { 0 };
3001 union ldlm_policy_data flock = { { 0 } };
3002 int fl_type = file_lock->fl_type;
3008 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
3009 PFID(ll_inode2fid(inode)), file_lock);
3011 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
3013 if (file_lock->fl_flags & FL_FLOCK) {
3014 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
3015 /* flocks are whole-file locks */
3016 flock.l_flock.end = OFFSET_MAX;
3017 /* For flocks owner is determined by the local file desctiptor*/
3018 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
3019 } else if (file_lock->fl_flags & FL_POSIX) {
3020 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
3021 flock.l_flock.start = file_lock->fl_start;
3022 flock.l_flock.end = file_lock->fl_end;
3026 flock.l_flock.pid = file_lock->fl_pid;
3028 /* Somewhat ugly workaround for svc lockd.
3029 * lockd installs custom fl_lmops->lm_compare_owner that checks
3030 * for the fl_owner to be the same (which it always is on local node
3031 * I guess between lockd processes) and then compares pid.
3032 * As such we assign pid to the owner field to make it all work,
3033 * conflict with normal locks is unlikely since pid space and
3034 * pointer space for current->files are not intersecting */
3035 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
3036 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
3040 einfo.ei_mode = LCK_PR;
3043 /* An unlock request may or may not have any relation to
3044 * existing locks so we may not be able to pass a lock handle
3045 * via a normal ldlm_lock_cancel() request. The request may even
3046 * unlock a byte range in the middle of an existing lock. In
3047 * order to process an unlock request we need all of the same
3048 * information that is given with a normal read or write record
3049 * lock request. To avoid creating another ldlm unlock (cancel)
3050 * message we'll treat a LCK_NL flock request as an unlock. */
3051 einfo.ei_mode = LCK_NL;
3054 einfo.ei_mode = LCK_PW;
3057 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
3072 flags = LDLM_FL_BLOCK_NOWAIT;
3078 flags = LDLM_FL_TEST_LOCK;
3081 CERROR("unknown fcntl lock command: %d\n", cmd);
3085 /* Save the old mode so that if the mode in the lock changes we
3086 * can decrement the appropriate reader or writer refcount. */
3087 file_lock->fl_type = einfo.ei_mode;
3089 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3090 LUSTRE_OPC_ANY, NULL);
3091 if (IS_ERR(op_data))
3092 RETURN(PTR_ERR(op_data));
3094 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
3095 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
3096 flock.l_flock.pid, flags, einfo.ei_mode,
3097 flock.l_flock.start, flock.l_flock.end);
3099 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
3102 /* Restore the file lock type if not TEST lock. */
3103 if (!(flags & LDLM_FL_TEST_LOCK))
3104 file_lock->fl_type = fl_type;
3106 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
3107 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
3108 !(flags & LDLM_FL_TEST_LOCK))
3109 rc2 = locks_lock_file_wait(file, file_lock);
3111 if ((file_lock->fl_flags & FL_FLOCK) &&
3112 (rc == 0 || file_lock->fl_type == F_UNLCK))
3113 rc2 = flock_lock_file_wait(file, file_lock);
3114 if ((file_lock->fl_flags & FL_POSIX) &&
3115 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
3116 !(flags & LDLM_FL_TEST_LOCK))
3117 rc2 = posix_lock_file_wait(file, file_lock);
3118 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
3120 if (rc2 && file_lock->fl_type != F_UNLCK) {
3121 einfo.ei_mode = LCK_NL;
3122 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
3127 ll_finish_md_op_data(op_data);
3132 int ll_get_fid_by_name(struct inode *parent, const char *name,
3133 int namelen, struct lu_fid *fid,
3134 struct inode **inode)
3136 struct md_op_data *op_data = NULL;
3137 struct mdt_body *body;
3138 struct ptlrpc_request *req;
3142 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
3143 LUSTRE_OPC_ANY, NULL);
3144 if (IS_ERR(op_data))
3145 RETURN(PTR_ERR(op_data));
3147 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
3148 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
3149 ll_finish_md_op_data(op_data);
3153 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3155 GOTO(out_req, rc = -EFAULT);
3157 *fid = body->mbo_fid1;
3160 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
3162 ptlrpc_req_finished(req);
3166 int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
3167 const char *name, int namelen)
3169 struct dentry *dchild = NULL;
3170 struct inode *child_inode = NULL;
3171 struct md_op_data *op_data;
3172 struct ptlrpc_request *request = NULL;
3173 struct obd_client_handle *och = NULL;
3175 struct mdt_body *body;
3177 __u64 data_version = 0;
3180 CDEBUG(D_VFSTRACE, "migrate %s under "DFID" to MDT%04x\n",
3181 name, PFID(ll_inode2fid(parent)), mdtidx);
3183 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
3184 0, LUSTRE_OPC_ANY, NULL);
3185 if (IS_ERR(op_data))
3186 RETURN(PTR_ERR(op_data));
3188 /* Get child FID first */
3189 qstr.hash = full_name_hash(name, namelen);
3192 dchild = d_lookup(file_dentry(file), &qstr);
3193 if (dchild != NULL) {
3194 if (dchild->d_inode != NULL)
3195 child_inode = igrab(dchild->d_inode);
3199 if (child_inode == NULL) {
3200 rc = ll_get_fid_by_name(parent, name, namelen,
3201 &op_data->op_fid3, &child_inode);
3206 if (child_inode == NULL)
3207 GOTO(out_free, rc = -EINVAL);
3210 * lfs migrate command needs to be blocked on the client
3211 * by checking the migrate FID against the FID of the
3214 if (child_inode == parent->i_sb->s_root->d_inode)
3215 GOTO(out_iput, rc = -EINVAL);
3217 inode_lock(child_inode);
3218 op_data->op_fid3 = *ll_inode2fid(child_inode);
3219 if (!fid_is_sane(&op_data->op_fid3)) {
3220 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
3221 ll_get_fsname(parent->i_sb, NULL, 0), name,
3222 PFID(&op_data->op_fid3));
3223 GOTO(out_unlock, rc = -EINVAL);
3226 rc = ll_get_mdt_idx_by_fid(ll_i2sbi(parent), &op_data->op_fid3);
3228 GOTO(out_unlock, rc);
3231 CDEBUG(D_INFO, "%s: "DFID" is already on MDT%04x\n", name,
3232 PFID(&op_data->op_fid3), mdtidx);
3233 GOTO(out_unlock, rc = 0);
3236 if (S_ISREG(child_inode->i_mode)) {
3237 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
3241 GOTO(out_unlock, rc);
3244 rc = ll_data_version(child_inode, &data_version,
3247 GOTO(out_close, rc);
3249 op_data->op_handle = och->och_fh;
3250 op_data->op_data = och->och_mod;
3251 op_data->op_data_version = data_version;
3252 op_data->op_lease_handle = och->och_lease_handle;
3253 op_data->op_bias |= MDS_RENAME_MIGRATE;
3256 op_data->op_mds = mdtidx;
3257 op_data->op_cli_flags = CLI_MIGRATE;
3258 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name,
3259 namelen, name, namelen, &request);
3261 LASSERT(request != NULL);
3262 ll_update_times(request, parent);
3264 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
3265 LASSERT(body != NULL);
3267 /* If the server does release layout lock, then we cleanup
3268 * the client och here, otherwise release it in out_close: */
3270 body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
3271 obd_mod_put(och->och_mod);
3272 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
3274 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
3280 if (request != NULL) {
3281 ptlrpc_req_finished(request);
3285 /* Try again if the file layout has changed. */
3286 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
3290 if (och != NULL) /* close the file */
3291 ll_lease_close(och, child_inode, NULL);
3293 clear_nlink(child_inode);
3295 inode_unlock(child_inode);
3299 ll_finish_md_op_data(op_data);
3304 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
3312 * test if some locks matching bits and l_req_mode are acquired
3313 * - bits can be in different locks
3314 * - if found clear the common lock bits in *bits
3315 * - the bits not found, are kept in *bits
3317 * \param bits [IN] searched lock bits [IN]
3318 * \param l_req_mode [IN] searched lock mode
3319 * \retval boolean, true iff all bits are found
3321 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
3323 struct lustre_handle lockh;
3324 union ldlm_policy_data policy;
3325 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
3326 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
3335 fid = &ll_i2info(inode)->lli_fid;
3336 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
3337 ldlm_lockname[mode]);
3339 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
3340 for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
3341 policy.l_inodebits.bits = *bits & (1 << i);
3342 if (policy.l_inodebits.bits == 0)
3345 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
3346 &policy, mode, &lockh)) {
3347 struct ldlm_lock *lock;
3349 lock = ldlm_handle2lock(&lockh);
3352 ~(lock->l_policy_data.l_inodebits.bits);
3353 LDLM_LOCK_PUT(lock);
3355 *bits &= ~policy.l_inodebits.bits;
3362 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
3363 struct lustre_handle *lockh, __u64 flags,
3364 enum ldlm_mode mode)
3366 union ldlm_policy_data policy = { .l_inodebits = { bits } };
3371 fid = &ll_i2info(inode)->lli_fid;
3372 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
3374 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
3375 fid, LDLM_IBITS, &policy, mode, lockh);
3380 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
3382 /* Already unlinked. Just update nlink and return success */
3383 if (rc == -ENOENT) {
3385 /* If it is striped directory, and there is bad stripe
3386 * Let's revalidate the dentry again, instead of returning
3388 if (S_ISDIR(inode->i_mode) &&
3389 ll_i2info(inode)->lli_lsm_md != NULL)
3392 /* This path cannot be hit for regular files unless in
3393 * case of obscure races, so no need to to validate
3395 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
3397 } else if (rc != 0) {
3398 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
3399 "%s: revalidate FID "DFID" error: rc = %d\n",
3400 ll_get_fsname(inode->i_sb, NULL, 0),
3401 PFID(ll_inode2fid(inode)), rc);
3407 static int __ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
3409 struct inode *inode = dentry->d_inode;
3410 struct ptlrpc_request *req = NULL;
3411 struct obd_export *exp;
3415 LASSERT(inode != NULL);
3417 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
3418 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
3420 exp = ll_i2mdexp(inode);
3422 /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
3423 * But under CMD case, it caused some lock issues, should be fixed
3424 * with new CMD ibits lock. See bug 12718 */
3425 if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) {
3426 struct lookup_intent oit = { .it_op = IT_GETATTR };
3427 struct md_op_data *op_data;
3429 if (ibits == MDS_INODELOCK_LOOKUP)
3430 oit.it_op = IT_LOOKUP;
3432 /* Call getattr by fid, so do not provide name at all. */
3433 op_data = ll_prep_md_op_data(NULL, dentry->d_inode,
3434 dentry->d_inode, NULL, 0, 0,
3435 LUSTRE_OPC_ANY, NULL);
3436 if (IS_ERR(op_data))
3437 RETURN(PTR_ERR(op_data));
3439 rc = md_intent_lock(exp, op_data, &oit, &req,
3440 &ll_md_blocking_ast, 0);
3441 ll_finish_md_op_data(op_data);
3443 rc = ll_inode_revalidate_fini(inode, rc);
3447 rc = ll_revalidate_it_finish(req, &oit, dentry);
3449 ll_intent_release(&oit);
3453 /* Unlinked? Unhash dentry, so it is not picked up later by
3454 do_lookup() -> ll_revalidate_it(). We cannot use d_drop
3455 here to preserve get_cwd functionality on 2.6.
3457 if (!dentry->d_inode->i_nlink) {
3458 ll_lock_dcache(inode);
3459 d_lustre_invalidate(dentry, 0);
3460 ll_unlock_dcache(inode);
3463 ll_lookup_finish_locks(&oit, dentry);
3464 } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
3465 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
3466 u64 valid = OBD_MD_FLGETATTR;
3467 struct md_op_data *op_data;
3470 if (S_ISREG(inode->i_mode)) {
3471 rc = ll_get_default_mdsize(sbi, &ealen);
3474 valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
3477 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
3478 0, ealen, LUSTRE_OPC_ANY,
3480 if (IS_ERR(op_data))
3481 RETURN(PTR_ERR(op_data));
3483 op_data->op_valid = valid;
3484 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
3485 ll_finish_md_op_data(op_data);
3487 rc = ll_inode_revalidate_fini(inode, rc);
3491 rc = ll_prep_inode(&inode, req, NULL, NULL);
3494 ptlrpc_req_finished(req);
3498 static int ll_merge_md_attr(struct inode *inode)
3500 struct cl_attr attr = { 0 };
3503 LASSERT(ll_i2info(inode)->lli_lsm_md != NULL);
3504 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
3505 &attr, ll_md_blocking_ast);
3509 set_nlink(inode, attr.cat_nlink);
3510 inode->i_blocks = attr.cat_blocks;
3511 i_size_write(inode, attr.cat_size);
3513 ll_i2info(inode)->lli_atime = attr.cat_atime;
3514 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
3515 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
3521 ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
3523 struct inode *inode = dentry->d_inode;
3527 rc = __ll_inode_revalidate(dentry, ibits);
3531 /* if object isn't regular file, don't validate size */
3532 if (!S_ISREG(inode->i_mode)) {
3533 if (S_ISDIR(inode->i_mode) &&
3534 ll_i2info(inode)->lli_lsm_md != NULL) {
3535 rc = ll_merge_md_attr(inode);
3540 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_atime;
3541 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_mtime;
3542 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_ctime;
3544 /* In case of restore, the MDT has the right size and has
3545 * already send it back without granting the layout lock,
3546 * inode is up-to-date so glimpse is useless.
3547 * Also to glimpse we need the layout, in case of a running
3548 * restore the MDT holds the layout lock so the glimpse will
3549 * block up to the end of restore (getattr will block)
3551 if (!ll_file_test_flag(ll_i2info(inode), LLIF_FILE_RESTORING))
3552 rc = ll_glimpse_size(inode);
3557 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
3559 struct inode *inode = de->d_inode;
3560 struct ll_sb_info *sbi = ll_i2sbi(inode);
3561 struct ll_inode_info *lli = ll_i2info(inode);
3564 res = ll_inode_revalidate(de, MDS_INODELOCK_UPDATE |
3565 MDS_INODELOCK_LOOKUP);
3566 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
3571 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
3573 stat->dev = inode->i_sb->s_dev;
3574 if (ll_need_32bit_api(sbi))
3575 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
3577 stat->ino = inode->i_ino;
3578 stat->mode = inode->i_mode;
3579 stat->uid = inode->i_uid;
3580 stat->gid = inode->i_gid;
3581 stat->rdev = inode->i_rdev;
3582 stat->atime = inode->i_atime;
3583 stat->mtime = inode->i_mtime;
3584 stat->ctime = inode->i_ctime;
3585 stat->blksize = 1 << inode->i_blkbits;
3587 stat->nlink = inode->i_nlink;
3588 stat->size = i_size_read(inode);
3589 stat->blocks = inode->i_blocks;
3594 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3595 __u64 start, __u64 len)
3599 struct fiemap *fiemap;
3600 unsigned int extent_count = fieinfo->fi_extents_max;
3602 num_bytes = sizeof(*fiemap) + (extent_count *
3603 sizeof(struct fiemap_extent));
3604 OBD_ALLOC_LARGE(fiemap, num_bytes);
3609 fiemap->fm_flags = fieinfo->fi_flags;
3610 fiemap->fm_extent_count = fieinfo->fi_extents_max;
3611 fiemap->fm_start = start;
3612 fiemap->fm_length = len;
3613 if (extent_count > 0 &&
3614 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
3615 sizeof(struct fiemap_extent)) != 0)
3616 GOTO(out, rc = -EFAULT);
3618 rc = ll_do_fiemap(inode, fiemap, num_bytes);
3620 fieinfo->fi_flags = fiemap->fm_flags;
3621 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
3622 if (extent_count > 0 &&
3623 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
3624 fiemap->fm_mapped_extents *
3625 sizeof(struct fiemap_extent)) != 0)
3626 GOTO(out, rc = -EFAULT);
3628 OBD_FREE_LARGE(fiemap, num_bytes);
3632 struct posix_acl *ll_get_acl(struct inode *inode, int type)
3634 struct ll_inode_info *lli = ll_i2info(inode);
3635 struct posix_acl *acl = NULL;
3638 spin_lock(&lli->lli_lock);
3639 /* VFS' acl_permission_check->check_acl will release the refcount */
3640 acl = posix_acl_dup(lli->lli_posix_acl);
3641 spin_unlock(&lli->lli_lock);
3646 #ifndef HAVE_GENERIC_PERMISSION_2ARGS
3648 # ifdef HAVE_GENERIC_PERMISSION_4ARGS
3649 ll_check_acl(struct inode *inode, int mask, unsigned int flags)
3651 ll_check_acl(struct inode *inode, int mask)
3654 # ifdef CONFIG_FS_POSIX_ACL
3655 struct posix_acl *acl;
3659 # ifdef HAVE_GENERIC_PERMISSION_4ARGS
3660 if (flags & IPERM_FLAG_RCU)
3663 acl = ll_get_acl(inode, ACL_TYPE_ACCESS);
3668 rc = posix_acl_permission(inode, acl, mask);
3669 posix_acl_release(acl);
3672 # else /* !CONFIG_FS_POSIX_ACL */
3674 # endif /* CONFIG_FS_POSIX_ACL */
3676 #endif /* HAVE_GENERIC_PERMISSION_2ARGS */
3678 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
3679 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
3681 # ifdef HAVE_INODE_PERMISION_2ARGS
3682 int ll_inode_permission(struct inode *inode, int mask)
3684 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
3689 struct ll_sb_info *sbi;
3690 struct root_squash_info *squash;
3691 struct cred *cred = NULL;
3692 const struct cred *old_cred = NULL;
3694 bool squash_id = false;
3697 #ifdef MAY_NOT_BLOCK
3698 if (mask & MAY_NOT_BLOCK)
3700 #elif defined(HAVE_GENERIC_PERMISSION_4ARGS)
3701 if (flags & IPERM_FLAG_RCU)
3705 /* as root inode are NOT getting validated in lookup operation,
3706 * need to do it before permission check. */
3708 if (inode == inode->i_sb->s_root->d_inode) {
3709 rc = __ll_inode_revalidate(inode->i_sb->s_root,
3710 MDS_INODELOCK_LOOKUP);
3715 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
3716 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
3718 /* squash fsuid/fsgid if needed */
3719 sbi = ll_i2sbi(inode);
3720 squash = &sbi->ll_squash;
3721 if (unlikely(squash->rsi_uid != 0 &&
3722 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
3723 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
3727 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
3728 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
3729 squash->rsi_uid, squash->rsi_gid);
3731 /* update current process's credentials
3732 * and FS capability */
3733 cred = prepare_creds();
3737 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
3738 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
3739 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
3740 if ((1 << cap) & CFS_CAP_FS_MASK)
3741 cap_lower(cred->cap_effective, cap);
3743 old_cred = override_creds(cred);
3746 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM, 1);
3747 rc = ll_generic_permission(inode, mask, flags, ll_check_acl);
3748 /* restore current process's credentials and FS capability */
3750 revert_creds(old_cred);
3757 /* -o localflock - only provides locally consistent flock locks */
3758 struct file_operations ll_file_operations = {
3759 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
3760 # ifdef HAVE_SYNC_READ_WRITE
3761 .read = new_sync_read,
3762 .write = new_sync_write,
3764 .read_iter = ll_file_read_iter,
3765 .write_iter = ll_file_write_iter,
3766 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3767 .read = ll_file_read,
3768 .aio_read = ll_file_aio_read,
3769 .write = ll_file_write,
3770 .aio_write = ll_file_aio_write,
3771 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3772 .unlocked_ioctl = ll_file_ioctl,
3773 .open = ll_file_open,
3774 .release = ll_file_release,
3775 .mmap = ll_file_mmap,
3776 .llseek = ll_file_seek,
3777 .splice_read = ll_file_splice_read,
3782 struct file_operations ll_file_operations_flock = {
3783 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
3784 # ifdef HAVE_SYNC_READ_WRITE
3785 .read = new_sync_read,
3786 .write = new_sync_write,
3787 # endif /* HAVE_SYNC_READ_WRITE */
3788 .read_iter = ll_file_read_iter,
3789 .write_iter = ll_file_write_iter,
3790 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3791 .read = ll_file_read,
3792 .aio_read = ll_file_aio_read,
3793 .write = ll_file_write,
3794 .aio_write = ll_file_aio_write,
3795 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3796 .unlocked_ioctl = ll_file_ioctl,
3797 .open = ll_file_open,
3798 .release = ll_file_release,
3799 .mmap = ll_file_mmap,
3800 .llseek = ll_file_seek,
3801 .splice_read = ll_file_splice_read,
3804 .flock = ll_file_flock,
3805 .lock = ll_file_flock
3808 /* These are for -o noflock - to return ENOSYS on flock calls */
3809 struct file_operations ll_file_operations_noflock = {
3810 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
3811 # ifdef HAVE_SYNC_READ_WRITE
3812 .read = new_sync_read,
3813 .write = new_sync_write,
3814 # endif /* HAVE_SYNC_READ_WRITE */
3815 .read_iter = ll_file_read_iter,
3816 .write_iter = ll_file_write_iter,
3817 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3818 .read = ll_file_read,
3819 .aio_read = ll_file_aio_read,
3820 .write = ll_file_write,
3821 .aio_write = ll_file_aio_write,
3822 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
3823 .unlocked_ioctl = ll_file_ioctl,
3824 .open = ll_file_open,
3825 .release = ll_file_release,
3826 .mmap = ll_file_mmap,
3827 .llseek = ll_file_seek,
3828 .splice_read = ll_file_splice_read,
3831 .flock = ll_file_noflock,
3832 .lock = ll_file_noflock
3835 struct inode_operations ll_file_inode_operations = {
3836 .setattr = ll_setattr,
3837 .getattr = ll_getattr,
3838 .permission = ll_inode_permission,
3839 .setxattr = ll_setxattr,
3840 .getxattr = ll_getxattr,
3841 .listxattr = ll_listxattr,
3842 .removexattr = ll_removexattr,
3843 .fiemap = ll_fiemap,
3844 #ifdef HAVE_IOP_GET_ACL
3845 .get_acl = ll_get_acl,
3849 /* dynamic ioctl number support routins */
3850 static struct llioc_ctl_data {
3851 struct rw_semaphore ioc_sem;
3852 struct list_head ioc_head;
3854 __RWSEM_INITIALIZER(llioc.ioc_sem),
3855 LIST_HEAD_INIT(llioc.ioc_head)
3860 struct list_head iocd_list;
3861 unsigned int iocd_size;
3862 llioc_callback_t iocd_cb;
3863 unsigned int iocd_count;
3864 unsigned int iocd_cmd[0];
3867 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
3870 struct llioc_data *in_data = NULL;
3873 if (cb == NULL || cmd == NULL ||
3874 count > LLIOC_MAX_CMD || count < 0)
3877 size = sizeof(*in_data) + count * sizeof(unsigned int);
3878 OBD_ALLOC(in_data, size);
3879 if (in_data == NULL)
3882 memset(in_data, 0, sizeof(*in_data));
3883 in_data->iocd_size = size;
3884 in_data->iocd_cb = cb;
3885 in_data->iocd_count = count;
3886 memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
3888 down_write(&llioc.ioc_sem);
3889 list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
3890 up_write(&llioc.ioc_sem);
3895 void ll_iocontrol_unregister(void *magic)
3897 struct llioc_data *tmp;
3902 down_write(&llioc.ioc_sem);
3903 list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
3905 unsigned int size = tmp->iocd_size;
3907 list_del(&tmp->iocd_list);
3908 up_write(&llioc.ioc_sem);
3910 OBD_FREE(tmp, size);
3914 up_write(&llioc.ioc_sem);
3916 CWARN("didn't find iocontrol register block with magic: %p\n", magic);
3919 EXPORT_SYMBOL(ll_iocontrol_register);
3920 EXPORT_SYMBOL(ll_iocontrol_unregister);
3922 static enum llioc_iter
3923 ll_iocontrol_call(struct inode *inode, struct file *file,
3924 unsigned int cmd, unsigned long arg, int *rcp)
3926 enum llioc_iter ret = LLIOC_CONT;
3927 struct llioc_data *data;
3928 int rc = -EINVAL, i;
3930 down_read(&llioc.ioc_sem);
3931 list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
3932 for (i = 0; i < data->iocd_count; i++) {
3933 if (cmd != data->iocd_cmd[i])
3936 ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
3940 if (ret == LLIOC_STOP)
3943 up_read(&llioc.ioc_sem);
3950 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
3952 struct ll_inode_info *lli = ll_i2info(inode);
3953 struct cl_object *obj = lli->lli_clob;
3962 env = cl_env_get(&refcheck);
3964 RETURN(PTR_ERR(env));
3966 rc = cl_conf_set(env, lli->lli_clob, conf);
3970 if (conf->coc_opc == OBJECT_CONF_SET) {
3971 struct ldlm_lock *lock = conf->coc_lock;
3972 struct cl_layout cl = {
3976 LASSERT(lock != NULL);
3977 LASSERT(ldlm_has_layout(lock));
3979 /* it can only be allowed to match after layout is
3980 * applied to inode otherwise false layout would be
3981 * seen. Applying layout shoud happen before dropping
3982 * the intent lock. */
3983 ldlm_lock_allow_match(lock);
3985 rc = cl_object_layout_get(env, obj, &cl);
3990 DFID": layout version change: %u -> %u\n",
3991 PFID(&lli->lli_fid), ll_layout_version_get(lli),
3993 ll_layout_version_set(lli, cl.cl_layout_gen);
3997 cl_env_put(env, &refcheck);
4002 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
4003 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
4006 struct ll_sb_info *sbi = ll_i2sbi(inode);
4007 struct ptlrpc_request *req;
4008 struct mdt_body *body;
4015 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
4016 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
4017 lock->l_lvb_data, lock->l_lvb_len);
4019 if (lock->l_lvb_data != NULL)
4022 /* if layout lock was granted right away, the layout is returned
4023 * within DLM_LVB of dlm reply; otherwise if the lock was ever
4024 * blocked and then granted via completion ast, we have to fetch
4025 * layout here. Please note that we can't use the LVB buffer in
4026 * completion AST because it doesn't have a large enough buffer */
4027 rc = ll_get_default_mdsize(sbi, &lmmsize);
4029 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
4030 OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
4035 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4037 GOTO(out, rc = -EPROTO);
4039 lmmsize = body->mbo_eadatasize;
4040 if (lmmsize == 0) /* empty layout */
4043 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
4045 GOTO(out, rc = -EFAULT);
4047 OBD_ALLOC_LARGE(lvbdata, lmmsize);
4048 if (lvbdata == NULL)
4049 GOTO(out, rc = -ENOMEM);
4051 memcpy(lvbdata, lmm, lmmsize);
4052 lock_res_and_lock(lock);
4053 if (unlikely(lock->l_lvb_data == NULL)) {
4054 lock->l_lvb_type = LVB_T_LAYOUT;
4055 lock->l_lvb_data = lvbdata;
4056 lock->l_lvb_len = lmmsize;
4059 unlock_res_and_lock(lock);
4062 OBD_FREE_LARGE(lvbdata, lmmsize);
4067 ptlrpc_req_finished(req);
4072 * Apply the layout to the inode. Layout lock is held and will be released
4075 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
4076 struct inode *inode)
4078 struct ll_inode_info *lli = ll_i2info(inode);
4079 struct ll_sb_info *sbi = ll_i2sbi(inode);
4080 struct ldlm_lock *lock;
4081 struct cl_object_conf conf;
4084 bool wait_layout = false;
4087 LASSERT(lustre_handle_is_used(lockh));
4089 lock = ldlm_handle2lock(lockh);
4090 LASSERT(lock != NULL);
4091 LASSERT(ldlm_has_layout(lock));
4093 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
4094 PFID(&lli->lli_fid), inode);
4096 /* in case this is a caching lock and reinstate with new inode */
4097 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
4099 lock_res_and_lock(lock);
4100 lvb_ready = ldlm_is_lvb_ready(lock);
4101 unlock_res_and_lock(lock);
4102 /* checking lvb_ready is racy but this is okay. The worst case is
4103 * that multi processes may configure the file on the same time. */
4108 rc = ll_layout_fetch(inode, lock);
4112 /* for layout lock, lmm is stored in lock's lvb.
4113 * lvb_data is immutable if the lock is held so it's safe to access it
4116 * set layout to file. Unlikely this will fail as old layout was
4117 * surely eliminated */
4118 memset(&conf, 0, sizeof conf);
4119 conf.coc_opc = OBJECT_CONF_SET;
4120 conf.coc_inode = inode;
4121 conf.coc_lock = lock;
4122 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
4123 conf.u.coc_layout.lb_len = lock->l_lvb_len;
4124 rc = ll_layout_conf(inode, &conf);
4126 /* refresh layout failed, need to wait */
4127 wait_layout = rc == -EBUSY;
4131 LDLM_LOCK_PUT(lock);
4132 ldlm_lock_decref(lockh, mode);
4134 /* wait for IO to complete if it's still being used. */
4136 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
4137 ll_get_fsname(inode->i_sb, NULL, 0),
4138 PFID(&lli->lli_fid), inode);
4140 memset(&conf, 0, sizeof conf);
4141 conf.coc_opc = OBJECT_CONF_WAIT;
4142 conf.coc_inode = inode;
4143 rc = ll_layout_conf(inode, &conf);
4147 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
4148 ll_get_fsname(inode->i_sb, NULL, 0),
4149 PFID(&lli->lli_fid), rc);
4154 static int ll_layout_refresh_locked(struct inode *inode)
4156 struct ll_inode_info *lli = ll_i2info(inode);
4157 struct ll_sb_info *sbi = ll_i2sbi(inode);
4158 struct md_op_data *op_data;
4159 struct lookup_intent it;
4160 struct lustre_handle lockh;
4161 enum ldlm_mode mode;
4162 struct ptlrpc_request *req;
4167 /* mostly layout lock is caching on the local side, so try to match
4168 * it before grabbing layout lock mutex. */
4169 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
4170 LCK_CR | LCK_CW | LCK_PR | LCK_PW);
4171 if (mode != 0) { /* hit cached lock */
4172 rc = ll_layout_lock_set(&lockh, mode, inode);
4179 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
4180 0, 0, LUSTRE_OPC_ANY, NULL);
4181 if (IS_ERR(op_data))
4182 RETURN(PTR_ERR(op_data));
4184 /* have to enqueue one */
4185 memset(&it, 0, sizeof(it));
4186 it.it_op = IT_LAYOUT;
4188 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
4189 ll_get_fsname(inode->i_sb, NULL, 0),
4190 PFID(&lli->lli_fid), inode);
4192 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
4193 &ll_md_blocking_ast, 0);
4194 if (it.it_request != NULL)
4195 ptlrpc_req_finished(it.it_request);
4196 it.it_request = NULL;
4198 ll_finish_md_op_data(op_data);
4200 mode = it.it_lock_mode;
4201 it.it_lock_mode = 0;
4202 ll_intent_drop_lock(&it);
4205 /* set lock data in case this is a new lock */
4206 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
4207 lockh.cookie = it.it_lock_handle;
4208 rc = ll_layout_lock_set(&lockh, mode, inode);
4217 * This function checks if there exists a LAYOUT lock on the client side,
4218 * or enqueues it if it doesn't have one in cache.
4220 * This function will not hold layout lock so it may be revoked any time after
4221 * this function returns. Any operations depend on layout should be redone
4224 * This function should be called before lov_io_init() to get an uptodate
4225 * layout version, the caller should save the version number and after IO
4226 * is finished, this function should be called again to verify that layout
4227 * is not changed during IO time.
4229 int ll_layout_refresh(struct inode *inode, __u32 *gen)
4231 struct ll_inode_info *lli = ll_i2info(inode);
4232 struct ll_sb_info *sbi = ll_i2sbi(inode);
4236 *gen = ll_layout_version_get(lli);
4237 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
4241 LASSERT(fid_is_sane(ll_inode2fid(inode)));
4242 LASSERT(S_ISREG(inode->i_mode));
4244 /* take layout lock mutex to enqueue layout lock exclusively. */
4245 mutex_lock(&lli->lli_layout_mutex);
4247 rc = ll_layout_refresh_locked(inode);
4251 *gen = ll_layout_version_get(lli);
4253 mutex_unlock(&lli->lli_layout_mutex);
4259 * This function send a restore request to the MDT
4261 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
4263 struct hsm_user_request *hur;
4267 len = sizeof(struct hsm_user_request) +
4268 sizeof(struct hsm_user_item);
4269 OBD_ALLOC(hur, len);
4273 hur->hur_request.hr_action = HUA_RESTORE;
4274 hur->hur_request.hr_archive_id = 0;
4275 hur->hur_request.hr_flags = 0;
4276 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
4277 sizeof(hur->hur_user_item[0].hui_fid));
4278 hur->hur_user_item[0].hui_extent.offset = offset;
4279 hur->hur_user_item[0].hui_extent.length = length;
4280 hur->hur_request.hr_itemcount = 1;
4281 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,