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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Author: Peter Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Andreas Dilger <adilger@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
39 #include <lustre_dlm.h>
40 #include <linux/pagemap.h>
41 #include <linux/file.h>
42 #include <linux/sched.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
45 #include <linux/falloc.h>
46 #include <linux/ktime.h>
48 #include <uapi/linux/lustre/lustre_ioctl.h>
49 #include <uapi/linux/llcrypt.h>
50 #include <lustre_swab.h>
52 #include "cl_object.h"
53 #include "llite_internal.h"
54 #include "vvp_internal.h"
57 struct inode *sp_inode;
62 __u64 pa_data_version;
68 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
70 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
73 static struct ll_file_data *ll_file_data_get(void)
75 struct ll_file_data *fd;
77 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
81 fd->fd_write_failed = false;
82 pcc_file_init(&fd->fd_pcc_file);
87 static void ll_file_data_put(struct ll_file_data *fd)
90 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
94 * Packs all the attributes into @op_data for the CLOSE rpc.
96 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
97 struct obd_client_handle *och)
101 ll_prep_md_op_data(op_data, inode, NULL, NULL,
102 0, 0, LUSTRE_OPC_ANY, NULL);
104 op_data->op_attr.ia_mode = inode->i_mode;
105 op_data->op_attr.ia_atime = inode->i_atime;
106 op_data->op_attr.ia_mtime = inode->i_mtime;
107 op_data->op_attr.ia_ctime = inode->i_ctime;
108 /* In case of encrypted file without the key, visible size was rounded
109 * up to next LUSTRE_ENCRYPTION_UNIT_SIZE, and clear text size was
110 * stored into lli_lazysize in ll_merge_attr(), so set proper file size
111 * now that we are closing.
113 if (llcrypt_require_key(inode) == -ENOKEY &&
114 ll_i2info(inode)->lli_attr_valid & OBD_MD_FLLAZYSIZE)
115 op_data->op_attr.ia_size = ll_i2info(inode)->lli_lazysize;
117 op_data->op_attr.ia_size = i_size_read(inode);
118 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
119 ATTR_MTIME | ATTR_MTIME_SET |
121 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
122 op_data->op_attr_blocks = inode->i_blocks;
123 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
124 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
125 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
126 op_data->op_open_handle = och->och_open_handle;
128 if (och->och_flags & FMODE_WRITE &&
129 test_and_clear_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags))
130 /* For HSM: if inode data has been modified, pack it so that
131 * MDT can set data dirty flag in the archive. */
132 op_data->op_bias |= MDS_DATA_MODIFIED;
138 * Perform a close, possibly with a bias.
139 * The meaning of "data" depends on the value of "bias".
141 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
142 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
145 static int ll_close_inode_openhandle(struct inode *inode,
146 struct obd_client_handle *och,
147 enum mds_op_bias bias, void *data)
149 struct obd_export *md_exp = ll_i2mdexp(inode);
150 const struct ll_inode_info *lli = ll_i2info(inode);
151 struct md_op_data *op_data;
152 struct ptlrpc_request *req = NULL;
156 if (class_exp2obd(md_exp) == NULL) {
157 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
158 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
162 OBD_ALLOC_PTR(op_data);
163 /* We leak openhandle and request here on error, but not much to be
164 * done in OOM case since app won't retry close on error either. */
166 GOTO(out, rc = -ENOMEM);
168 ll_prepare_close(inode, op_data, och);
170 case MDS_CLOSE_LAYOUT_MERGE:
171 /* merge blocks from the victim inode */
172 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
173 op_data->op_attr.ia_valid |= ATTR_SIZE;
174 op_data->op_xvalid |= OP_XVALID_BLOCKS;
176 case MDS_CLOSE_LAYOUT_SPLIT:
177 case MDS_CLOSE_LAYOUT_SWAP: {
178 struct split_param *sp = data;
180 LASSERT(data != NULL);
181 op_data->op_bias |= bias;
182 op_data->op_data_version = 0;
183 op_data->op_lease_handle = och->och_lease_handle;
184 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
185 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
186 op_data->op_mirror_id = sp->sp_mirror_id;
188 op_data->op_fid2 = *ll_inode2fid(data);
193 case MDS_CLOSE_RESYNC_DONE: {
194 struct ll_ioc_lease *ioc = data;
196 LASSERT(data != NULL);
197 op_data->op_attr_blocks +=
198 ioc->lil_count * op_data->op_attr_blocks;
199 op_data->op_attr.ia_valid |= ATTR_SIZE;
200 op_data->op_xvalid |= OP_XVALID_BLOCKS;
201 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
203 op_data->op_lease_handle = och->och_lease_handle;
204 op_data->op_data = &ioc->lil_ids[0];
205 op_data->op_data_size =
206 ioc->lil_count * sizeof(ioc->lil_ids[0]);
210 case MDS_PCC_ATTACH: {
211 struct pcc_param *param = data;
213 LASSERT(data != NULL);
214 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
215 op_data->op_archive_id = param->pa_archive_id;
216 op_data->op_data_version = param->pa_data_version;
217 op_data->op_lease_handle = och->och_lease_handle;
221 case MDS_HSM_RELEASE:
222 LASSERT(data != NULL);
223 op_data->op_bias |= MDS_HSM_RELEASE;
224 op_data->op_data_version = *(__u64 *)data;
225 op_data->op_lease_handle = och->och_lease_handle;
226 op_data->op_attr.ia_valid |= ATTR_SIZE;
227 op_data->op_xvalid |= OP_XVALID_BLOCKS;
231 LASSERT(data == NULL);
235 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
236 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
237 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
238 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
240 rc = md_close(md_exp, op_data, och->och_mod, &req);
241 if (rc != 0 && rc != -EINTR)
242 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
243 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
245 if (rc == 0 && op_data->op_bias & bias) {
246 struct mdt_body *body;
248 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
249 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
252 if (bias & MDS_PCC_ATTACH) {
253 struct pcc_param *param = data;
255 param->pa_layout_gen = body->mbo_layout_gen;
259 ll_finish_md_op_data(op_data);
263 md_clear_open_replay_data(md_exp, och);
264 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
267 ptlrpc_req_finished(req); /* This is close request */
271 int ll_md_real_close(struct inode *inode, fmode_t fmode)
273 struct ll_inode_info *lli = ll_i2info(inode);
274 struct obd_client_handle **och_p;
275 struct obd_client_handle *och;
280 if (fmode & FMODE_WRITE) {
281 och_p = &lli->lli_mds_write_och;
282 och_usecount = &lli->lli_open_fd_write_count;
283 } else if (fmode & FMODE_EXEC) {
284 och_p = &lli->lli_mds_exec_och;
285 och_usecount = &lli->lli_open_fd_exec_count;
287 LASSERT(fmode & FMODE_READ);
288 och_p = &lli->lli_mds_read_och;
289 och_usecount = &lli->lli_open_fd_read_count;
292 mutex_lock(&lli->lli_och_mutex);
293 if (*och_usecount > 0) {
294 /* There are still users of this handle, so skip
296 mutex_unlock(&lli->lli_och_mutex);
302 mutex_unlock(&lli->lli_och_mutex);
305 /* There might be a race and this handle may already
307 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
313 static int ll_md_close(struct inode *inode, struct file *file)
315 union ldlm_policy_data policy = {
316 .l_inodebits = { MDS_INODELOCK_OPEN },
318 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
319 struct ll_file_data *fd = file->private_data;
320 struct ll_inode_info *lli = ll_i2info(inode);
321 struct lustre_handle lockh;
322 enum ldlm_mode lockmode;
326 /* clear group lock, if present */
327 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
328 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
330 mutex_lock(&lli->lli_och_mutex);
331 if (fd->fd_lease_och != NULL) {
333 struct obd_client_handle *lease_och;
335 lease_och = fd->fd_lease_och;
336 fd->fd_lease_och = NULL;
337 mutex_unlock(&lli->lli_och_mutex);
339 /* Usually the lease is not released when the
340 * application crashed, we need to release here. */
341 rc = ll_lease_close(lease_och, inode, &lease_broken);
343 mutex_lock(&lli->lli_och_mutex);
345 CDEBUG_LIMIT(rc ? D_ERROR : D_INODE,
346 "Clean up lease "DFID" %d/%d\n",
347 PFID(&lli->lli_fid), rc, lease_broken);
350 if (fd->fd_och != NULL) {
351 struct obd_client_handle *och;
355 mutex_unlock(&lli->lli_och_mutex);
357 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
361 /* Let's see if we have good enough OPEN lock on the file and if
362 we can skip talking to MDS */
363 if (fd->fd_omode & FMODE_WRITE) {
365 LASSERT(lli->lli_open_fd_write_count);
366 lli->lli_open_fd_write_count--;
367 } else if (fd->fd_omode & FMODE_EXEC) {
369 LASSERT(lli->lli_open_fd_exec_count);
370 lli->lli_open_fd_exec_count--;
373 LASSERT(lli->lli_open_fd_read_count);
374 lli->lli_open_fd_read_count--;
376 mutex_unlock(&lli->lli_och_mutex);
378 /* LU-4398: do not cache write open lock if the file has exec bit */
379 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
380 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
381 LDLM_IBITS, &policy, lockmode, &lockh))
382 rc = ll_md_real_close(inode, fd->fd_omode);
385 file->private_data = NULL;
386 ll_file_data_put(fd);
391 /* While this returns an error code, fput() the caller does not, so we need
392 * to make every effort to clean up all of our state here. Also, applications
393 * rarely check close errors and even if an error is returned they will not
394 * re-try the close call.
396 int ll_file_release(struct inode *inode, struct file *file)
398 struct ll_file_data *fd;
399 struct ll_sb_info *sbi = ll_i2sbi(inode);
400 struct ll_inode_info *lli = ll_i2info(inode);
401 ktime_t kstart = ktime_get();
406 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
407 PFID(ll_inode2fid(inode)), inode);
409 fd = file->private_data;
412 /* The last ref on @file, maybe not the the owner pid of statahead,
413 * because parent and child process can share the same file handle. */
414 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
415 ll_deauthorize_statahead(inode, fd);
417 if (is_root_inode(inode)) {
418 file->private_data = NULL;
419 ll_file_data_put(fd);
423 pcc_file_release(inode, file);
425 if (!S_ISDIR(inode->i_mode)) {
426 if (lli->lli_clob != NULL)
427 lov_read_and_clear_async_rc(lli->lli_clob);
428 lli->lli_async_rc = 0;
431 lli->lli_close_fd_time = ktime_get();
433 rc = ll_md_close(inode, file);
435 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
436 libcfs_debug_dumplog();
439 if (!rc && !is_root_inode(inode))
440 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
441 ktime_us_delta(ktime_get(), kstart));
445 static inline int ll_dom_readpage(void *data, struct page *page)
447 struct niobuf_local *lnb = data;
451 struct inode *inode = page2inode(page);
453 kaddr = kmap_atomic(page);
454 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
455 if (lnb->lnb_len < PAGE_SIZE)
456 memset(kaddr + lnb->lnb_len, 0,
457 PAGE_SIZE - lnb->lnb_len);
458 flush_dcache_page(page);
459 SetPageUptodate(page);
460 kunmap_atomic(kaddr);
462 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
463 if (!llcrypt_has_encryption_key(inode))
464 CDEBUG(D_SEC, "no enc key for "DFID"\n",
465 PFID(ll_inode2fid(inode)));
467 unsigned int offs = 0;
469 while (offs < PAGE_SIZE) {
470 /* decrypt only if page is not empty */
471 if (memcmp(page_address(page) + offs,
472 page_address(ZERO_PAGE(0)),
473 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
476 rc = llcrypt_decrypt_pagecache_blocks(page,
477 LUSTRE_ENCRYPTION_UNIT_SIZE,
482 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
491 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
495 struct ll_inode_info *lli = ll_i2info(inode);
496 struct cl_object *obj = lli->lli_clob;
497 struct address_space *mapping = inode->i_mapping;
499 struct niobuf_remote *rnb;
500 struct mdt_body *body;
502 unsigned long index, start;
503 struct niobuf_local lnb;
512 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
516 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
517 if (rnb == NULL || rnb->rnb_len == 0)
520 /* LU-11595: Server may return whole file and that is OK always or
521 * it may return just file tail and its offset must be aligned with
522 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
523 * smaller then offset may be not aligned and that data is just ignored.
525 if (rnb->rnb_offset & ~PAGE_MASK)
528 /* Server returns whole file or just file tail if it fills in reply
529 * buffer, in both cases total size should be equal to the file size.
531 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
532 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
533 !(inode && IS_ENCRYPTED(inode))) {
534 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
535 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
536 rnb->rnb_len, body->mbo_dom_size);
540 env = cl_env_get(&refcheck);
543 io = vvp_env_thread_io(env);
545 io->ci_ignore_layout = 1;
546 rc = cl_io_init(env, io, CIT_MISC, obj);
550 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
551 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
553 data = (char *)rnb + sizeof(*rnb);
555 lnb.lnb_file_offset = rnb->rnb_offset;
556 start = lnb.lnb_file_offset >> PAGE_SHIFT;
558 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
559 lnb.lnb_page_offset = 0;
561 struct cl_page *page;
563 lnb.lnb_data = data + (index << PAGE_SHIFT);
564 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
565 if (lnb.lnb_len > PAGE_SIZE)
566 lnb.lnb_len = PAGE_SIZE;
568 vmpage = read_cache_page(mapping, index + start,
569 ll_dom_readpage, &lnb);
570 if (IS_ERR(vmpage)) {
571 CWARN("%s: cannot fill page %lu for "DFID
572 " with data: rc = %li\n",
573 ll_i2sbi(inode)->ll_fsname, index + start,
574 PFID(lu_object_fid(&obj->co_lu)),
579 if (vmpage->mapping == NULL) {
582 /* page was truncated */
585 /* attach VM page to CL page cache */
586 page = cl_page_find(env, obj, vmpage->index, vmpage,
589 ClearPageUptodate(vmpage);
594 cl_page_export(env, page, 1);
595 cl_page_put(env, page);
599 } while (rnb->rnb_len > (index << PAGE_SHIFT));
603 cl_env_put(env, &refcheck);
608 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
609 struct lookup_intent *itp)
611 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
612 struct dentry *parent = de->d_parent;
615 struct md_op_data *op_data;
616 struct ptlrpc_request *req = NULL;
620 LASSERT(parent != NULL);
621 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
623 /* if server supports open-by-fid, or file name is invalid, don't pack
624 * name in open request */
625 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
626 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
628 len = de->d_name.len;
629 name = kmalloc(len + 1, GFP_NOFS);
634 spin_lock(&de->d_lock);
635 if (len != de->d_name.len) {
636 spin_unlock(&de->d_lock);
640 memcpy(name, de->d_name.name, len);
642 spin_unlock(&de->d_lock);
644 if (!lu_name_is_valid_2(name, len)) {
650 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
651 name, len, 0, LUSTRE_OPC_OPEN, NULL);
652 if (IS_ERR(op_data)) {
654 RETURN(PTR_ERR(op_data));
656 op_data->op_data = lmm;
657 op_data->op_data_size = lmmsize;
659 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_OPEN_DELAY, cfs_fail_val);
661 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
662 &ll_md_blocking_ast, 0);
664 ll_finish_md_op_data(op_data);
666 /* reason for keep own exit path - don`t flood log
667 * with messages with -ESTALE errors.
669 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
670 it_open_error(DISP_OPEN_OPEN, itp))
672 ll_release_openhandle(de, itp);
676 if (it_disposition(itp, DISP_LOOKUP_NEG))
677 GOTO(out, rc = -ENOENT);
679 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
680 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
681 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
685 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
687 if (!rc && itp->it_lock_mode) {
690 /* If we got a lock back and it has a LOOKUP bit set,
691 * make sure the dentry is marked as valid so we can find it.
692 * We don't need to care about actual hashing since other bits
693 * of kernel will deal with that later.
695 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
696 if (bits & MDS_INODELOCK_LOOKUP) {
697 d_lustre_revalidate(de);
698 ll_update_dir_depth(parent->d_inode, de->d_inode);
701 /* if DoM bit returned along with LAYOUT bit then there
702 * can be read-on-open data returned.
704 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
705 ll_dom_finish_open(de->d_inode, req);
709 ptlrpc_req_finished(req);
710 ll_intent_drop_lock(itp);
712 /* We did open by fid, but by the time we got to the server, the object
713 * disappeared. This is possible if the object was unlinked, but it's
714 * also possible if the object was unlinked by a rename. In the case
715 * of an object renamed over our existing one, we can't fail this open.
716 * O_CREAT also goes through this path if we had an existing dentry,
717 * and it's obviously wrong to return ENOENT for O_CREAT.
719 * Instead let's return -ESTALE, and the VFS will retry the open with
720 * LOOKUP_REVAL, which we catch in ll_revalidate_dentry and fail to
721 * revalidate, causing a lookup. This causes extra lookups in the case
722 * where we had a dentry in cache but the file is being unlinked and we
723 * lose the race with unlink, but this should be very rare.
731 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
732 struct obd_client_handle *och)
734 struct mdt_body *body;
736 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
737 och->och_open_handle = body->mbo_open_handle;
738 och->och_fid = body->mbo_fid1;
739 och->och_lease_handle.cookie = it->it_lock_handle;
740 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
741 och->och_flags = it->it_flags;
743 return md_set_open_replay_data(md_exp, och, it);
746 static int ll_local_open(struct file *file, struct lookup_intent *it,
747 struct ll_file_data *fd, struct obd_client_handle *och)
749 struct inode *inode = file_inode(file);
752 LASSERT(!file->private_data);
759 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
764 file->private_data = fd;
765 ll_readahead_init(inode, &fd->fd_ras);
766 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
771 void ll_track_file_opens(struct inode *inode)
773 struct ll_inode_info *lli = ll_i2info(inode);
774 struct ll_sb_info *sbi = ll_i2sbi(inode);
776 /* do not skew results with delays from never-opened inodes */
777 if (ktime_to_ns(lli->lli_close_fd_time))
778 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
779 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
781 if (ktime_after(ktime_get(),
782 ktime_add_ms(lli->lli_close_fd_time,
783 sbi->ll_oc_max_ms))) {
784 lli->lli_open_fd_count = 1;
785 lli->lli_close_fd_time = ns_to_ktime(0);
787 lli->lli_open_fd_count++;
790 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
791 lli->lli_open_fd_count);
794 /* Open a file, and (for the very first open) create objects on the OSTs at
795 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
796 * creation or open until ll_lov_setstripe() ioctl is called.
798 * If we already have the stripe MD locally then we don't request it in
799 * md_open(), by passing a lmm_size = 0.
801 * It is up to the application to ensure no other processes open this file
802 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
803 * used. We might be able to avoid races of that sort by getting lli_open_sem
804 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
805 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
807 int ll_file_open(struct inode *inode, struct file *file)
809 struct ll_inode_info *lli = ll_i2info(inode);
810 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
811 .it_flags = file->f_flags };
812 struct obd_client_handle **och_p = NULL;
813 __u64 *och_usecount = NULL;
814 struct ll_file_data *fd;
815 ktime_t kstart = ktime_get();
819 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
820 PFID(ll_inode2fid(inode)), inode, file->f_flags);
822 it = file->private_data; /* XXX: compat macro */
823 file->private_data = NULL; /* prevent ll_local_open assertion */
825 if (S_ISREG(inode->i_mode)) {
826 rc = ll_file_open_encrypt(inode, file);
828 if (it && it->it_disposition)
829 ll_release_openhandle(file_dentry(file), it);
830 GOTO(out_nofiledata, rc);
834 fd = ll_file_data_get();
836 GOTO(out_nofiledata, rc = -ENOMEM);
839 if (S_ISDIR(inode->i_mode))
840 ll_authorize_statahead(inode, fd);
842 ll_track_file_opens(inode);
843 if (is_root_inode(inode)) {
844 file->private_data = fd;
848 if (!it || !it->it_disposition) {
849 /* Convert f_flags into access mode. We cannot use file->f_mode,
850 * because everything but O_ACCMODE mask was stripped from
852 if ((oit.it_flags + 1) & O_ACCMODE)
854 if (file->f_flags & O_TRUNC)
855 oit.it_flags |= FMODE_WRITE;
857 /* kernel only call f_op->open in dentry_open. filp_open calls
858 * dentry_open after call to open_namei that checks permissions.
859 * Only nfsd_open call dentry_open directly without checking
860 * permissions and because of that this code below is safe.
862 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
863 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
865 /* We do not want O_EXCL here, presumably we opened the file
866 * already? XXX - NFS implications? */
867 oit.it_flags &= ~O_EXCL;
869 /* bug20584, if "it_flags" contains O_CREAT, the file will be
870 * created if necessary, then "IT_CREAT" should be set to keep
871 * consistent with it */
872 if (oit.it_flags & O_CREAT)
873 oit.it_op |= IT_CREAT;
879 /* Let's see if we have file open on MDS already. */
880 if (it->it_flags & FMODE_WRITE) {
881 och_p = &lli->lli_mds_write_och;
882 och_usecount = &lli->lli_open_fd_write_count;
883 } else if (it->it_flags & FMODE_EXEC) {
884 och_p = &lli->lli_mds_exec_och;
885 och_usecount = &lli->lli_open_fd_exec_count;
887 och_p = &lli->lli_mds_read_och;
888 och_usecount = &lli->lli_open_fd_read_count;
891 mutex_lock(&lli->lli_och_mutex);
892 if (*och_p) { /* Open handle is present */
893 if (it_disposition(it, DISP_OPEN_OPEN)) {
894 /* Well, there's extra open request that we do not need,
895 * let's close it somehow. This will decref request. */
896 rc = it_open_error(DISP_OPEN_OPEN, it);
898 mutex_unlock(&lli->lli_och_mutex);
899 GOTO(out_openerr, rc);
902 ll_release_openhandle(file_dentry(file), it);
906 rc = ll_local_open(file, it, fd, NULL);
909 mutex_unlock(&lli->lli_och_mutex);
910 GOTO(out_openerr, rc);
913 LASSERT(*och_usecount == 0);
914 if (!it->it_disposition) {
915 struct dentry *dentry = file_dentry(file);
916 struct ll_sb_info *sbi = ll_i2sbi(inode);
917 struct ll_dentry_data *ldd;
919 /* We cannot just request lock handle now, new ELC code
920 * means that one of other OPEN locks for this file
921 * could be cancelled, and since blocking ast handler
922 * would attempt to grab och_mutex as well, that would
923 * result in a deadlock
925 mutex_unlock(&lli->lli_och_mutex);
927 * Normally called under two situations:
929 * 2. A race/condition on MDS resulting in no open
930 * handle to be returned from LOOKUP|OPEN request,
931 * for example if the target entry was a symlink.
933 * In NFS path we know there's pathologic behavior
934 * so we always enable open lock caching when coming
935 * from there. It's detected by setting a flag in
938 * After reaching number of opens of this inode
939 * we always ask for an open lock on it to handle
940 * bad userspace actors that open and close files
941 * in a loop for absolutely no good reason
944 ldd = ll_d2d(dentry);
945 if (filename_is_volatile(dentry->d_name.name,
948 /* There really is nothing here, but this
949 * make this more readable I think.
950 * We do not want openlock for volatile
951 * files under any circumstances
953 } else if (ldd && ldd->lld_nfs_dentry) {
954 /* NFS path. This also happens to catch
955 * open by fh files I guess
957 it->it_flags |= MDS_OPEN_LOCK;
958 /* clear the flag for future lookups */
959 ldd->lld_nfs_dentry = 0;
960 } else if (sbi->ll_oc_thrsh_count > 0) {
961 /* Take MDS_OPEN_LOCK with many opens */
962 if (lli->lli_open_fd_count >=
963 sbi->ll_oc_thrsh_count)
964 it->it_flags |= MDS_OPEN_LOCK;
966 /* If this is open after we just closed */
967 else if (ktime_before(ktime_get(),
968 ktime_add_ms(lli->lli_close_fd_time,
969 sbi->ll_oc_thrsh_ms)))
970 it->it_flags |= MDS_OPEN_LOCK;
974 * Always specify MDS_OPEN_BY_FID because we don't want
975 * to get file with different fid.
977 it->it_flags |= MDS_OPEN_BY_FID;
978 rc = ll_intent_file_open(dentry, NULL, 0, it);
980 GOTO(out_openerr, rc);
984 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
986 GOTO(out_och_free, rc = -ENOMEM);
990 /* md_intent_lock() didn't get a request ref if there was an
991 * open error, so don't do cleanup on the request here
993 /* XXX (green): Should not we bail out on any error here, not
994 * just open error? */
995 rc = it_open_error(DISP_OPEN_OPEN, it);
997 GOTO(out_och_free, rc);
999 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
1000 "inode %p: disposition %x, status %d\n", inode,
1001 it_disposition(it, ~0), it->it_status);
1003 rc = ll_local_open(file, it, fd, *och_p);
1005 GOTO(out_och_free, rc);
1008 rc = pcc_file_open(inode, file);
1010 GOTO(out_och_free, rc);
1012 mutex_unlock(&lli->lli_och_mutex);
1016 /* Must do this outside lli_och_mutex lock to prevent deadlock where
1017 different kind of OPEN lock for this same inode gets cancelled
1018 by ldlm_cancel_lru */
1019 if (!S_ISREG(inode->i_mode))
1020 GOTO(out_och_free, rc);
1021 cl_lov_delay_create_clear(&file->f_flags);
1022 GOTO(out_och_free, rc);
1026 if (och_p && *och_p) {
1027 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1028 *och_p = NULL; /* OBD_FREE writes some magic there */
1031 mutex_unlock(&lli->lli_och_mutex);
1034 if (lli->lli_opendir_key == fd)
1035 ll_deauthorize_statahead(inode, fd);
1038 ll_file_data_put(fd);
1040 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1041 ktime_us_delta(ktime_get(), kstart));
1045 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1046 ptlrpc_req_finished(it->it_request);
1047 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1053 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1054 struct ldlm_lock_desc *desc, void *data, int flag)
1057 struct lustre_handle lockh;
1061 case LDLM_CB_BLOCKING:
1062 ldlm_lock2handle(lock, &lockh);
1063 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1065 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1069 case LDLM_CB_CANCELING:
1077 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1078 * and save it as fd->fd_och so as to force client to reopen the file even
1079 * if it has an open lock in cache already.
1081 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1082 struct lustre_handle *old_open_handle)
1084 struct ll_inode_info *lli = ll_i2info(inode);
1085 struct ll_file_data *fd = file->private_data;
1086 struct obd_client_handle **och_p;
1087 __u64 *och_usecount;
1091 /* Get the openhandle of the file */
1092 mutex_lock(&lli->lli_och_mutex);
1093 if (fd->fd_lease_och != NULL)
1094 GOTO(out_unlock, rc = -EBUSY);
1096 if (fd->fd_och == NULL) {
1097 if (file->f_mode & FMODE_WRITE) {
1098 LASSERT(lli->lli_mds_write_och != NULL);
1099 och_p = &lli->lli_mds_write_och;
1100 och_usecount = &lli->lli_open_fd_write_count;
1102 LASSERT(lli->lli_mds_read_och != NULL);
1103 och_p = &lli->lli_mds_read_och;
1104 och_usecount = &lli->lli_open_fd_read_count;
1107 if (*och_usecount > 1)
1108 GOTO(out_unlock, rc = -EBUSY);
1110 fd->fd_och = *och_p;
1115 *old_open_handle = fd->fd_och->och_open_handle;
1119 mutex_unlock(&lli->lli_och_mutex);
1124 * Release ownership on lli_mds_*_och when putting back a file lease.
1126 static int ll_lease_och_release(struct inode *inode, struct file *file)
1128 struct ll_inode_info *lli = ll_i2info(inode);
1129 struct ll_file_data *fd = file->private_data;
1130 struct obd_client_handle **och_p;
1131 struct obd_client_handle *old_och = NULL;
1132 __u64 *och_usecount;
1136 mutex_lock(&lli->lli_och_mutex);
1137 if (file->f_mode & FMODE_WRITE) {
1138 och_p = &lli->lli_mds_write_och;
1139 och_usecount = &lli->lli_open_fd_write_count;
1141 och_p = &lli->lli_mds_read_och;
1142 och_usecount = &lli->lli_open_fd_read_count;
1145 /* The file may have been open by another process (broken lease) so
1146 * *och_p is not NULL. In this case we should simply increase usecount
1149 if (*och_p != NULL) {
1150 old_och = fd->fd_och;
1153 *och_p = fd->fd_och;
1157 mutex_unlock(&lli->lli_och_mutex);
1159 if (old_och != NULL)
1160 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1166 * Acquire a lease and open the file.
1168 static struct obd_client_handle *
1169 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1172 struct lookup_intent it = { .it_op = IT_OPEN };
1173 struct ll_sb_info *sbi = ll_i2sbi(inode);
1174 struct md_op_data *op_data;
1175 struct ptlrpc_request *req = NULL;
1176 struct lustre_handle old_open_handle = { 0 };
1177 struct obd_client_handle *och = NULL;
1182 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1183 RETURN(ERR_PTR(-EINVAL));
1186 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1187 RETURN(ERR_PTR(-EPERM));
1189 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1191 RETURN(ERR_PTR(rc));
1196 RETURN(ERR_PTR(-ENOMEM));
1198 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1199 LUSTRE_OPC_ANY, NULL);
1200 if (IS_ERR(op_data))
1201 GOTO(out, rc = PTR_ERR(op_data));
1203 /* To tell the MDT this openhandle is from the same owner */
1204 op_data->op_open_handle = old_open_handle;
1206 it.it_flags = fmode | open_flags;
1207 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1208 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1209 &ll_md_blocking_lease_ast,
1210 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1211 * it can be cancelled which may mislead applications that the lease is
1213 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1214 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1215 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1216 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1217 ll_finish_md_op_data(op_data);
1218 ptlrpc_req_finished(req);
1220 GOTO(out_release_it, rc);
1222 if (it_disposition(&it, DISP_LOOKUP_NEG))
1223 GOTO(out_release_it, rc = -ENOENT);
1225 rc = it_open_error(DISP_OPEN_OPEN, &it);
1227 GOTO(out_release_it, rc);
1229 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1230 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1232 GOTO(out_release_it, rc);
1234 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1235 GOTO(out_close, rc = -EOPNOTSUPP);
1237 /* already get lease, handle lease lock */
1238 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1239 if (!it.it_lock_mode ||
1240 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1241 /* open lock must return for lease */
1242 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1243 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1245 GOTO(out_close, rc = -EPROTO);
1248 ll_intent_release(&it);
1252 /* Cancel open lock */
1253 if (it.it_lock_mode != 0) {
1254 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1256 it.it_lock_mode = 0;
1257 och->och_lease_handle.cookie = 0ULL;
1259 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1261 CERROR("%s: error closing file "DFID": %d\n",
1262 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1263 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1265 ll_intent_release(&it);
1269 RETURN(ERR_PTR(rc));
1273 * Check whether a layout swap can be done between two inodes.
1275 * \param[in] inode1 First inode to check
1276 * \param[in] inode2 Second inode to check
1278 * \retval 0 on success, layout swap can be performed between both inodes
1279 * \retval negative error code if requirements are not met
1281 static int ll_check_swap_layouts_validity(struct inode *inode1,
1282 struct inode *inode2)
1284 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1287 if (inode_permission(inode1, MAY_WRITE) ||
1288 inode_permission(inode2, MAY_WRITE))
1291 if (inode1->i_sb != inode2->i_sb)
1297 static int ll_swap_layouts_close(struct obd_client_handle *och,
1298 struct inode *inode, struct inode *inode2)
1300 const struct lu_fid *fid1 = ll_inode2fid(inode);
1301 const struct lu_fid *fid2;
1305 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1306 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1308 rc = ll_check_swap_layouts_validity(inode, inode2);
1310 GOTO(out_free_och, rc);
1312 /* We now know that inode2 is a lustre inode */
1313 fid2 = ll_inode2fid(inode2);
1315 rc = lu_fid_cmp(fid1, fid2);
1317 GOTO(out_free_och, rc = -EINVAL);
1319 /* Close the file and {swap,merge} layouts between inode & inode2.
1320 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1321 * because we still need it to pack l_remote_handle to MDT. */
1322 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1325 och = NULL; /* freed in ll_close_inode_openhandle() */
1335 * Release lease and close the file.
1336 * It will check if the lease has ever broken.
1338 static int ll_lease_close_intent(struct obd_client_handle *och,
1339 struct inode *inode,
1340 bool *lease_broken, enum mds_op_bias bias,
1343 struct ldlm_lock *lock;
1344 bool cancelled = true;
1348 lock = ldlm_handle2lock(&och->och_lease_handle);
1350 lock_res_and_lock(lock);
1351 cancelled = ldlm_is_cancel(lock);
1352 unlock_res_and_lock(lock);
1353 LDLM_LOCK_PUT(lock);
1356 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1357 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1359 if (lease_broken != NULL)
1360 *lease_broken = cancelled;
1362 if (!cancelled && !bias)
1363 ldlm_cli_cancel(&och->och_lease_handle, 0);
1365 if (cancelled) { /* no need to excute intent */
1370 rc = ll_close_inode_openhandle(inode, och, bias, data);
1374 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1377 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1381 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1383 static int ll_lease_file_resync(struct obd_client_handle *och,
1384 struct inode *inode, unsigned long arg)
1386 struct ll_sb_info *sbi = ll_i2sbi(inode);
1387 struct md_op_data *op_data;
1388 struct ll_ioc_lease_id ioc;
1389 __u64 data_version_unused;
1393 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1394 LUSTRE_OPC_ANY, NULL);
1395 if (IS_ERR(op_data))
1396 RETURN(PTR_ERR(op_data));
1398 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1402 /* before starting file resync, it's necessary to clean up page cache
1403 * in client memory, otherwise once the layout version is increased,
1404 * writing back cached data will be denied the OSTs. */
1405 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1409 op_data->op_lease_handle = och->och_lease_handle;
1410 op_data->op_mirror_id = ioc.lil_mirror_id;
1411 rc = md_file_resync(sbi->ll_md_exp, op_data);
1417 ll_finish_md_op_data(op_data);
1421 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1423 struct ll_inode_info *lli = ll_i2info(inode);
1424 struct cl_object *obj = lli->lli_clob;
1425 struct cl_attr *attr = vvp_env_thread_attr(env);
1433 ll_inode_size_lock(inode);
1435 /* Merge timestamps the most recently obtained from MDS with
1436 * timestamps obtained from OSTs.
1438 * Do not overwrite atime of inode because it may be refreshed
1439 * by file_accessed() function. If the read was served by cache
1440 * data, there is no RPC to be sent so that atime may not be
1441 * transferred to OSTs at all. MDT only updates atime at close time
1442 * if it's at least 'mdd.*.atime_diff' older.
1443 * All in all, the atime in Lustre does not strictly comply with
1444 * POSIX. Solving this problem needs to send an RPC to MDT for each
1445 * read, this will hurt performance.
1447 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1448 inode->i_atime.tv_sec < lli->lli_atime)
1449 inode->i_atime.tv_sec = lli->lli_atime;
1451 inode->i_mtime.tv_sec = lli->lli_mtime;
1452 inode->i_ctime.tv_sec = lli->lli_ctime;
1454 mtime = inode->i_mtime.tv_sec;
1455 atime = inode->i_atime.tv_sec;
1456 ctime = inode->i_ctime.tv_sec;
1458 cl_object_attr_lock(obj);
1459 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1462 rc = cl_object_attr_get(env, obj, attr);
1463 cl_object_attr_unlock(obj);
1466 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1468 if (atime < attr->cat_atime)
1469 atime = attr->cat_atime;
1471 if (ctime < attr->cat_ctime)
1472 ctime = attr->cat_ctime;
1474 if (mtime < attr->cat_mtime)
1475 mtime = attr->cat_mtime;
1477 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1478 PFID(&lli->lli_fid), attr->cat_size);
1480 if (llcrypt_require_key(inode) == -ENOKEY) {
1481 /* Without the key, round up encrypted file size to next
1482 * LUSTRE_ENCRYPTION_UNIT_SIZE. Clear text size is put in
1483 * lli_lazysize for proper file size setting at close time.
1485 lli->lli_attr_valid |= OBD_MD_FLLAZYSIZE;
1486 lli->lli_lazysize = attr->cat_size;
1487 attr->cat_size = round_up(attr->cat_size,
1488 LUSTRE_ENCRYPTION_UNIT_SIZE);
1490 i_size_write(inode, attr->cat_size);
1491 inode->i_blocks = attr->cat_blocks;
1493 inode->i_mtime.tv_sec = mtime;
1494 inode->i_atime.tv_sec = atime;
1495 inode->i_ctime.tv_sec = ctime;
1498 ll_inode_size_unlock(inode);
1504 * Set designated mirror for I/O.
1506 * So far only read, write, and truncated can support to issue I/O to
1507 * designated mirror.
1509 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1511 struct ll_file_data *fd = file->private_data;
1513 /* clear layout version for generic(non-resync) I/O in case it carries
1514 * stale layout version due to I/O restart */
1515 io->ci_layout_version = 0;
1517 /* FLR: disable non-delay for designated mirror I/O because obviously
1518 * only one mirror is available */
1519 if (fd->fd_designated_mirror > 0) {
1521 io->ci_designated_mirror = fd->fd_designated_mirror;
1522 io->ci_layout_version = fd->fd_layout_version;
1525 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1526 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1529 static bool file_is_noatime(const struct file *file)
1531 const struct vfsmount *mnt = file->f_path.mnt;
1532 const struct inode *inode = file_inode((struct file *)file);
1534 /* Adapted from file_accessed() and touch_atime().*/
1535 if (file->f_flags & O_NOATIME)
1538 if (inode->i_flags & S_NOATIME)
1541 if (IS_NOATIME(inode))
1544 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1547 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1550 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1556 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1557 struct vvp_io_args *args)
1559 struct inode *inode = file_inode(file);
1560 struct ll_file_data *fd = file->private_data;
1562 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1563 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1565 if (iot == CIT_WRITE) {
1566 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1567 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1568 file->f_flags & O_DIRECT ||
1570 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1571 io->u.ci_wr.wr_sync |= !!(args &&
1572 (args->u.normal.via_iocb->ki_flags &
1577 io->ci_obj = ll_i2info(inode)->lli_clob;
1578 io->ci_lockreq = CILR_MAYBE;
1579 if (ll_file_nolock(file)) {
1580 io->ci_lockreq = CILR_NEVER;
1581 io->ci_no_srvlock = 1;
1582 } else if (file->f_flags & O_APPEND) {
1583 io->ci_lockreq = CILR_MANDATORY;
1585 io->ci_noatime = file_is_noatime(file);
1586 io->ci_async_readahead = false;
1588 /* FLR: only use non-delay I/O for read as there is only one
1589 * avaliable mirror for write. */
1590 io->ci_ndelay = !(iot == CIT_WRITE);
1592 ll_io_set_mirror(io, file);
1595 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1598 struct ll_inode_info *lli = ll_i2info(inode);
1599 struct ll_sb_info *sbi = ll_i2sbi(inode);
1600 enum obd_heat_type sample_type;
1601 enum obd_heat_type iobyte_type;
1602 __u64 now = ktime_get_real_seconds();
1604 if (!ll_sbi_has_file_heat(sbi) ||
1605 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1608 if (iot == CIT_READ) {
1609 sample_type = OBD_HEAT_READSAMPLE;
1610 iobyte_type = OBD_HEAT_READBYTE;
1611 } else if (iot == CIT_WRITE) {
1612 sample_type = OBD_HEAT_WRITESAMPLE;
1613 iobyte_type = OBD_HEAT_WRITEBYTE;
1618 spin_lock(&lli->lli_heat_lock);
1619 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1620 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1621 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1622 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1623 spin_unlock(&lli->lli_heat_lock);
1627 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1628 struct file *file, enum cl_io_type iot,
1629 loff_t *ppos, size_t count)
1631 struct vvp_io *vio = vvp_env_io(env);
1632 struct inode *inode = file_inode(file);
1633 struct ll_inode_info *lli = ll_i2info(inode);
1634 struct ll_sb_info *sbi = ll_i2sbi(inode);
1635 struct ll_file_data *fd = file->private_data;
1636 struct range_lock range;
1637 bool range_locked = false;
1642 unsigned int retried = 0, dio_lock = 0;
1643 bool is_aio = false;
1644 bool is_parallel_dio = false;
1645 struct cl_dio_aio *ci_aio = NULL;
1647 bool partial_io = false;
1648 size_t max_io_pages, max_cached_pages;
1652 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1653 file_dentry(file)->d_name.name,
1654 iot == CIT_READ ? "read" : "write", *ppos, count);
1656 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1657 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1658 if (max_io_pages > (max_cached_pages >> 2))
1659 max_io_pages = max_cached_pages >> 2;
1661 io = vvp_env_thread_io(env);
1662 if (file->f_flags & O_DIRECT) {
1663 if (!is_sync_kiocb(args->u.normal.via_iocb))
1666 /* the kernel does not support AIO on pipes, and parallel DIO
1667 * uses part of the AIO path, so we must not do parallel dio
1670 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1673 if (!ll_sbi_has_parallel_dio(sbi))
1674 is_parallel_dio = false;
1676 ci_aio = cl_aio_alloc(args->u.normal.via_iocb,
1677 ll_i2info(inode)->lli_clob);
1679 GOTO(out, rc = -ENOMEM);
1684 * IO block size need be aware of cached page limit, otherwise
1685 * if we have small max_cached_mb but large block IO issued, io
1686 * could not be finished and blocked whole client.
1688 if (file->f_flags & O_DIRECT)
1691 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1692 partial_io = per_bytes < count;
1693 io = vvp_env_thread_io(env);
1694 ll_io_init(io, file, iot, args);
1695 io->ci_aio = ci_aio;
1696 io->ci_dio_lock = dio_lock;
1697 io->ci_ndelay_tried = retried;
1698 io->ci_parallel_dio = is_parallel_dio;
1700 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1701 if (file->f_flags & O_APPEND)
1702 range_lock_init(&range, 0, LUSTRE_EOF);
1704 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1706 vio->vui_fd = file->private_data;
1707 vio->vui_iter = args->u.normal.via_iter;
1708 vio->vui_iocb = args->u.normal.via_iocb;
1709 /* Direct IO reads must also take range lock,
1710 * or multiple reads will try to work on the same pages
1711 * See LU-6227 for details.
1713 if (((iot == CIT_WRITE) ||
1714 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1715 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1716 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1718 rc = range_lock(&lli->lli_write_tree, &range);
1722 range_locked = true;
1725 ll_cl_add(inode, env, io, LCC_RW);
1726 rc = cl_io_loop(env, io);
1727 ll_cl_remove(inode, env);
1729 if (range_locked && !is_parallel_dio) {
1730 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1732 range_unlock(&lli->lli_write_tree, &range);
1733 range_locked = false;
1736 /* cl_io_rw_init() handled IO */
1740 /* N/B: parallel DIO may be disabled during i/o submission;
1741 * if that occurs, async RPCs are resolved before we get here, and this
1742 * wait call completes immediately.
1744 if (is_parallel_dio) {
1745 struct cl_sync_io *anchor = &io->ci_aio->cda_sync;
1747 /* for dio, EIOCBQUEUED is an implementation detail,
1748 * and we don't return it to userspace
1750 if (rc == -EIOCBQUEUED)
1753 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1758 range_unlock(&lli->lli_write_tree, &range);
1759 range_locked = false;
1764 * In order to move forward AIO, ci_nob was increased,
1765 * but that doesn't mean io have been finished, it just
1766 * means io have been submited, we will always return
1767 * EIOCBQUEUED to the caller, So we could only return
1768 * number of bytes in non-AIO case.
1770 if (io->ci_nob > 0) {
1773 result += io->ci_nob;
1774 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1779 count -= io->ci_nob;
1781 /* prepare IO restart */
1783 args->u.normal.via_iter = vio->vui_iter;
1787 * Reexpand iov count because it was zero
1790 iov_iter_reexpand(vio->vui_iter, count);
1791 if (per_bytes == io->ci_nob)
1792 io->ci_need_restart = 1;
1796 cl_io_fini(env, io);
1799 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1800 file->f_path.dentry->d_name.name,
1801 iot, rc, result, io->ci_need_restart);
1803 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1804 count > 0 && io->ci_need_restart) {
1806 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1807 file_dentry(file)->d_name.name,
1808 iot == CIT_READ ? "read" : "write",
1809 *ppos, count, result, rc);
1810 /* preserve the tried count for FLR */
1811 retried = io->ci_ndelay_tried;
1812 dio_lock = io->ci_dio_lock;
1818 * VFS will call aio_complete() if no -EIOCBQUEUED
1819 * is returned for AIO, so we can not call aio_complete()
1822 if (rc != -EIOCBQUEUED)
1823 io->ci_aio->cda_no_aio_complete = 1;
1825 * Drop one extra reference so that end_io() could be
1826 * called for this IO context, we could call it after
1827 * we make sure all AIO requests have been proceed.
1829 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1830 rc == -EIOCBQUEUED ? 0 : rc);
1832 cl_aio_free(env, io->ci_aio);
1837 if (iot == CIT_READ) {
1839 ll_stats_ops_tally(ll_i2sbi(inode),
1840 LPROC_LL_READ_BYTES, result);
1841 } else if (iot == CIT_WRITE) {
1843 ll_stats_ops_tally(ll_i2sbi(inode),
1844 LPROC_LL_WRITE_BYTES, result);
1845 fd->fd_write_failed = false;
1846 } else if (result == 0 && rc == 0) {
1849 fd->fd_write_failed = true;
1851 fd->fd_write_failed = false;
1852 } else if (rc != -ERESTARTSYS) {
1853 fd->fd_write_failed = true;
1857 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1859 ll_heat_add(inode, iot, result);
1861 RETURN(result > 0 ? result : rc);
1865 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1866 * especially for small I/O.
1868 * To serve a read request, CLIO has to create and initialize a cl_io and
1869 * then request DLM lock. This has turned out to have siginificant overhead
1870 * and affects the performance of small I/O dramatically.
1872 * It's not necessary to create a cl_io for each I/O. Under the help of read
1873 * ahead, most of the pages being read are already in memory cache and we can
1874 * read those pages directly because if the pages exist, the corresponding DLM
1875 * lock must exist so that page content must be valid.
1877 * In fast read implementation, the llite speculatively finds and reads pages
1878 * in memory cache. There are three scenarios for fast read:
1879 * - If the page exists and is uptodate, kernel VM will provide the data and
1880 * CLIO won't be intervened;
1881 * - If the page was brought into memory by read ahead, it will be exported
1882 * and read ahead parameters will be updated;
1883 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1884 * it will go back and invoke normal read, i.e., a cl_io will be created
1885 * and DLM lock will be requested.
1887 * POSIX compliance: posix standard states that read is intended to be atomic.
1888 * Lustre read implementation is in line with Linux kernel read implementation
1889 * and neither of them complies with POSIX standard in this matter. Fast read
1890 * doesn't make the situation worse on single node but it may interleave write
1891 * results from multiple nodes due to short read handling in ll_file_aio_read().
1893 * \param env - lu_env
1894 * \param iocb - kiocb from kernel
1895 * \param iter - user space buffers where the data will be copied
1897 * \retval - number of bytes have been read, or error code if error occurred.
1900 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1904 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1907 /* NB: we can't do direct IO for fast read because it will need a lock
1908 * to make IO engine happy. */
1909 if (iocb->ki_filp->f_flags & O_DIRECT)
1912 result = generic_file_read_iter(iocb, iter);
1914 /* If the first page is not in cache, generic_file_aio_read() will be
1915 * returned with -ENODATA.
1916 * See corresponding code in ll_readpage(). */
1917 if (result == -ENODATA)
1921 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1922 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1923 LPROC_LL_READ_BYTES, result);
1930 * Read from a file (through the page cache).
1932 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1935 struct vvp_io_args *args;
1936 struct file *file = iocb->ki_filp;
1940 ktime_t kstart = ktime_get();
1943 if (!iov_iter_count(to))
1947 * Currently when PCC read failed, we do not fall back to the
1948 * normal read path, just return the error.
1949 * The resaon is that: for RW-PCC, the file data may be modified
1950 * in the PCC and inconsistent with the data on OSTs (or file
1951 * data has been removed from the Lustre file system), at this
1952 * time, fallback to the normal read path may read the wrong
1954 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1955 * path: read data from data copy on OSTs.
1957 result = pcc_file_read_iter(iocb, to, &cached);
1961 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1963 result = ll_do_fast_read(iocb, to);
1964 if (result < 0 || iov_iter_count(to) == 0)
1967 env = cl_env_get(&refcheck);
1969 return PTR_ERR(env);
1971 args = ll_env_args(env);
1972 args->u.normal.via_iter = to;
1973 args->u.normal.via_iocb = iocb;
1975 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1976 &iocb->ki_pos, iov_iter_count(to));
1979 else if (result == 0)
1982 cl_env_put(env, &refcheck);
1985 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1986 file->private_data, iocb->ki_pos, result,
1988 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1989 ktime_us_delta(ktime_get(), kstart));
1996 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1997 * If a page is already in the page cache and dirty (and some other things -
1998 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1999 * write to it without doing a full I/O, because Lustre already knows about it
2000 * and will write it out. This saves a lot of processing time.
2002 * All writes here are within one page, so exclusion is handled by the page
2003 * lock on the vm page. We do not do tiny writes for writes which touch
2004 * multiple pages because it's very unlikely multiple sequential pages are
2005 * are already dirty.
2007 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
2008 * and are unlikely to be to already dirty pages.
2010 * Attribute updates are important here, we do them in ll_tiny_write_end.
2012 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
2014 ssize_t count = iov_iter_count(iter);
2015 struct file *file = iocb->ki_filp;
2016 struct inode *inode = file_inode(file);
2017 bool lock_inode = !IS_NOSEC(inode);
2022 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
2023 * of function for why.
2025 if (count >= PAGE_SIZE ||
2026 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
2029 if (unlikely(lock_inode))
2031 result = __generic_file_write_iter(iocb, iter);
2033 if (unlikely(lock_inode))
2034 inode_unlock(inode);
2036 /* If the page is not already dirty, ll_tiny_write_begin returns
2037 * -ENODATA. We continue on to normal write.
2039 if (result == -ENODATA)
2043 ll_heat_add(inode, CIT_WRITE, result);
2044 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2047 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2053 * Write to a file (through the page cache).
2055 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2057 struct vvp_io_args *args;
2059 ssize_t rc_tiny = 0, rc_normal;
2060 struct file *file = iocb->ki_filp;
2063 ktime_t kstart = ktime_get();
2068 if (!iov_iter_count(from))
2069 GOTO(out, rc_normal = 0);
2072 * When PCC write failed, we usually do not fall back to the normal
2073 * write path, just return the error. But there is a special case when
2074 * returned error code is -ENOSPC due to running out of space on PCC HSM
2075 * bakcend. At this time, it will fall back to normal I/O path and
2076 * retry the I/O. As the file is in HSM released state, it will restore
2077 * the file data to OSTs first and redo the write again. And the
2078 * restore process will revoke the layout lock and detach the file
2079 * from PCC cache automatically.
2081 result = pcc_file_write_iter(iocb, from, &cached);
2082 if (cached && result != -ENOSPC && result != -EDQUOT)
2083 GOTO(out, rc_normal = result);
2085 /* NB: we can't do direct IO for tiny writes because they use the page
2086 * cache, we can't do sync writes because tiny writes can't flush
2087 * pages, and we can't do append writes because we can't guarantee the
2088 * required DLM locks are held to protect file size.
2090 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2091 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2092 rc_tiny = ll_do_tiny_write(iocb, from);
2094 /* In case of error, go on and try normal write - Only stop if tiny
2095 * write completed I/O.
2097 if (iov_iter_count(from) == 0)
2098 GOTO(out, rc_normal = rc_tiny);
2100 env = cl_env_get(&refcheck);
2102 return PTR_ERR(env);
2104 args = ll_env_args(env);
2105 args->u.normal.via_iter = from;
2106 args->u.normal.via_iocb = iocb;
2108 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2109 &iocb->ki_pos, iov_iter_count(from));
2111 /* On success, combine bytes written. */
2112 if (rc_tiny >= 0 && rc_normal > 0)
2113 rc_normal += rc_tiny;
2114 /* On error, only return error from normal write if tiny write did not
2115 * write any bytes. Otherwise return bytes written by tiny write.
2117 else if (rc_tiny > 0)
2118 rc_normal = rc_tiny;
2120 cl_env_put(env, &refcheck);
2122 if (rc_normal > 0) {
2123 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2124 file->private_data, iocb->ki_pos,
2126 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2127 ktime_us_delta(ktime_get(), kstart));
2133 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2135 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2137 static int ll_file_get_iov_count(const struct iovec *iov,
2138 unsigned long *nr_segs, size_t *count,
2144 for (seg = 0; seg < *nr_segs; seg++) {
2145 const struct iovec *iv = &iov[seg];
2148 * If any segment has a negative length, or the cumulative
2149 * length ever wraps negative then return -EINVAL.
2152 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2154 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2159 cnt -= iv->iov_len; /* This segment is no good */
2166 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2167 unsigned long nr_segs, loff_t pos)
2174 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2181 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2182 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2183 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2184 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2185 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2187 result = ll_file_read_iter(iocb, &to);
2192 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2195 struct iovec iov = { .iov_base = buf, .iov_len = count };
2204 init_sync_kiocb(&kiocb, file);
2205 kiocb.ki_pos = *ppos;
2206 #ifdef HAVE_KIOCB_KI_LEFT
2207 kiocb.ki_left = count;
2208 #elif defined(HAVE_KI_NBYTES)
2209 kiocb.i_nbytes = count;
2212 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2213 *ppos = kiocb.ki_pos;
2219 * Write to a file (through the page cache).
2222 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2223 unsigned long nr_segs, loff_t pos)
2225 struct iov_iter from;
2230 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2237 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2238 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2239 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2240 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2241 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2243 result = ll_file_write_iter(iocb, &from);
2248 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2249 size_t count, loff_t *ppos)
2251 struct iovec iov = { .iov_base = (void __user *)buf,
2261 init_sync_kiocb(&kiocb, file);
2262 kiocb.ki_pos = *ppos;
2263 #ifdef HAVE_KIOCB_KI_LEFT
2264 kiocb.ki_left = count;
2265 #elif defined(HAVE_KI_NBYTES)
2266 kiocb.ki_nbytes = count;
2269 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2270 *ppos = kiocb.ki_pos;
2274 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2276 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2277 __u64 flags, struct lov_user_md *lum, int lum_size)
2279 struct lookup_intent oit = {
2281 .it_flags = flags | MDS_OPEN_BY_FID,
2286 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2287 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2288 /* this code will only exist for big-endian systems */
2289 lustre_swab_lov_user_md(lum, 0);
2292 ll_inode_size_lock(inode);
2293 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2295 GOTO(out_unlock, rc);
2297 ll_release_openhandle(dentry, &oit);
2300 ll_inode_size_unlock(inode);
2301 ll_intent_release(&oit);
2306 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2307 struct lov_mds_md **lmmp, int *lmm_size,
2308 struct ptlrpc_request **request)
2310 struct ll_sb_info *sbi = ll_i2sbi(inode);
2311 struct mdt_body *body;
2312 struct lov_mds_md *lmm = NULL;
2313 struct ptlrpc_request *req = NULL;
2314 struct md_op_data *op_data;
2319 rc = ll_get_default_mdsize(sbi, &lmmsize);
2323 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2324 strlen(filename), lmmsize,
2325 LUSTRE_OPC_ANY, NULL);
2326 if (IS_ERR(op_data))
2327 RETURN(PTR_ERR(op_data));
2329 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2330 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2331 ll_finish_md_op_data(op_data);
2333 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
2338 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2339 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2341 lmmsize = body->mbo_eadatasize;
2343 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2345 GOTO(out, rc = -ENODATA);
2347 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2348 LASSERT(lmm != NULL);
2350 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2351 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2352 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2353 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2354 GOTO(out, rc = -EPROTO);
2357 * This is coming from the MDS, so is probably in
2358 * little endian. We convert it to host endian before
2359 * passing it to userspace.
2361 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2362 int stripe_count = 0;
2364 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2365 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2366 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2367 if (le32_to_cpu(lmm->lmm_pattern) &
2368 LOV_PATTERN_F_RELEASED)
2370 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2372 /* if function called for directory - we should
2373 * avoid swab not existent lsm objects
2375 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2376 S_ISREG(body->mbo_mode))
2377 lustre_swab_lov_user_md_objects(
2378 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2380 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2381 S_ISREG(body->mbo_mode))
2382 lustre_swab_lov_user_md_objects(
2383 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2385 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2386 lustre_swab_lov_comp_md_v1(
2387 (struct lov_comp_md_v1 *)lmm);
2391 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2392 struct lov_comp_md_v1 *comp_v1 = NULL;
2393 struct lov_comp_md_entry_v1 *ent;
2394 struct lov_user_md_v1 *v1;
2398 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2399 /* Dump the striping information */
2400 for (; i < comp_v1->lcm_entry_count; i++) {
2401 ent = &comp_v1->lcm_entries[i];
2402 off = ent->lcme_offset;
2403 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2405 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2406 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2410 * Return valid stripe_count and stripe_size instead of 0 for
2411 * DoM files to avoid divide-by-zero for older userspace that
2412 * calls this ioctl, e.g. lustre ADIO driver.
2414 if (lmm->lmm_stripe_count == 0)
2415 lmm->lmm_stripe_count = 1;
2416 if (lmm->lmm_stripe_size == 0) {
2417 /* Since the first component of the file data is placed
2418 * on the MDT for faster access, the stripe_size of the
2419 * second one is always that applications which are
2422 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2423 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2425 i = comp_v1->lcm_entry_count > 1 ?
2426 comp_v1->lcm_entry_count - 1 : 0;
2427 ent = &comp_v1->lcm_entries[i];
2428 off = ent->lcme_offset;
2429 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2430 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2435 *lmm_size = lmmsize;
2440 static int ll_lov_setea(struct inode *inode, struct file *file,
2443 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2444 struct lov_user_md *lump;
2445 int lum_size = sizeof(struct lov_user_md) +
2446 sizeof(struct lov_user_ost_data);
2450 if (!capable(CAP_SYS_ADMIN))
2453 OBD_ALLOC_LARGE(lump, lum_size);
2457 if (copy_from_user(lump, arg, lum_size))
2458 GOTO(out_lump, rc = -EFAULT);
2460 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2462 cl_lov_delay_create_clear(&file->f_flags);
2465 OBD_FREE_LARGE(lump, lum_size);
2469 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2476 env = cl_env_get(&refcheck);
2478 RETURN(PTR_ERR(env));
2480 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2481 cl_env_put(env, &refcheck);
2485 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2488 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2489 struct lov_user_md *klum;
2491 __u64 flags = FMODE_WRITE;
2494 rc = ll_copy_user_md(lum, &klum);
2499 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2504 rc = put_user(0, &lum->lmm_stripe_count);
2508 rc = ll_layout_refresh(inode, &gen);
2512 rc = ll_file_getstripe(inode, arg, lum_size);
2513 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2514 ll_i2info(inode)->lli_clob) {
2515 struct iattr attr = { 0 };
2517 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2518 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2521 cl_lov_delay_create_clear(&file->f_flags);
2524 OBD_FREE_LARGE(klum, lum_size);
2530 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2532 struct ll_inode_info *lli = ll_i2info(inode);
2533 struct cl_object *obj = lli->lli_clob;
2534 struct ll_file_data *fd = file->private_data;
2535 struct ll_grouplock grouplock;
2540 CWARN("group id for group lock must not be 0\n");
2544 if (ll_file_nolock(file))
2545 RETURN(-EOPNOTSUPP);
2547 if (file->f_flags & O_NONBLOCK) {
2548 if (!mutex_trylock(&lli->lli_group_mutex))
2551 mutex_lock(&lli->lli_group_mutex);
2553 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2554 CWARN("group lock already existed with gid %lu\n",
2555 fd->fd_grouplock.lg_gid);
2556 GOTO(out, rc = -EINVAL);
2558 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2559 if (file->f_flags & O_NONBLOCK)
2560 GOTO(out, rc = -EAGAIN);
2561 mutex_unlock(&lli->lli_group_mutex);
2562 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2563 GOTO(retry, rc = 0);
2565 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2568 * XXX: group lock needs to protect all OST objects while PFL
2569 * can add new OST objects during the IO, so we'd instantiate
2570 * all OST objects before getting its group lock.
2575 struct cl_layout cl = {
2576 .cl_is_composite = false,
2578 struct lu_extent ext = {
2580 .e_end = OBD_OBJECT_EOF,
2583 env = cl_env_get(&refcheck);
2585 GOTO(out, rc = PTR_ERR(env));
2587 rc = cl_object_layout_get(env, obj, &cl);
2588 if (rc >= 0 && cl.cl_is_composite)
2589 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2592 cl_env_put(env, &refcheck);
2597 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2598 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2603 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2604 fd->fd_grouplock = grouplock;
2605 if (lli->lli_group_users == 0)
2606 lli->lli_group_gid = grouplock.lg_gid;
2607 lli->lli_group_users++;
2609 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2611 mutex_unlock(&lli->lli_group_mutex);
2616 static int ll_put_grouplock(struct inode *inode, struct file *file,
2619 struct ll_inode_info *lli = ll_i2info(inode);
2620 struct ll_file_data *fd = file->private_data;
2621 struct ll_grouplock grouplock;
2625 mutex_lock(&lli->lli_group_mutex);
2626 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2627 CWARN("no group lock held\n");
2628 GOTO(out, rc = -EINVAL);
2631 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2633 if (fd->fd_grouplock.lg_gid != arg) {
2634 CWARN("group lock %lu doesn't match current id %lu\n",
2635 arg, fd->fd_grouplock.lg_gid);
2636 GOTO(out, rc = -EINVAL);
2639 grouplock = fd->fd_grouplock;
2640 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2641 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2643 cl_put_grouplock(&grouplock);
2645 lli->lli_group_users--;
2646 if (lli->lli_group_users == 0) {
2647 lli->lli_group_gid = 0;
2648 wake_up_var(&lli->lli_group_users);
2650 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2653 mutex_unlock(&lli->lli_group_mutex);
2659 * Close inode open handle
2661 * \param dentry [in] dentry which contains the inode
2662 * \param it [in,out] intent which contains open info and result
2665 * \retval <0 failure
2667 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2669 struct inode *inode = dentry->d_inode;
2670 struct obd_client_handle *och;
2676 /* Root ? Do nothing. */
2677 if (is_root_inode(inode))
2680 /* No open handle to close? Move away */
2681 if (!it_disposition(it, DISP_OPEN_OPEN))
2684 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2686 OBD_ALLOC(och, sizeof(*och));
2688 GOTO(out, rc = -ENOMEM);
2690 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2694 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2696 /* this one is in place of ll_file_open */
2697 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2698 ptlrpc_req_finished(it->it_request);
2699 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2705 * Get size for inode for which FIEMAP mapping is requested.
2706 * Make the FIEMAP get_info call and returns the result.
2707 * \param fiemap kernel buffer to hold extens
2708 * \param num_bytes kernel buffer size
2710 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2716 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2719 /* Checks for fiemap flags */
2720 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2721 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2725 /* Check for FIEMAP_FLAG_SYNC */
2726 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2727 rc = filemap_fdatawrite(inode->i_mapping);
2732 env = cl_env_get(&refcheck);
2734 RETURN(PTR_ERR(env));
2736 if (i_size_read(inode) == 0) {
2737 rc = ll_glimpse_size(inode);
2742 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2743 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2744 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2746 /* If filesize is 0, then there would be no objects for mapping */
2747 if (fmkey.lfik_oa.o_size == 0) {
2748 fiemap->fm_mapped_extents = 0;
2752 fmkey.lfik_fiemap = *fiemap;
2754 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2755 &fmkey, fiemap, &num_bytes);
2757 cl_env_put(env, &refcheck);
2761 int ll_fid2path(struct inode *inode, void __user *arg)
2763 struct obd_export *exp = ll_i2mdexp(inode);
2764 const struct getinfo_fid2path __user *gfin = arg;
2766 struct getinfo_fid2path *gfout;
2772 if (!capable(CAP_DAC_READ_SEARCH) &&
2773 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
2776 /* Only need to get the buflen */
2777 if (get_user(pathlen, &gfin->gf_pathlen))
2780 if (pathlen > PATH_MAX)
2783 outsize = sizeof(*gfout) + pathlen;
2784 OBD_ALLOC(gfout, outsize);
2788 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2789 GOTO(gf_free, rc = -EFAULT);
2790 /* append root FID after gfout to let MDT know the root FID so that it
2791 * can lookup the correct path, this is mainly for fileset.
2792 * old server without fileset mount support will ignore this. */
2793 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2795 /* Call mdc_iocontrol */
2796 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2800 if (copy_to_user(arg, gfout, outsize))
2804 OBD_FREE(gfout, outsize);
2809 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2811 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2819 ioc->idv_version = 0;
2820 ioc->idv_layout_version = UINT_MAX;
2822 /* If no file object initialized, we consider its version is 0. */
2826 env = cl_env_get(&refcheck);
2828 RETURN(PTR_ERR(env));
2830 io = vvp_env_thread_io(env);
2832 io->u.ci_data_version.dv_data_version = 0;
2833 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2834 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2837 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2838 result = cl_io_loop(env, io);
2840 result = io->ci_result;
2842 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2843 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2845 cl_io_fini(env, io);
2847 if (unlikely(io->ci_need_restart))
2850 cl_env_put(env, &refcheck);
2856 * Read the data_version for inode.
2858 * This value is computed using stripe object version on OST.
2859 * Version is computed using server side locking.
2861 * @param flags if do sync on the OST side;
2863 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2864 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2866 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2868 struct ioc_data_version ioc = { .idv_flags = flags };
2871 rc = ll_ioc_data_version(inode, &ioc);
2873 *data_version = ioc.idv_version;
2879 * Trigger a HSM release request for the provided inode.
2881 int ll_hsm_release(struct inode *inode)
2884 struct obd_client_handle *och = NULL;
2885 __u64 data_version = 0;
2890 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2891 ll_i2sbi(inode)->ll_fsname,
2892 PFID(&ll_i2info(inode)->lli_fid));
2894 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2896 GOTO(out, rc = PTR_ERR(och));
2898 /* Grab latest data_version and [am]time values */
2899 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2903 env = cl_env_get(&refcheck);
2905 GOTO(out, rc = PTR_ERR(env));
2907 rc = ll_merge_attr(env, inode);
2908 cl_env_put(env, &refcheck);
2910 /* If error happen, we have the wrong size for a file.
2916 /* Release the file.
2917 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2918 * we still need it to pack l_remote_handle to MDT. */
2919 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2925 if (och != NULL && !IS_ERR(och)) /* close the file */
2926 ll_lease_close(och, inode, NULL);
2931 struct ll_swap_stack {
2934 struct inode *inode1;
2935 struct inode *inode2;
2940 static int ll_swap_layouts(struct file *file1, struct file *file2,
2941 struct lustre_swap_layouts *lsl)
2943 struct mdc_swap_layouts msl;
2944 struct md_op_data *op_data;
2947 struct ll_swap_stack *llss = NULL;
2950 OBD_ALLOC_PTR(llss);
2954 llss->inode1 = file_inode(file1);
2955 llss->inode2 = file_inode(file2);
2957 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2961 /* we use 2 bool because it is easier to swap than 2 bits */
2962 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2963 llss->check_dv1 = true;
2965 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2966 llss->check_dv2 = true;
2968 /* we cannot use lsl->sl_dvX directly because we may swap them */
2969 llss->dv1 = lsl->sl_dv1;
2970 llss->dv2 = lsl->sl_dv2;
2972 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2973 if (rc == 0) /* same file, done! */
2976 if (rc < 0) { /* sequentialize it */
2977 swap(llss->inode1, llss->inode2);
2979 swap(llss->dv1, llss->dv2);
2980 swap(llss->check_dv1, llss->check_dv2);
2984 if (gid != 0) { /* application asks to flush dirty cache */
2985 rc = ll_get_grouplock(llss->inode1, file1, gid);
2989 rc = ll_get_grouplock(llss->inode2, file2, gid);
2991 ll_put_grouplock(llss->inode1, file1, gid);
2996 /* ultimate check, before swaping the layouts we check if
2997 * dataversion has changed (if requested) */
2998 if (llss->check_dv1) {
2999 rc = ll_data_version(llss->inode1, &dv, 0);
3002 if (dv != llss->dv1)
3003 GOTO(putgl, rc = -EAGAIN);
3006 if (llss->check_dv2) {
3007 rc = ll_data_version(llss->inode2, &dv, 0);
3010 if (dv != llss->dv2)
3011 GOTO(putgl, rc = -EAGAIN);
3014 /* struct md_op_data is used to send the swap args to the mdt
3015 * only flags is missing, so we use struct mdc_swap_layouts
3016 * through the md_op_data->op_data */
3017 /* flags from user space have to be converted before they are send to
3018 * server, no flag is sent today, they are only used on the client */
3021 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
3022 0, LUSTRE_OPC_ANY, &msl);
3023 if (IS_ERR(op_data))
3024 GOTO(free, rc = PTR_ERR(op_data));
3026 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
3027 sizeof(*op_data), op_data, NULL);
3028 ll_finish_md_op_data(op_data);
3035 ll_put_grouplock(llss->inode2, file2, gid);
3036 ll_put_grouplock(llss->inode1, file1, gid);
3046 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3048 struct obd_export *exp = ll_i2mdexp(inode);
3049 struct md_op_data *op_data;
3053 /* Detect out-of range masks */
3054 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3057 /* Non-root users are forbidden to set or clear flags which are
3058 * NOT defined in HSM_USER_MASK. */
3059 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3060 !capable(CAP_SYS_ADMIN))
3063 if (!exp_connect_archive_id_array(exp)) {
3064 /* Detect out-of range archive id */
3065 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3066 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3070 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3071 LUSTRE_OPC_ANY, hss);
3072 if (IS_ERR(op_data))
3073 RETURN(PTR_ERR(op_data));
3075 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3078 ll_finish_md_op_data(op_data);
3083 static int ll_hsm_import(struct inode *inode, struct file *file,
3084 struct hsm_user_import *hui)
3086 struct hsm_state_set *hss = NULL;
3087 struct iattr *attr = NULL;
3091 if (!S_ISREG(inode->i_mode))
3097 GOTO(out, rc = -ENOMEM);
3099 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3100 hss->hss_archive_id = hui->hui_archive_id;
3101 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3102 rc = ll_hsm_state_set(inode, hss);
3106 OBD_ALLOC_PTR(attr);
3108 GOTO(out, rc = -ENOMEM);
3110 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3111 attr->ia_mode |= S_IFREG;
3112 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3113 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3114 attr->ia_size = hui->hui_size;
3115 attr->ia_mtime.tv_sec = hui->hui_mtime;
3116 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3117 attr->ia_atime.tv_sec = hui->hui_atime;
3118 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3120 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3121 ATTR_UID | ATTR_GID |
3122 ATTR_MTIME | ATTR_MTIME_SET |
3123 ATTR_ATIME | ATTR_ATIME_SET;
3127 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3131 inode_unlock(inode);
3143 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3145 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3146 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3149 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3151 struct inode *inode = file_inode(file);
3153 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3154 ATTR_MTIME | ATTR_MTIME_SET |
3157 .tv_sec = lfu->lfu_atime_sec,
3158 .tv_nsec = lfu->lfu_atime_nsec,
3161 .tv_sec = lfu->lfu_mtime_sec,
3162 .tv_nsec = lfu->lfu_mtime_nsec,
3165 .tv_sec = lfu->lfu_ctime_sec,
3166 .tv_nsec = lfu->lfu_ctime_nsec,
3172 if (!capable(CAP_SYS_ADMIN))
3175 if (!S_ISREG(inode->i_mode))
3179 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3181 inode_unlock(inode);
3186 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3189 case MODE_READ_USER:
3191 case MODE_WRITE_USER:
3198 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3200 /* Used to allow the upper layers of the client to request an LDLM lock
3201 * without doing an actual read or write.
3203 * Used for ladvise lockahead to manually request specific locks.
3205 * \param[in] file file this ladvise lock request is on
3206 * \param[in] ladvise ladvise struct describing this lock request
3208 * \retval 0 success, no detailed result available (sync requests
3209 * and requests sent to the server [not handled locally]
3210 * cannot return detailed results)
3211 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3212 * see definitions for details.
3213 * \retval negative negative errno on error
3215 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3217 struct lu_env *env = NULL;
3218 struct cl_io *io = NULL;
3219 struct cl_lock *lock = NULL;
3220 struct cl_lock_descr *descr = NULL;
3221 struct dentry *dentry = file->f_path.dentry;
3222 struct inode *inode = dentry->d_inode;
3223 enum cl_lock_mode cl_mode;
3224 off_t start = ladvise->lla_start;
3225 off_t end = ladvise->lla_end;
3232 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3233 dentry, dentry->d_inode,
3234 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3237 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3239 GOTO(out, result = cl_mode);
3241 /* Get IO environment */
3242 result = cl_io_get(inode, &env, &io, &refcheck);
3246 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3249 * nothing to do for this io. This currently happens when
3250 * stripe sub-object's are not yet created.
3252 result = io->ci_result;
3253 } else if (result == 0) {
3254 lock = vvp_env_lock(env);
3255 descr = &lock->cll_descr;
3257 descr->cld_obj = io->ci_obj;
3258 /* Convert byte offsets to pages */
3259 descr->cld_start = cl_index(io->ci_obj, start);
3260 descr->cld_end = cl_index(io->ci_obj, end);
3261 descr->cld_mode = cl_mode;
3262 /* CEF_MUST is used because we do not want to convert a
3263 * lockahead request to a lockless lock */
3264 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3266 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3267 descr->cld_enq_flags |= CEF_SPECULATIVE;
3269 result = cl_lock_request(env, io, lock);
3271 /* On success, we need to release the lock */
3273 cl_lock_release(env, lock);
3275 cl_io_fini(env, io);
3276 cl_env_put(env, &refcheck);
3278 /* -ECANCELED indicates a matching lock with a different extent
3279 * was already present, and -EEXIST indicates a matching lock
3280 * on exactly the same extent was already present.
3281 * We convert them to positive values for userspace to make
3282 * recognizing true errors easier.
3283 * Note we can only return these detailed results on async requests,
3284 * as sync requests look the same as i/o requests for locking. */
3285 if (result == -ECANCELED)
3286 result = LLA_RESULT_DIFFERENT;
3287 else if (result == -EEXIST)
3288 result = LLA_RESULT_SAME;
3293 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3295 static int ll_ladvise_sanity(struct inode *inode,
3296 struct llapi_lu_ladvise *ladvise)
3298 struct ll_sb_info *sbi = ll_i2sbi(inode);
3299 enum lu_ladvise_type advice = ladvise->lla_advice;
3300 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3301 * be in the first 32 bits of enum ladvise_flags */
3302 __u32 flags = ladvise->lla_peradvice_flags;
3303 /* 3 lines at 80 characters per line, should be plenty */
3306 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3309 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3310 sbi->ll_fsname, advice,
3311 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3315 /* Per-advice checks */
3317 case LU_LADVISE_LOCKNOEXPAND:
3318 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3320 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3321 "rc = %d\n", sbi->ll_fsname, flags,
3322 ladvise_names[advice], rc);
3326 case LU_LADVISE_LOCKAHEAD:
3327 /* Currently only READ and WRITE modes can be requested */
3328 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3329 ladvise->lla_lockahead_mode == 0) {
3331 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3332 "rc = %d\n", sbi->ll_fsname,
3333 ladvise->lla_lockahead_mode,
3334 ladvise_names[advice], rc);
3338 case LU_LADVISE_WILLREAD:
3339 case LU_LADVISE_DONTNEED:
3341 /* Note fall through above - These checks apply to all advices
3342 * except LOCKNOEXPAND */
3343 if (flags & ~LF_DEFAULT_MASK) {
3345 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3346 "rc = %d\n", sbi->ll_fsname, flags,
3347 ladvise_names[advice], rc);
3350 if (ladvise->lla_start >= ladvise->lla_end) {
3352 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3353 "for %s: rc = %d\n", sbi->ll_fsname,
3354 ladvise->lla_start, ladvise->lla_end,
3355 ladvise_names[advice], rc);
3367 * Give file access advices
3369 * The ladvise interface is similar to Linux fadvise() system call, except it
3370 * forwards the advices directly from Lustre client to server. The server side
3371 * codes will apply appropriate read-ahead and caching techniques for the
3372 * corresponding files.
3374 * A typical workload for ladvise is e.g. a bunch of different clients are
3375 * doing small random reads of a file, so prefetching pages into OSS cache
3376 * with big linear reads before the random IO is a net benefit. Fetching
3377 * all that data into each client cache with fadvise() may not be, due to
3378 * much more data being sent to the client.
3380 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3381 struct llapi_lu_ladvise *ladvise)
3385 struct cl_ladvise_io *lio;
3390 env = cl_env_get(&refcheck);
3392 RETURN(PTR_ERR(env));
3394 io = vvp_env_thread_io(env);
3395 io->ci_obj = ll_i2info(inode)->lli_clob;
3397 /* initialize parameters for ladvise */
3398 lio = &io->u.ci_ladvise;
3399 lio->li_start = ladvise->lla_start;
3400 lio->li_end = ladvise->lla_end;
3401 lio->li_fid = ll_inode2fid(inode);
3402 lio->li_advice = ladvise->lla_advice;
3403 lio->li_flags = flags;
3405 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3406 rc = cl_io_loop(env, io);
3410 cl_io_fini(env, io);
3411 cl_env_put(env, &refcheck);
3415 static int ll_lock_noexpand(struct file *file, int flags)
3417 struct ll_file_data *fd = file->private_data;
3419 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3424 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3427 struct fsxattr fsxattr;
3429 if (copy_from_user(&fsxattr,
3430 (const struct fsxattr __user *)arg,
3434 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3435 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3436 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3437 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3438 if (copy_to_user((struct fsxattr __user *)arg,
3439 &fsxattr, sizeof(fsxattr)))
3445 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3449 * Project Quota ID state is only allowed to change from within the init
3450 * namespace. Enforce that restriction only if we are trying to change
3451 * the quota ID state. Everything else is allowed in user namespaces.
3453 if (current_user_ns() == &init_user_ns) {
3455 * Caller is allowed to change the project ID. if it is being
3456 * changed, make sure that the new value is valid.
3458 if (ll_i2info(inode)->lli_projid != projid &&
3459 !projid_valid(make_kprojid(&init_user_ns, projid)))
3465 if (ll_i2info(inode)->lli_projid != projid)
3468 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3469 if (!(xflags & FS_XFLAG_PROJINHERIT))
3472 if (xflags & FS_XFLAG_PROJINHERIT)
3479 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3481 struct md_op_data *op_data;
3482 struct ptlrpc_request *req = NULL;
3483 struct cl_object *obj;
3484 unsigned int inode_flags;
3487 rc = ll_ioctl_check_project(inode, xflags, projid);
3491 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3492 LUSTRE_OPC_ANY, NULL);
3493 if (IS_ERR(op_data))
3494 RETURN(PTR_ERR(op_data));
3496 inode_flags = ll_xflags_to_inode_flags(xflags);
3497 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3498 if (xflags & FS_XFLAG_PROJINHERIT)
3499 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3500 op_data->op_projid = projid;
3501 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3502 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3503 ptlrpc_req_finished(req);
3505 GOTO(out_fsxattr, rc);
3506 ll_update_inode_flags(inode, op_data->op_attr_flags);
3508 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3509 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3510 GOTO(out_fsxattr, rc);
3512 obj = ll_i2info(inode)->lli_clob;
3514 struct iattr attr = { 0 };
3516 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3520 ll_finish_md_op_data(op_data);
3524 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3527 struct fsxattr fsxattr;
3531 if (copy_from_user(&fsxattr,
3532 (const struct fsxattr __user *)arg,
3536 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3537 fsxattr.fsx_projid));
3540 int ll_ioctl_project(struct file *file, unsigned int cmd,
3543 struct lu_project lu_project;
3544 struct dentry *dentry = file_dentry(file);
3545 struct inode *inode = file_inode(file);
3546 struct dentry *child_dentry = NULL;
3547 int rc = 0, name_len;
3549 if (copy_from_user(&lu_project,
3550 (const struct lu_project __user *)arg,
3551 sizeof(lu_project)))
3554 /* apply child dentry if name is valid */
3555 name_len = strnlen(lu_project.project_name, NAME_MAX);
3556 if (name_len > 0 && name_len <= NAME_MAX) {
3558 child_dentry = lookup_one_len(lu_project.project_name,
3560 inode_unlock(inode);
3561 if (IS_ERR(child_dentry)) {
3562 rc = PTR_ERR(child_dentry);
3565 inode = child_dentry->d_inode;
3570 } else if (name_len > NAME_MAX) {
3575 switch (lu_project.project_type) {
3576 case LU_PROJECT_SET:
3577 rc = ll_set_project(inode, lu_project.project_xflags,
3578 lu_project.project_id);
3580 case LU_PROJECT_GET:
3581 lu_project.project_xflags =
3582 ll_inode_flags_to_xflags(inode->i_flags);
3583 if (test_bit(LLIF_PROJECT_INHERIT,
3584 &ll_i2info(inode)->lli_flags))
3585 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3586 lu_project.project_id = ll_i2info(inode)->lli_projid;
3587 if (copy_to_user((struct lu_project __user *)arg,
3588 &lu_project, sizeof(lu_project))) {
3598 if (!IS_ERR_OR_NULL(child_dentry))
3603 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3606 struct inode *inode = file_inode(file);
3607 struct ll_file_data *fd = file->private_data;
3608 struct ll_inode_info *lli = ll_i2info(inode);
3609 struct obd_client_handle *och = NULL;
3610 struct split_param sp;
3611 struct pcc_param param;
3612 bool lease_broken = false;
3614 enum mds_op_bias bias = 0;
3615 struct file *layout_file = NULL;
3617 size_t data_size = 0;
3618 bool attached = false;
3623 mutex_lock(&lli->lli_och_mutex);
3624 if (fd->fd_lease_och != NULL) {
3625 och = fd->fd_lease_och;
3626 fd->fd_lease_och = NULL;
3628 mutex_unlock(&lli->lli_och_mutex);
3633 fmode = och->och_flags;
3635 switch (ioc->lil_flags) {
3636 case LL_LEASE_RESYNC_DONE:
3637 if (ioc->lil_count > IOC_IDS_MAX)
3638 GOTO(out_lease_close, rc = -EINVAL);
3640 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3641 OBD_ALLOC(data, data_size);
3643 GOTO(out_lease_close, rc = -ENOMEM);
3645 if (copy_from_user(data, (void __user *)arg, data_size))
3646 GOTO(out_lease_close, rc = -EFAULT);
3648 bias = MDS_CLOSE_RESYNC_DONE;
3650 case LL_LEASE_LAYOUT_MERGE: {
3653 if (ioc->lil_count != 1)
3654 GOTO(out_lease_close, rc = -EINVAL);
3656 arg += sizeof(*ioc);
3657 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3658 GOTO(out_lease_close, rc = -EFAULT);
3660 layout_file = fget(fd);
3662 GOTO(out_lease_close, rc = -EBADF);
3664 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3665 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3666 GOTO(out_lease_close, rc = -EPERM);
3668 data = file_inode(layout_file);
3669 bias = MDS_CLOSE_LAYOUT_MERGE;
3672 case LL_LEASE_LAYOUT_SPLIT: {
3676 if (ioc->lil_count != 2)
3677 GOTO(out_lease_close, rc = -EINVAL);
3679 arg += sizeof(*ioc);
3680 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3681 GOTO(out_lease_close, rc = -EFAULT);
3683 arg += sizeof(__u32);
3684 if (copy_from_user(&mirror_id, (void __user *)arg,
3686 GOTO(out_lease_close, rc = -EFAULT);
3688 layout_file = fget(fdv);
3690 GOTO(out_lease_close, rc = -EBADF);
3692 /* if layout_file == file, it means to destroy the mirror */
3693 sp.sp_inode = file_inode(layout_file);
3694 sp.sp_mirror_id = (__u16)mirror_id;
3696 bias = MDS_CLOSE_LAYOUT_SPLIT;
3699 case LL_LEASE_PCC_ATTACH:
3700 if (ioc->lil_count != 1)
3703 if (IS_ENCRYPTED(inode))
3704 RETURN(-EOPNOTSUPP);
3706 arg += sizeof(*ioc);
3707 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3709 GOTO(out_lease_close, rc2 = -EFAULT);
3711 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3713 GOTO(out_lease_close, rc2);
3716 /* Grab latest data version */
3717 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3720 GOTO(out_lease_close, rc2);
3723 bias = MDS_PCC_ATTACH;
3726 /* without close intent */
3731 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3735 rc = ll_lease_och_release(inode, file);
3744 switch (ioc->lil_flags) {
3745 case LL_LEASE_RESYNC_DONE:
3747 OBD_FREE(data, data_size);
3749 case LL_LEASE_LAYOUT_MERGE:
3750 case LL_LEASE_LAYOUT_SPLIT:
3754 ll_layout_refresh(inode, &fd->fd_layout_version);
3756 case LL_LEASE_PCC_ATTACH:
3759 rc = pcc_readwrite_attach_fini(file, inode,
3760 param.pa_layout_gen,
3767 rc = ll_lease_type_from_fmode(fmode);
3771 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3774 struct inode *inode = file_inode(file);
3775 struct ll_inode_info *lli = ll_i2info(inode);
3776 struct ll_file_data *fd = file->private_data;
3777 struct obd_client_handle *och = NULL;
3778 __u64 open_flags = 0;
3784 switch (ioc->lil_mode) {
3785 case LL_LEASE_WRLCK:
3786 if (!(file->f_mode & FMODE_WRITE))
3788 fmode = FMODE_WRITE;
3790 case LL_LEASE_RDLCK:
3791 if (!(file->f_mode & FMODE_READ))
3795 case LL_LEASE_UNLCK:
3796 RETURN(ll_file_unlock_lease(file, ioc, arg));
3801 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3803 /* apply for lease */
3804 if (ioc->lil_flags & LL_LEASE_RESYNC)
3805 open_flags = MDS_OPEN_RESYNC;
3806 och = ll_lease_open(inode, file, fmode, open_flags);
3808 RETURN(PTR_ERR(och));
3810 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3811 rc = ll_lease_file_resync(och, inode, arg);
3813 ll_lease_close(och, inode, NULL);
3816 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3818 ll_lease_close(och, inode, NULL);
3824 mutex_lock(&lli->lli_och_mutex);
3825 if (fd->fd_lease_och == NULL) {
3826 fd->fd_lease_och = och;
3829 mutex_unlock(&lli->lli_och_mutex);
3831 /* impossible now that only excl is supported for now */
3832 ll_lease_close(och, inode, &lease_broken);
3838 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3840 struct ll_inode_info *lli = ll_i2info(inode);
3841 struct ll_sb_info *sbi = ll_i2sbi(inode);
3842 __u64 now = ktime_get_real_seconds();
3845 spin_lock(&lli->lli_heat_lock);
3846 heat->lh_flags = lli->lli_heat_flags;
3847 for (i = 0; i < heat->lh_count; i++)
3848 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3849 now, sbi->ll_heat_decay_weight,
3850 sbi->ll_heat_period_second);
3851 spin_unlock(&lli->lli_heat_lock);
3854 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3856 struct ll_inode_info *lli = ll_i2info(inode);
3859 spin_lock(&lli->lli_heat_lock);
3860 if (flags & LU_HEAT_FLAG_CLEAR)
3861 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3863 if (flags & LU_HEAT_FLAG_OFF)
3864 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3866 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3868 spin_unlock(&lli->lli_heat_lock);
3874 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3876 struct inode *inode = file_inode(file);
3877 struct ll_file_data *fd = file->private_data;
3881 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3882 PFID(ll_inode2fid(inode)), inode, cmd);
3883 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3885 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3886 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3890 case LL_IOC_GETFLAGS:
3891 /* Get the current value of the file flags */
3892 return put_user(fd->fd_flags, (int __user *)arg);
3893 case LL_IOC_SETFLAGS:
3894 case LL_IOC_CLRFLAGS:
3895 /* Set or clear specific file flags */
3896 /* XXX This probably needs checks to ensure the flags are
3897 * not abused, and to handle any flag side effects.
3899 if (get_user(flags, (int __user *) arg))
3902 if (cmd == LL_IOC_SETFLAGS) {
3903 if ((flags & LL_FILE_IGNORE_LOCK) &&
3904 !(file->f_flags & O_DIRECT)) {
3905 CERROR("%s: unable to disable locking on "
3906 "non-O_DIRECT file\n", current->comm);
3910 fd->fd_flags |= flags;
3912 fd->fd_flags &= ~flags;
3915 case LL_IOC_LOV_SETSTRIPE:
3916 case LL_IOC_LOV_SETSTRIPE_NEW:
3917 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3918 case LL_IOC_LOV_SETEA:
3919 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3920 case LL_IOC_LOV_SWAP_LAYOUTS: {
3922 struct lustre_swap_layouts lsl;
3924 if (copy_from_user(&lsl, (char __user *)arg,
3925 sizeof(struct lustre_swap_layouts)))
3928 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3931 file2 = fget(lsl.sl_fd);
3935 /* O_WRONLY or O_RDWR */
3936 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3937 GOTO(out, rc = -EPERM);
3939 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3940 struct inode *inode2;
3941 struct ll_inode_info *lli;
3942 struct obd_client_handle *och = NULL;
3944 lli = ll_i2info(inode);
3945 mutex_lock(&lli->lli_och_mutex);
3946 if (fd->fd_lease_och != NULL) {
3947 och = fd->fd_lease_och;
3948 fd->fd_lease_och = NULL;
3950 mutex_unlock(&lli->lli_och_mutex);
3952 GOTO(out, rc = -ENOLCK);
3953 inode2 = file_inode(file2);
3954 rc = ll_swap_layouts_close(och, inode, inode2);
3956 rc = ll_swap_layouts(file, file2, &lsl);
3962 case LL_IOC_LOV_GETSTRIPE:
3963 case LL_IOC_LOV_GETSTRIPE_NEW:
3964 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3965 case FS_IOC_GETFLAGS:
3966 case FS_IOC_SETFLAGS:
3967 RETURN(ll_iocontrol(inode, file, cmd, arg));
3968 case FSFILT_IOC_GETVERSION:
3969 case FS_IOC_GETVERSION:
3970 RETURN(put_user(inode->i_generation, (int __user *)arg));
3971 /* We need to special case any other ioctls we want to handle,
3972 * to send them to the MDS/OST as appropriate and to properly
3973 * network encode the arg field. */
3974 case FS_IOC_SETVERSION:
3977 case LL_IOC_GROUP_LOCK:
3978 RETURN(ll_get_grouplock(inode, file, arg));
3979 case LL_IOC_GROUP_UNLOCK:
3980 RETURN(ll_put_grouplock(inode, file, arg));
3981 case IOC_OBD_STATFS:
3982 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3984 case LL_IOC_FLUSHCTX:
3985 RETURN(ll_flush_ctx(inode));
3986 case LL_IOC_PATH2FID: {
3987 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3988 sizeof(struct lu_fid)))
3993 case LL_IOC_GETPARENT:
3994 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3996 case OBD_IOC_FID2PATH:
3997 RETURN(ll_fid2path(inode, (void __user *)arg));
3998 case LL_IOC_DATA_VERSION: {
3999 struct ioc_data_version idv;
4002 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
4005 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
4006 rc = ll_ioc_data_version(inode, &idv);
4009 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
4015 case LL_IOC_GET_MDTIDX: {
4018 mdtidx = ll_get_mdt_idx(inode);
4022 if (put_user((int)mdtidx, (int __user *)arg))
4027 case OBD_IOC_GETNAME_OLD:
4029 case OBD_IOC_GETDTNAME:
4031 case OBD_IOC_GETMDNAME:
4032 RETURN(ll_get_obd_name(inode, cmd, arg));
4033 case LL_IOC_HSM_STATE_GET: {
4034 struct md_op_data *op_data;
4035 struct hsm_user_state *hus;
4042 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4043 LUSTRE_OPC_ANY, hus);
4044 if (IS_ERR(op_data)) {
4046 RETURN(PTR_ERR(op_data));
4049 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4052 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4055 ll_finish_md_op_data(op_data);
4059 case LL_IOC_HSM_STATE_SET: {
4060 struct hsm_state_set *hss;
4067 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4072 rc = ll_hsm_state_set(inode, hss);
4077 case LL_IOC_HSM_ACTION: {
4078 struct md_op_data *op_data;
4079 struct hsm_current_action *hca;
4087 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4088 LUSTRE_OPC_ANY, hca);
4089 if (IS_ERR(op_data)) {
4091 RETURN(PTR_ERR(op_data));
4094 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4097 GOTO(skip_copy, rc);
4099 /* The hsm_current_action retreived from the server could
4100 * contain corrupt information. If it is incorrect data collect
4101 * debug information. We still send the data even if incorrect
4102 * to user land to handle.
4104 action = hsm_user_action2name(hca->hca_action);
4105 if (strcmp(action, "UNKNOWN") == 0 ||
4106 hca->hca_state > HPS_DONE) {
4108 "HSM current state %s action %s, offset = %llu, length %llu\n",
4109 hsm_progress_state2name(hca->hca_state), action,
4110 hca->hca_location.offset, hca->hca_location.length);
4113 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4116 ll_finish_md_op_data(op_data);
4120 case LL_IOC_SET_LEASE_OLD: {
4121 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4123 RETURN(ll_file_set_lease(file, &ioc, 0));
4125 case LL_IOC_SET_LEASE: {
4126 struct ll_ioc_lease ioc;
4128 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4131 RETURN(ll_file_set_lease(file, &ioc, arg));
4133 case LL_IOC_GET_LEASE: {
4134 struct ll_inode_info *lli = ll_i2info(inode);
4135 struct ldlm_lock *lock = NULL;
4138 mutex_lock(&lli->lli_och_mutex);
4139 if (fd->fd_lease_och != NULL) {
4140 struct obd_client_handle *och = fd->fd_lease_och;
4142 lock = ldlm_handle2lock(&och->och_lease_handle);
4144 lock_res_and_lock(lock);
4145 if (!ldlm_is_cancel(lock))
4146 fmode = och->och_flags;
4148 unlock_res_and_lock(lock);
4149 LDLM_LOCK_PUT(lock);
4152 mutex_unlock(&lli->lli_och_mutex);
4154 RETURN(ll_lease_type_from_fmode(fmode));
4156 case LL_IOC_HSM_IMPORT: {
4157 struct hsm_user_import *hui;
4163 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4168 rc = ll_hsm_import(inode, file, hui);
4173 case LL_IOC_FUTIMES_3: {
4174 struct ll_futimes_3 lfu;
4176 if (copy_from_user(&lfu,
4177 (const struct ll_futimes_3 __user *)arg,
4181 RETURN(ll_file_futimes_3(file, &lfu));
4183 case LL_IOC_LADVISE: {
4184 struct llapi_ladvise_hdr *k_ladvise_hdr;
4185 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4188 int alloc_size = sizeof(*k_ladvise_hdr);
4191 u_ladvise_hdr = (void __user *)arg;
4192 OBD_ALLOC_PTR(k_ladvise_hdr);
4193 if (k_ladvise_hdr == NULL)
4196 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4197 GOTO(out_ladvise, rc = -EFAULT);
4199 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4200 k_ladvise_hdr->lah_count < 1)
4201 GOTO(out_ladvise, rc = -EINVAL);
4203 num_advise = k_ladvise_hdr->lah_count;
4204 if (num_advise >= LAH_COUNT_MAX)
4205 GOTO(out_ladvise, rc = -EFBIG);
4207 OBD_FREE_PTR(k_ladvise_hdr);
4208 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4209 lah_advise[num_advise]);
4210 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4211 if (k_ladvise_hdr == NULL)
4215 * TODO: submit multiple advices to one server in a single RPC
4217 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4218 GOTO(out_ladvise, rc = -EFAULT);
4220 for (i = 0; i < num_advise; i++) {
4221 struct llapi_lu_ladvise *k_ladvise =
4222 &k_ladvise_hdr->lah_advise[i];
4223 struct llapi_lu_ladvise __user *u_ladvise =
4224 &u_ladvise_hdr->lah_advise[i];
4226 rc = ll_ladvise_sanity(inode, k_ladvise);
4228 GOTO(out_ladvise, rc);
4230 switch (k_ladvise->lla_advice) {
4231 case LU_LADVISE_LOCKNOEXPAND:
4232 rc = ll_lock_noexpand(file,
4233 k_ladvise->lla_peradvice_flags);
4234 GOTO(out_ladvise, rc);
4235 case LU_LADVISE_LOCKAHEAD:
4237 rc = ll_file_lock_ahead(file, k_ladvise);
4240 GOTO(out_ladvise, rc);
4243 &u_ladvise->lla_lockahead_result))
4244 GOTO(out_ladvise, rc = -EFAULT);
4247 rc = ll_ladvise(inode, file,
4248 k_ladvise_hdr->lah_flags,
4251 GOTO(out_ladvise, rc);
4258 OBD_FREE(k_ladvise_hdr, alloc_size);
4261 case LL_IOC_FLR_SET_MIRROR: {
4262 /* mirror I/O must be direct to avoid polluting page cache
4264 if (!(file->f_flags & O_DIRECT))
4267 fd->fd_designated_mirror = (__u32)arg;
4270 case FS_IOC_FSGETXATTR:
4271 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4272 case FS_IOC_FSSETXATTR:
4273 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4274 case LL_IOC_PROJECT:
4275 RETURN(ll_ioctl_project(file, cmd, arg));
4277 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4278 case LL_IOC_HEAT_GET: {
4279 struct lu_heat uheat;
4280 struct lu_heat *heat;
4283 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4286 if (uheat.lh_count > OBD_HEAT_COUNT)
4287 uheat.lh_count = OBD_HEAT_COUNT;
4289 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4290 OBD_ALLOC(heat, size);
4294 heat->lh_count = uheat.lh_count;
4295 ll_heat_get(inode, heat);
4296 rc = copy_to_user((char __user *)arg, heat, size);
4297 OBD_FREE(heat, size);
4298 RETURN(rc ? -EFAULT : 0);
4300 case LL_IOC_HEAT_SET: {
4303 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4306 rc = ll_heat_set(inode, flags);
4309 case LL_IOC_PCC_DETACH: {
4310 struct lu_pcc_detach *detach;
4312 OBD_ALLOC_PTR(detach);
4316 if (copy_from_user(detach,
4317 (const struct lu_pcc_detach __user *)arg,
4319 GOTO(out_detach_free, rc = -EFAULT);
4321 if (!S_ISREG(inode->i_mode))
4322 GOTO(out_detach_free, rc = -EINVAL);
4324 if (!inode_owner_or_capable(inode))
4325 GOTO(out_detach_free, rc = -EPERM);
4327 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4329 OBD_FREE_PTR(detach);
4332 case LL_IOC_PCC_STATE: {
4333 struct lu_pcc_state __user *ustate =
4334 (struct lu_pcc_state __user *)arg;
4335 struct lu_pcc_state *state;
4337 OBD_ALLOC_PTR(state);
4341 if (copy_from_user(state, ustate, sizeof(*state)))
4342 GOTO(out_state, rc = -EFAULT);
4344 rc = pcc_ioctl_state(file, inode, state);
4346 GOTO(out_state, rc);
4348 if (copy_to_user(ustate, state, sizeof(*state)))
4349 GOTO(out_state, rc = -EFAULT);
4352 OBD_FREE_PTR(state);
4355 #ifdef HAVE_LUSTRE_CRYPTO
4356 case LL_IOC_SET_ENCRYPTION_POLICY:
4357 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4359 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4360 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4361 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4363 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4364 case LL_IOC_ADD_ENCRYPTION_KEY:
4365 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4367 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4368 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4369 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4371 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4372 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4373 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4375 return llcrypt_ioctl_remove_key_all_users(file,
4376 (void __user *)arg);
4377 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4378 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4380 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4383 case LL_IOC_UNLOCK_FOREIGN: {
4384 struct dentry *dentry = file_dentry(file);
4386 /* if not a foreign symlink do nothing */
4387 if (ll_foreign_is_removable(dentry, true)) {
4389 "prevent unlink of non-foreign file ("DFID")\n",
4390 PFID(ll_inode2fid(inode)));
4391 RETURN(-EOPNOTSUPP);
4397 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4398 (void __user *)arg));
4402 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4404 struct inode *inode = file_inode(file);
4407 struct cl_lseek_io *lsio;
4414 env = cl_env_get(&refcheck);
4416 RETURN(PTR_ERR(env));
4418 io = vvp_env_thread_io(env);
4419 io->ci_obj = ll_i2info(inode)->lli_clob;
4420 ll_io_set_mirror(io, file);
4422 lsio = &io->u.ci_lseek;
4423 lsio->ls_start = offset;
4424 lsio->ls_whence = whence;
4425 lsio->ls_result = -ENXIO;
4428 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4430 struct vvp_io *vio = vvp_env_io(env);
4432 vio->vui_fd = file->private_data;
4433 rc = cl_io_loop(env, io);
4437 retval = rc ? : lsio->ls_result;
4438 cl_io_fini(env, io);
4439 } while (unlikely(io->ci_need_restart));
4441 cl_env_put(env, &refcheck);
4443 /* Without the key, SEEK_HOLE return value has to be
4444 * rounded up to next LUSTRE_ENCRYPTION_UNIT_SIZE.
4446 if (llcrypt_require_key(inode) == -ENOKEY && whence == SEEK_HOLE)
4447 retval = round_up(retval, LUSTRE_ENCRYPTION_UNIT_SIZE);
4452 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4454 struct inode *inode = file_inode(file);
4455 loff_t retval = offset, eof = 0;
4456 ktime_t kstart = ktime_get();
4460 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4461 PFID(ll_inode2fid(inode)), inode, retval, retval,
4464 if (origin == SEEK_END) {
4465 retval = ll_glimpse_size(inode);
4468 eof = i_size_read(inode);
4471 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4475 /* flush local cache first if any */
4476 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4479 retval = ll_lseek(file, offset, origin);
4482 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4484 retval = generic_file_llseek_size(file, offset, origin,
4485 ll_file_maxbytes(inode), eof);
4488 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4489 ktime_us_delta(ktime_get(), kstart));
4493 static int ll_flush(struct file *file, fl_owner_t id)
4495 struct inode *inode = file_inode(file);
4496 struct ll_inode_info *lli = ll_i2info(inode);
4497 struct ll_file_data *fd = file->private_data;
4500 LASSERT(!S_ISDIR(inode->i_mode));
4502 /* catch async errors that were recorded back when async writeback
4503 * failed for pages in this mapping. */
4504 rc = lli->lli_async_rc;
4505 lli->lli_async_rc = 0;
4506 if (lli->lli_clob != NULL) {
4507 err = lov_read_and_clear_async_rc(lli->lli_clob);
4512 /* The application has been told write failure already.
4513 * Do not report failure again. */
4514 if (fd->fd_write_failed)
4516 return rc ? -EIO : 0;
4520 * Called to make sure a portion of file has been written out.
4521 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4523 * Return how many pages have been written.
4525 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4526 enum cl_fsync_mode mode, int ignore_layout)
4530 struct cl_fsync_io *fio;
4535 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4536 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4539 env = cl_env_get(&refcheck);
4541 RETURN(PTR_ERR(env));
4543 io = vvp_env_thread_io(env);
4544 io->ci_obj = ll_i2info(inode)->lli_clob;
4545 io->ci_ignore_layout = ignore_layout;
4547 /* initialize parameters for sync */
4548 fio = &io->u.ci_fsync;
4549 fio->fi_start = start;
4551 fio->fi_fid = ll_inode2fid(inode);
4552 fio->fi_mode = mode;
4553 fio->fi_nr_written = 0;
4555 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4556 result = cl_io_loop(env, io);
4558 result = io->ci_result;
4560 result = fio->fi_nr_written;
4561 cl_io_fini(env, io);
4562 cl_env_put(env, &refcheck);
4568 * When dentry is provided (the 'else' case), file_dentry() may be
4569 * null and dentry must be used directly rather than pulled from
4570 * file_dentry() as is done otherwise.
4573 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4575 struct dentry *dentry = file_dentry(file);
4576 struct inode *inode = dentry->d_inode;
4577 struct ll_inode_info *lli = ll_i2info(inode);
4578 struct ptlrpc_request *req;
4579 ktime_t kstart = ktime_get();
4585 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4586 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4588 /* fsync's caller has already called _fdata{sync,write}, we want
4589 * that IO to finish before calling the osc and mdc sync methods */
4590 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4592 /* catch async errors that were recorded back when async writeback
4593 * failed for pages in this mapping. */
4594 if (!S_ISDIR(inode->i_mode)) {
4595 err = lli->lli_async_rc;
4596 lli->lli_async_rc = 0;
4599 if (lli->lli_clob != NULL) {
4600 err = lov_read_and_clear_async_rc(lli->lli_clob);
4606 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4610 ptlrpc_req_finished(req);
4612 if (S_ISREG(inode->i_mode)) {
4613 struct ll_file_data *fd = file->private_data;
4616 /* Sync metadata on MDT first, and then sync the cached data
4619 err = pcc_fsync(file, start, end, datasync, &cached);
4621 err = cl_sync_file_range(inode, start, end,
4623 if (rc == 0 && err < 0)
4626 fd->fd_write_failed = true;
4628 fd->fd_write_failed = false;
4632 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4633 ktime_us_delta(ktime_get(), kstart));
4638 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4640 struct inode *inode = file_inode(file);
4641 struct ll_sb_info *sbi = ll_i2sbi(inode);
4642 struct ldlm_enqueue_info einfo = {
4643 .ei_type = LDLM_FLOCK,
4644 .ei_cb_cp = ldlm_flock_completion_ast,
4645 .ei_cbdata = file_lock,
4647 struct md_op_data *op_data;
4648 struct lustre_handle lockh = { 0 };
4649 union ldlm_policy_data flock = { { 0 } };
4650 int fl_type = file_lock->fl_type;
4651 ktime_t kstart = ktime_get();
4657 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4658 PFID(ll_inode2fid(inode)), file_lock);
4660 if (file_lock->fl_flags & FL_FLOCK) {
4661 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4662 /* flocks are whole-file locks */
4663 flock.l_flock.end = OFFSET_MAX;
4664 /* For flocks owner is determined by the local file desctiptor*/
4665 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4666 } else if (file_lock->fl_flags & FL_POSIX) {
4667 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4668 flock.l_flock.start = file_lock->fl_start;
4669 flock.l_flock.end = file_lock->fl_end;
4673 flock.l_flock.pid = file_lock->fl_pid;
4675 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4676 /* Somewhat ugly workaround for svc lockd.
4677 * lockd installs custom fl_lmops->lm_compare_owner that checks
4678 * for the fl_owner to be the same (which it always is on local node
4679 * I guess between lockd processes) and then compares pid.
4680 * As such we assign pid to the owner field to make it all work,
4681 * conflict with normal locks is unlikely since pid space and
4682 * pointer space for current->files are not intersecting */
4683 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4684 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4689 einfo.ei_mode = LCK_PR;
4692 /* An unlock request may or may not have any relation to
4693 * existing locks so we may not be able to pass a lock handle
4694 * via a normal ldlm_lock_cancel() request. The request may even
4695 * unlock a byte range in the middle of an existing lock. In
4696 * order to process an unlock request we need all of the same
4697 * information that is given with a normal read or write record
4698 * lock request. To avoid creating another ldlm unlock (cancel)
4699 * message we'll treat a LCK_NL flock request as an unlock. */
4700 einfo.ei_mode = LCK_NL;
4703 einfo.ei_mode = LCK_PW;
4706 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4721 flags = LDLM_FL_BLOCK_NOWAIT;
4727 flags = LDLM_FL_TEST_LOCK;
4730 CERROR("unknown fcntl lock command: %d\n", cmd);
4734 /* Save the old mode so that if the mode in the lock changes we
4735 * can decrement the appropriate reader or writer refcount. */
4736 file_lock->fl_type = einfo.ei_mode;
4738 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4739 LUSTRE_OPC_ANY, NULL);
4740 if (IS_ERR(op_data))
4741 RETURN(PTR_ERR(op_data));
4743 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4744 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4745 flock.l_flock.pid, flags, einfo.ei_mode,
4746 flock.l_flock.start, flock.l_flock.end);
4748 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4751 /* Restore the file lock type if not TEST lock. */
4752 if (!(flags & LDLM_FL_TEST_LOCK))
4753 file_lock->fl_type = fl_type;
4755 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4756 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4757 !(flags & LDLM_FL_TEST_LOCK))
4758 rc2 = locks_lock_file_wait(file, file_lock);
4760 if ((file_lock->fl_flags & FL_FLOCK) &&
4761 (rc == 0 || file_lock->fl_type == F_UNLCK))
4762 rc2 = flock_lock_file_wait(file, file_lock);
4763 if ((file_lock->fl_flags & FL_POSIX) &&
4764 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4765 !(flags & LDLM_FL_TEST_LOCK))
4766 rc2 = posix_lock_file_wait(file, file_lock);
4767 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4769 if (rc2 && file_lock->fl_type != F_UNLCK) {
4770 einfo.ei_mode = LCK_NL;
4771 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4776 ll_finish_md_op_data(op_data);
4779 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4780 ktime_us_delta(ktime_get(), kstart));
4784 int ll_get_fid_by_name(struct inode *parent, const char *name,
4785 int namelen, struct lu_fid *fid,
4786 struct inode **inode)
4788 struct md_op_data *op_data = NULL;
4789 struct mdt_body *body;
4790 struct ptlrpc_request *req;
4794 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4795 LUSTRE_OPC_ANY, NULL);
4796 if (IS_ERR(op_data))
4797 RETURN(PTR_ERR(op_data));
4799 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4800 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4801 ll_finish_md_op_data(op_data);
4805 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4807 GOTO(out_req, rc = -EFAULT);
4809 *fid = body->mbo_fid1;
4812 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4814 ptlrpc_req_finished(req);
4818 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4819 const char *name, __u32 flags)
4821 struct dentry *dchild = NULL;
4822 struct inode *child_inode = NULL;
4823 struct md_op_data *op_data;
4824 struct ptlrpc_request *request = NULL;
4825 struct obd_client_handle *och = NULL;
4827 struct mdt_body *body;
4828 __u64 data_version = 0;
4829 size_t namelen = strlen(name);
4830 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4831 bool oldformat = false;
4835 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4836 PFID(ll_inode2fid(parent)), name,
4837 lum->lum_stripe_offset, lum->lum_stripe_count);
4839 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4840 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4841 lustre_swab_lmv_user_md(lum);
4843 /* Get child FID first */
4844 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4847 dchild = d_lookup(file_dentry(file), &qstr);
4849 if (dchild->d_inode)
4850 child_inode = igrab(dchild->d_inode);
4855 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4864 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4865 OBD_CONNECT2_DIR_MIGRATE)) {
4866 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4867 ll_dir_striped(child_inode)) {
4868 CERROR("%s: MDT doesn't support stripe directory "
4869 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4870 GOTO(out_iput, rc = -EOPNOTSUPP);
4875 * lfs migrate command needs to be blocked on the client
4876 * by checking the migrate FID against the FID of the
4879 if (is_root_inode(child_inode))
4880 GOTO(out_iput, rc = -EINVAL);
4882 if (IS_ENCRYPTED(parent)) {
4883 if (unlikely(!llcrypt_policy_has_filename_enc(parent)))
4885 } else if (IS_ENCRYPTED(child_inode) &&
4886 unlikely(!llcrypt_policy_has_filename_enc(child_inode))) {
4889 if (unlikely(oldformat)) {
4891 "cannot migrate old format encrypted "DFID", please move to new enc dir first\n",
4892 PFID(ll_inode2fid(child_inode)));
4893 GOTO(out_iput, rc = -EUCLEAN);
4896 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4897 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4898 if (IS_ERR(op_data))
4899 GOTO(out_iput, rc = PTR_ERR(op_data));
4901 inode_lock(child_inode);
4902 op_data->op_fid3 = *ll_inode2fid(child_inode);
4903 if (!fid_is_sane(&op_data->op_fid3)) {
4904 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4905 ll_i2sbi(parent)->ll_fsname, name,
4906 PFID(&op_data->op_fid3));
4907 GOTO(out_unlock, rc = -EINVAL);
4910 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4911 op_data->op_data = lum;
4912 op_data->op_data_size = lumlen;
4914 /* migrate dirent only for subdirs if MDS_MIGRATE_NSONLY set */
4915 if (S_ISDIR(child_inode->i_mode) && (flags & MDS_MIGRATE_NSONLY) &&
4916 lmv_dir_layout_changing(ll_i2info(parent)->lli_lsm_md))
4917 op_data->op_bias |= MDS_MIGRATE_NSONLY;
4920 if (S_ISREG(child_inode->i_mode)) {
4921 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4925 GOTO(out_unlock, rc);
4928 rc = ll_data_version(child_inode, &data_version,
4931 GOTO(out_close, rc);
4933 op_data->op_open_handle = och->och_open_handle;
4934 op_data->op_data_version = data_version;
4935 op_data->op_lease_handle = och->och_lease_handle;
4936 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4938 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4939 och->och_mod->mod_open_req->rq_replay = 0;
4940 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4943 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data,
4944 op_data->op_name, op_data->op_namelen,
4945 op_data->op_name, op_data->op_namelen, &request);
4947 LASSERT(request != NULL);
4948 ll_update_times(request, parent);
4951 if (rc == 0 || rc == -EAGAIN) {
4952 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4953 LASSERT(body != NULL);
4955 /* If the server does release layout lock, then we cleanup
4956 * the client och here, otherwise release it in out_close: */
4957 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4958 obd_mod_put(och->och_mod);
4959 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4961 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4967 if (request != NULL) {
4968 ptlrpc_req_finished(request);
4972 /* Try again if the lease has cancelled. */
4973 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4978 ll_lease_close(och, child_inode, NULL);
4980 clear_nlink(child_inode);
4982 inode_unlock(child_inode);
4983 ll_finish_md_op_data(op_data);
4990 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4992 struct ll_file_data *fd = file->private_data;
4996 * In order to avoid flood of warning messages, only print one message
4997 * for one file. And the entire message rate on the client is limited
4998 * by CDEBUG_LIMIT too.
5000 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
5001 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
5002 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
5003 "flock disabled, mount with '-o [local]flock' to enable\r\n");
5009 * test if some locks matching bits and l_req_mode are acquired
5010 * - bits can be in different locks
5011 * - if found clear the common lock bits in *bits
5012 * - the bits not found, are kept in *bits
5014 * \param bits [IN] searched lock bits [IN]
5015 * \param l_req_mode [IN] searched lock mode
5016 * \retval boolean, true iff all bits are found
5018 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
5020 struct lustre_handle lockh;
5021 union ldlm_policy_data policy;
5022 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
5023 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
5032 fid = &ll_i2info(inode)->lli_fid;
5033 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
5034 ldlm_lockname[mode]);
5036 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
5037 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
5038 policy.l_inodebits.bits = *bits & BIT(i);
5039 if (policy.l_inodebits.bits == 0)
5042 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
5043 &policy, mode, &lockh)) {
5044 struct ldlm_lock *lock;
5046 lock = ldlm_handle2lock(&lockh);
5049 ~(lock->l_policy_data.l_inodebits.bits);
5050 LDLM_LOCK_PUT(lock);
5052 *bits &= ~policy.l_inodebits.bits;
5059 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5060 struct lustre_handle *lockh, __u64 flags,
5061 enum ldlm_mode mode)
5063 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5068 fid = &ll_i2info(inode)->lli_fid;
5069 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5071 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5072 fid, LDLM_IBITS, &policy, mode, lockh);
5077 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5079 /* Already unlinked. Just update nlink and return success */
5080 if (rc == -ENOENT) {
5082 /* If it is striped directory, and there is bad stripe
5083 * Let's revalidate the dentry again, instead of returning
5085 if (ll_dir_striped(inode))
5088 /* This path cannot be hit for regular files unless in
5089 * case of obscure races, so no need to to validate
5091 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5093 } else if (rc != 0) {
5094 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5095 "%s: revalidate FID "DFID" error: rc = %d\n",
5096 ll_i2sbi(inode)->ll_fsname,
5097 PFID(ll_inode2fid(inode)), rc);
5103 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5105 struct inode *parent;
5106 struct inode *inode = dentry->d_inode;
5107 struct obd_export *exp = ll_i2mdexp(inode);
5108 struct lookup_intent oit = {
5111 struct ptlrpc_request *req = NULL;
5112 struct md_op_data *op_data;
5113 const char *name = NULL;
5118 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5119 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5121 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5122 parent = dentry->d_parent->d_inode;
5123 name = dentry->d_name.name;
5124 namelen = dentry->d_name.len;
5129 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5130 LUSTRE_OPC_ANY, NULL);
5131 if (IS_ERR(op_data))
5132 RETURN(PTR_ERR(op_data));
5134 /* Call getattr by fid */
5135 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5136 op_data->op_flags = MF_GETATTR_BY_FID;
5137 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5138 ll_finish_md_op_data(op_data);
5140 rc = ll_inode_revalidate_fini(inode, rc);
5144 rc = ll_revalidate_it_finish(req, &oit, dentry);
5146 ll_intent_release(&oit);
5150 /* Unlinked? Unhash dentry, so it is not picked up later by
5151 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5152 * here to preserve get_cwd functionality on 2.6.
5154 if (!dentry->d_inode->i_nlink)
5155 d_lustre_invalidate(dentry);
5157 ll_lookup_finish_locks(&oit, dentry);
5159 ptlrpc_req_finished(req);
5164 static int ll_merge_md_attr(struct inode *inode)
5166 struct ll_inode_info *lli = ll_i2info(inode);
5167 struct cl_attr attr = { 0 };
5170 LASSERT(lli->lli_lsm_md != NULL);
5172 if (!lmv_dir_striped(lli->lli_lsm_md))
5175 down_read(&lli->lli_lsm_sem);
5176 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5177 &attr, ll_md_blocking_ast);
5178 up_read(&lli->lli_lsm_sem);
5182 spin_lock(&inode->i_lock);
5183 set_nlink(inode, attr.cat_nlink);
5184 spin_unlock(&inode->i_lock);
5186 inode->i_blocks = attr.cat_blocks;
5187 i_size_write(inode, attr.cat_size);
5189 ll_i2info(inode)->lli_atime = attr.cat_atime;
5190 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5191 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5196 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5197 unsigned int flags, bool foreign)
5199 struct inode *inode = de->d_inode;
5200 struct ll_sb_info *sbi = ll_i2sbi(inode);
5201 struct ll_inode_info *lli = ll_i2info(inode);
5202 struct inode *dir = de->d_parent->d_inode;
5203 bool need_glimpse = true;
5204 ktime_t kstart = ktime_get();
5207 /* The OST object(s) determine the file size, blocks and mtime. */
5208 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5209 request_mask & STATX_MTIME))
5210 need_glimpse = false;
5212 if (dentry_may_statahead(dir, de))
5213 ll_start_statahead(dir, de, need_glimpse &&
5214 !(flags & AT_STATX_DONT_SYNC));
5216 if (flags & AT_STATX_DONT_SYNC)
5217 GOTO(fill_attr, rc = 0);
5219 rc = ll_inode_revalidate(de, IT_GETATTR);
5223 /* foreign file/dir are always of zero length, so don't
5224 * need to validate size.
5226 if (S_ISREG(inode->i_mode) && !foreign) {
5230 GOTO(fill_attr, rc);
5232 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5233 if (cached && rc < 0)
5237 GOTO(fill_attr, rc);
5240 * If the returned attr is masked with OBD_MD_FLSIZE &
5241 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5242 * or blocks obtained from MDT is strictly correct, and the file
5243 * is usually not being modified by clients, and the [a|m|c]time
5244 * got from MDT is also strictly correct.
5245 * Under this circumstance, it does not need to send glimpse
5246 * RPCs to OSTs for file attributes such as the size and blocks.
5248 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5249 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5250 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5251 inode->i_mtime.tv_sec = lli->lli_mtime;
5252 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5253 inode->i_atime.tv_sec = lli->lli_atime;
5254 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5255 inode->i_ctime.tv_sec = lli->lli_ctime;
5256 GOTO(fill_attr, rc);
5259 /* In case of restore, the MDT has the right size and has
5260 * already send it back without granting the layout lock,
5261 * inode is up-to-date so glimpse is useless.
5262 * Also to glimpse we need the layout, in case of a running
5263 * restore the MDT holds the layout lock so the glimpse will
5264 * block up to the end of restore (getattr will block)
5266 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5267 rc = ll_glimpse_size(inode);
5272 /* If object isn't regular a file then don't validate size. */
5273 /* foreign dir is not striped dir */
5274 if (ll_dir_striped(inode) && !foreign) {
5275 rc = ll_merge_md_attr(inode);
5280 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5281 inode->i_atime.tv_sec = lli->lli_atime;
5282 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5283 inode->i_mtime.tv_sec = lli->lli_mtime;
5284 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5285 inode->i_ctime.tv_sec = lli->lli_ctime;
5289 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5291 if (ll_need_32bit_api(sbi)) {
5292 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5293 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5294 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5296 stat->ino = inode->i_ino;
5297 stat->dev = inode->i_sb->s_dev;
5298 stat->rdev = inode->i_rdev;
5301 /* foreign symlink to be exposed as a real symlink */
5303 stat->mode = inode->i_mode;
5305 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5307 stat->uid = inode->i_uid;
5308 stat->gid = inode->i_gid;
5309 stat->atime = inode->i_atime;
5310 stat->mtime = inode->i_mtime;
5311 stat->ctime = inode->i_ctime;
5312 /* stat->blksize is used to tell about preferred IO size */
5313 if (sbi->ll_stat_blksize)
5314 stat->blksize = sbi->ll_stat_blksize;
5315 else if (S_ISREG(inode->i_mode))
5316 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5317 LL_MAX_BLKSIZE_BITS);
5319 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5321 stat->nlink = inode->i_nlink;
5322 stat->size = i_size_read(inode);
5323 stat->blocks = inode->i_blocks;
5325 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5326 if (flags & AT_STATX_DONT_SYNC) {
5327 if (stat->size == 0 &&
5328 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5329 stat->size = lli->lli_lazysize;
5330 if (stat->blocks == 0 &&
5331 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5332 stat->blocks = lli->lli_lazyblocks;
5335 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5336 stat->result_mask |= STATX_BTIME;
5337 stat->btime.tv_sec = lli->lli_btime;
5340 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5341 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5342 stat->result_mask &= request_mask;
5345 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5346 ktime_us_delta(ktime_get(), kstart));
5351 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5352 int ll_getattr(const struct path *path, struct kstat *stat,
5353 u32 request_mask, unsigned int flags)
5355 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5359 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5361 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5362 AT_STATX_SYNC_AS_STAT, false);
5366 int cl_falloc(struct file *file, struct inode *inode, int mode, loff_t offset,
5373 loff_t size = i_size_read(inode);
5377 env = cl_env_get(&refcheck);
5379 RETURN(PTR_ERR(env));
5381 io = vvp_env_thread_io(env);
5382 io->ci_obj = ll_i2info(inode)->lli_clob;
5383 ll_io_set_mirror(io, file);
5385 io->ci_verify_layout = 1;
5386 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5387 io->u.ci_setattr.sa_falloc_mode = mode;
5388 io->u.ci_setattr.sa_falloc_offset = offset;
5389 io->u.ci_setattr.sa_falloc_end = offset + len;
5390 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5392 CDEBUG(D_INODE, "UID %u GID %u\n",
5393 from_kuid(&init_user_ns, inode->i_uid),
5394 from_kgid(&init_user_ns, inode->i_gid));
5396 io->u.ci_setattr.sa_falloc_uid = from_kuid(&init_user_ns, inode->i_uid);
5397 io->u.ci_setattr.sa_falloc_gid = from_kgid(&init_user_ns, inode->i_gid);
5399 if (io->u.ci_setattr.sa_falloc_end > size) {
5400 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5402 /* Check new size against VFS/VM file size limit and rlimit */
5403 rc = inode_newsize_ok(inode, newsize);
5406 if (newsize > ll_file_maxbytes(inode)) {
5407 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5408 (unsigned long long)newsize,
5409 ll_file_maxbytes(inode));
5416 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5418 rc = cl_io_loop(env, io);
5421 cl_io_fini(env, io);
5422 } while (unlikely(io->ci_need_restart));
5425 cl_env_put(env, &refcheck);
5429 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5431 struct inode *inode = file_inode(filp);
5434 if (offset < 0 || len <= 0)
5437 * Encrypted inodes can't handle collapse range or zero range or insert
5438 * range since we would need to re-encrypt blocks with a different IV or
5439 * XTS tweak (which are based on the logical block number).
5440 * Similar to what ext4 does.
5442 if (IS_ENCRYPTED(inode) &&
5443 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5444 FALLOC_FL_ZERO_RANGE)))
5445 RETURN(-EOPNOTSUPP);
5448 * mode == 0 (which is standard prealloc) and PUNCH is supported
5449 * Rest of mode options are not supported yet.
5451 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5452 RETURN(-EOPNOTSUPP);
5454 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5456 rc = cl_falloc(filp, inode, mode, offset, len);
5458 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5459 * used by Lustre in several places. Retuning it here would
5460 * confuse applications that explicity test for EOPNOTSUPP
5461 * (95) and fall back to ftruncate().
5463 if (rc == -ENOTSUPP)
5469 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5470 __u64 start, __u64 len)
5474 struct fiemap *fiemap;
5475 unsigned int extent_count = fieinfo->fi_extents_max;
5477 num_bytes = sizeof(*fiemap) + (extent_count *
5478 sizeof(struct fiemap_extent));
5479 OBD_ALLOC_LARGE(fiemap, num_bytes);
5484 fiemap->fm_flags = fieinfo->fi_flags;
5485 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5486 fiemap->fm_start = start;
5487 fiemap->fm_length = len;
5488 if (extent_count > 0 &&
5489 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5490 sizeof(struct fiemap_extent)) != 0)
5491 GOTO(out, rc = -EFAULT);
5493 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5495 if (IS_ENCRYPTED(inode)) {
5498 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5499 fiemap->fm_extents[i].fe_flags |=
5500 FIEMAP_EXTENT_DATA_ENCRYPTED |
5501 FIEMAP_EXTENT_ENCODED;
5504 fieinfo->fi_flags = fiemap->fm_flags;
5505 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5506 if (extent_count > 0 &&
5507 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5508 fiemap->fm_mapped_extents *
5509 sizeof(struct fiemap_extent)) != 0)
5510 GOTO(out, rc = -EFAULT);
5512 OBD_FREE_LARGE(fiemap, num_bytes);
5516 int ll_inode_permission(struct inode *inode, int mask)
5519 struct ll_sb_info *sbi;
5520 struct root_squash_info *squash;
5521 struct cred *cred = NULL;
5522 const struct cred *old_cred = NULL;
5523 bool squash_id = false;
5524 ktime_t kstart = ktime_get();
5528 if (mask & MAY_NOT_BLOCK)
5532 * as root inode are NOT getting validated in lookup operation,
5533 * need to do it before permission check.
5536 if (is_root_inode(inode)) {
5537 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5542 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5543 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5545 /* squash fsuid/fsgid if needed */
5546 sbi = ll_i2sbi(inode);
5547 squash = &sbi->ll_squash;
5548 if (unlikely(squash->rsi_uid != 0 &&
5549 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5550 !test_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags))) {
5554 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5555 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5556 squash->rsi_uid, squash->rsi_gid);
5558 /* update current process's credentials
5559 * and FS capability */
5560 cred = prepare_creds();
5564 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5565 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5566 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5567 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5569 old_cred = override_creds(cred);
5572 rc = generic_permission(inode, mask);
5573 /* restore current process's credentials and FS capability */
5575 revert_creds(old_cred);
5580 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5581 ktime_us_delta(ktime_get(), kstart));
5586 /* -o localflock - only provides locally consistent flock locks */
5587 static const struct file_operations ll_file_operations = {
5588 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5589 # ifdef HAVE_SYNC_READ_WRITE
5590 .read = new_sync_read,
5591 .write = new_sync_write,
5593 .read_iter = ll_file_read_iter,
5594 .write_iter = ll_file_write_iter,
5595 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5596 .read = ll_file_read,
5597 .aio_read = ll_file_aio_read,
5598 .write = ll_file_write,
5599 .aio_write = ll_file_aio_write,
5600 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5601 .unlocked_ioctl = ll_file_ioctl,
5602 .open = ll_file_open,
5603 .release = ll_file_release,
5604 .mmap = ll_file_mmap,
5605 .llseek = ll_file_seek,
5606 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5607 .splice_read = generic_file_splice_read,
5609 .splice_read = pcc_file_splice_read,
5613 .fallocate = ll_fallocate,
5616 static const struct file_operations ll_file_operations_flock = {
5617 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5618 # ifdef HAVE_SYNC_READ_WRITE
5619 .read = new_sync_read,
5620 .write = new_sync_write,
5621 # endif /* HAVE_SYNC_READ_WRITE */
5622 .read_iter = ll_file_read_iter,
5623 .write_iter = ll_file_write_iter,
5624 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5625 .read = ll_file_read,
5626 .aio_read = ll_file_aio_read,
5627 .write = ll_file_write,
5628 .aio_write = ll_file_aio_write,
5629 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5630 .unlocked_ioctl = ll_file_ioctl,
5631 .open = ll_file_open,
5632 .release = ll_file_release,
5633 .mmap = ll_file_mmap,
5634 .llseek = ll_file_seek,
5635 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5636 .splice_read = generic_file_splice_read,
5638 .splice_read = pcc_file_splice_read,
5642 .flock = ll_file_flock,
5643 .lock = ll_file_flock,
5644 .fallocate = ll_fallocate,
5647 /* These are for -o noflock - to return ENOSYS on flock calls */
5648 static const struct file_operations ll_file_operations_noflock = {
5649 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5650 # ifdef HAVE_SYNC_READ_WRITE
5651 .read = new_sync_read,
5652 .write = new_sync_write,
5653 # endif /* HAVE_SYNC_READ_WRITE */
5654 .read_iter = ll_file_read_iter,
5655 .write_iter = ll_file_write_iter,
5656 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5657 .read = ll_file_read,
5658 .aio_read = ll_file_aio_read,
5659 .write = ll_file_write,
5660 .aio_write = ll_file_aio_write,
5661 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5662 .unlocked_ioctl = ll_file_ioctl,
5663 .open = ll_file_open,
5664 .release = ll_file_release,
5665 .mmap = ll_file_mmap,
5666 .llseek = ll_file_seek,
5667 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5668 .splice_read = generic_file_splice_read,
5670 .splice_read = pcc_file_splice_read,
5674 .flock = ll_file_noflock,
5675 .lock = ll_file_noflock,
5676 .fallocate = ll_fallocate,
5679 const struct inode_operations ll_file_inode_operations = {
5680 .setattr = ll_setattr,
5681 .getattr = ll_getattr,
5682 .permission = ll_inode_permission,
5683 #ifdef HAVE_IOP_XATTR
5684 .setxattr = ll_setxattr,
5685 .getxattr = ll_getxattr,
5686 .removexattr = ll_removexattr,
5688 .listxattr = ll_listxattr,
5689 .fiemap = ll_fiemap,
5690 .get_acl = ll_get_acl,
5691 #ifdef HAVE_IOP_SET_ACL
5692 .set_acl = ll_set_acl,
5696 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5698 const struct file_operations *fops = &ll_file_operations_noflock;
5700 if (test_bit(LL_SBI_FLOCK, sbi->ll_flags))
5701 fops = &ll_file_operations_flock;
5702 else if (test_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags))
5703 fops = &ll_file_operations;
5708 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5710 struct ll_inode_info *lli = ll_i2info(inode);
5711 struct cl_object *obj = lli->lli_clob;
5720 env = cl_env_get(&refcheck);
5722 RETURN(PTR_ERR(env));
5724 rc = cl_conf_set(env, lli->lli_clob, conf);
5728 if (conf->coc_opc == OBJECT_CONF_SET) {
5729 struct ldlm_lock *lock = conf->coc_lock;
5730 struct cl_layout cl = {
5734 LASSERT(lock != NULL);
5735 LASSERT(ldlm_has_layout(lock));
5737 /* it can only be allowed to match after layout is
5738 * applied to inode otherwise false layout would be
5739 * seen. Applying layout shoud happen before dropping
5740 * the intent lock. */
5741 ldlm_lock_allow_match(lock);
5743 rc = cl_object_layout_get(env, obj, &cl);
5748 DFID": layout version change: %u -> %u\n",
5749 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5751 ll_layout_version_set(lli, cl.cl_layout_gen);
5755 cl_env_put(env, &refcheck);
5757 RETURN(rc < 0 ? rc : 0);
5760 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5761 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5764 struct ll_sb_info *sbi = ll_i2sbi(inode);
5765 struct ptlrpc_request *req;
5772 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5773 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5774 lock->l_lvb_data, lock->l_lvb_len);
5776 if (lock->l_lvb_data != NULL)
5779 /* if layout lock was granted right away, the layout is returned
5780 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5781 * blocked and then granted via completion ast, we have to fetch
5782 * layout here. Please note that we can't use the LVB buffer in
5783 * completion AST because it doesn't have a large enough buffer */
5784 rc = ll_get_default_mdsize(sbi, &lmmsize);
5788 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5789 XATTR_NAME_LOV, lmmsize, &req);
5792 GOTO(out, rc = 0); /* empty layout */
5799 if (lmmsize == 0) /* empty layout */
5802 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5804 GOTO(out, rc = -EFAULT);
5806 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5807 if (lvbdata == NULL)
5808 GOTO(out, rc = -ENOMEM);
5810 memcpy(lvbdata, lmm, lmmsize);
5811 lock_res_and_lock(lock);
5812 if (unlikely(lock->l_lvb_data == NULL)) {
5813 lock->l_lvb_type = LVB_T_LAYOUT;
5814 lock->l_lvb_data = lvbdata;
5815 lock->l_lvb_len = lmmsize;
5818 unlock_res_and_lock(lock);
5821 OBD_FREE_LARGE(lvbdata, lmmsize);
5826 ptlrpc_req_finished(req);
5831 * Apply the layout to the inode. Layout lock is held and will be released
5834 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5835 struct inode *inode)
5837 struct ll_inode_info *lli = ll_i2info(inode);
5838 struct ll_sb_info *sbi = ll_i2sbi(inode);
5839 struct ldlm_lock *lock;
5840 struct cl_object_conf conf;
5843 bool wait_layout = false;
5846 LASSERT(lustre_handle_is_used(lockh));
5848 lock = ldlm_handle2lock(lockh);
5849 LASSERT(lock != NULL);
5851 if (!ldlm_has_layout(lock))
5852 GOTO(out, rc = -EAGAIN);
5854 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5855 PFID(&lli->lli_fid), inode);
5857 /* in case this is a caching lock and reinstate with new inode */
5858 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5860 lock_res_and_lock(lock);
5861 lvb_ready = ldlm_is_lvb_ready(lock);
5862 unlock_res_and_lock(lock);
5864 /* checking lvb_ready is racy but this is okay. The worst case is
5865 * that multi processes may configure the file on the same time. */
5869 rc = ll_layout_fetch(inode, lock);
5873 /* for layout lock, lmm is stored in lock's lvb.
5874 * lvb_data is immutable if the lock is held so it's safe to access it
5877 * set layout to file. Unlikely this will fail as old layout was
5878 * surely eliminated */
5879 memset(&conf, 0, sizeof conf);
5880 conf.coc_opc = OBJECT_CONF_SET;
5881 conf.coc_inode = inode;
5882 conf.coc_lock = lock;
5883 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5884 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5885 rc = ll_layout_conf(inode, &conf);
5887 /* refresh layout failed, need to wait */
5888 wait_layout = rc == -EBUSY;
5891 LDLM_LOCK_PUT(lock);
5892 ldlm_lock_decref(lockh, mode);
5894 /* wait for IO to complete if it's still being used. */
5896 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5897 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5899 memset(&conf, 0, sizeof conf);
5900 conf.coc_opc = OBJECT_CONF_WAIT;
5901 conf.coc_inode = inode;
5902 rc = ll_layout_conf(inode, &conf);
5906 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5907 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5913 * Issue layout intent RPC to MDS.
5914 * \param inode [in] file inode
5915 * \param intent [in] layout intent
5917 * \retval 0 on success
5918 * \retval < 0 error code
5920 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5922 struct ll_inode_info *lli = ll_i2info(inode);
5923 struct ll_sb_info *sbi = ll_i2sbi(inode);
5924 struct md_op_data *op_data;
5925 struct lookup_intent it;
5926 struct ptlrpc_request *req;
5930 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5931 0, 0, LUSTRE_OPC_ANY, NULL);
5932 if (IS_ERR(op_data))
5933 RETURN(PTR_ERR(op_data));
5935 op_data->op_data = intent;
5936 op_data->op_data_size = sizeof(*intent);
5938 memset(&it, 0, sizeof(it));
5939 it.it_op = IT_LAYOUT;
5940 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5941 intent->li_opc == LAYOUT_INTENT_TRUNC)
5942 it.it_flags = FMODE_WRITE;
5944 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5945 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5947 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5948 &ll_md_blocking_ast, 0);
5949 if (it.it_request != NULL)
5950 ptlrpc_req_finished(it.it_request);
5951 it.it_request = NULL;
5953 ll_finish_md_op_data(op_data);
5955 /* set lock data in case this is a new lock */
5957 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5959 ll_intent_drop_lock(&it);
5965 * This function checks if there exists a LAYOUT lock on the client side,
5966 * or enqueues it if it doesn't have one in cache.
5968 * This function will not hold layout lock so it may be revoked any time after
5969 * this function returns. Any operations depend on layout should be redone
5972 * This function should be called before lov_io_init() to get an uptodate
5973 * layout version, the caller should save the version number and after IO
5974 * is finished, this function should be called again to verify that layout
5975 * is not changed during IO time.
5977 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5979 struct ll_inode_info *lli = ll_i2info(inode);
5980 struct ll_sb_info *sbi = ll_i2sbi(inode);
5981 struct lustre_handle lockh;
5982 struct layout_intent intent = {
5983 .li_opc = LAYOUT_INTENT_ACCESS,
5985 enum ldlm_mode mode;
5989 *gen = ll_layout_version_get(lli);
5990 if (!test_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags) ||
5991 *gen != CL_LAYOUT_GEN_NONE)
5995 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5996 LASSERT(S_ISREG(inode->i_mode));
5998 /* take layout lock mutex to enqueue layout lock exclusively. */
5999 mutex_lock(&lli->lli_layout_mutex);
6002 /* mostly layout lock is caching on the local side, so try to
6003 * match it before grabbing layout lock mutex. */
6004 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
6005 LCK_CR | LCK_CW | LCK_PR |
6007 if (mode != 0) { /* hit cached lock */
6008 rc = ll_layout_lock_set(&lockh, mode, inode);
6014 rc = ll_layout_intent(inode, &intent);
6020 *gen = ll_layout_version_get(lli);
6021 mutex_unlock(&lli->lli_layout_mutex);
6027 * Issue layout intent RPC indicating where in a file an IO is about to write.
6029 * \param[in] inode file inode.
6030 * \param[in] ext write range with start offset of fille in bytes where
6031 * an IO is about to write, and exclusive end offset in
6034 * \retval 0 on success
6035 * \retval < 0 error code
6037 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
6038 struct lu_extent *ext)
6040 struct layout_intent intent = {
6042 .li_extent.e_start = ext->e_start,
6043 .li_extent.e_end = ext->e_end,
6048 rc = ll_layout_intent(inode, &intent);
6054 * This function send a restore request to the MDT
6056 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
6058 struct hsm_user_request *hur;
6062 len = sizeof(struct hsm_user_request) +
6063 sizeof(struct hsm_user_item);
6064 OBD_ALLOC(hur, len);
6068 hur->hur_request.hr_action = HUA_RESTORE;
6069 hur->hur_request.hr_archive_id = 0;
6070 hur->hur_request.hr_flags = 0;
6071 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
6072 sizeof(hur->hur_user_item[0].hui_fid));
6073 hur->hur_user_item[0].hui_extent.offset = offset;
6074 hur->hur_user_item[0].hui_extent.length = length;
6075 hur->hur_request.hr_itemcount = 1;
6076 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,