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 */