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>
47 #ifdef HAVE_LINUX_FILELOCK_HEADER
48 #include <linux/filelock.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_swab.h>
54 #include "cl_object.h"
55 #include "llite_internal.h"
56 #include "vvp_internal.h"
59 struct inode *sp_inode;
64 __u64 pa_data_version;
70 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
72 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
75 static struct ll_file_data *ll_file_data_get(void)
77 struct ll_file_data *fd;
79 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
83 fd->fd_write_failed = false;
84 pcc_file_init(&fd->fd_pcc_file);
89 static void ll_file_data_put(struct ll_file_data *fd)
92 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
96 * Packs all the attributes into @op_data for the CLOSE rpc.
98 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
99 struct obd_client_handle *och)
103 ll_prep_md_op_data(op_data, inode, NULL, NULL,
104 0, 0, LUSTRE_OPC_ANY, NULL);
106 op_data->op_attr.ia_mode = inode->i_mode;
107 op_data->op_attr.ia_atime = inode->i_atime;
108 op_data->op_attr.ia_mtime = inode->i_mtime;
109 op_data->op_attr.ia_ctime = inode->i_ctime;
110 /* In case of encrypted file without the key, visible size was rounded
111 * up to next LUSTRE_ENCRYPTION_UNIT_SIZE, and clear text size was
112 * stored into lli_lazysize in ll_merge_attr(), so set proper file size
113 * now that we are closing.
115 if (llcrypt_require_key(inode) == -ENOKEY &&
116 ll_i2info(inode)->lli_attr_valid & OBD_MD_FLLAZYSIZE)
117 op_data->op_attr.ia_size = ll_i2info(inode)->lli_lazysize;
119 op_data->op_attr.ia_size = i_size_read(inode);
120 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
121 ATTR_MTIME | ATTR_MTIME_SET |
123 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
124 op_data->op_attr_blocks = inode->i_blocks;
125 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
126 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
127 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
128 op_data->op_open_handle = och->och_open_handle;
130 if (och->och_flags & FMODE_WRITE &&
131 test_and_clear_bit(LLIF_DATA_MODIFIED,
132 &ll_i2info(inode)->lli_flags))
133 /* For HSM: if inode data has been modified, pack it so that
134 * MDT can set data dirty flag in the archive. */
135 op_data->op_bias |= MDS_DATA_MODIFIED;
141 * Perform a close, possibly with a bias.
142 * The meaning of "data" depends on the value of "bias".
144 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
145 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
148 static int ll_close_inode_openhandle(struct inode *inode,
149 struct obd_client_handle *och,
150 enum mds_op_bias bias, void *data)
152 struct obd_export *md_exp = ll_i2mdexp(inode);
153 const struct ll_inode_info *lli = ll_i2info(inode);
154 struct md_op_data *op_data;
155 struct ptlrpc_request *req = NULL;
159 if (class_exp2obd(md_exp) == NULL) {
160 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
161 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
165 OBD_ALLOC_PTR(op_data);
166 /* We leak openhandle and request here on error, but not much to be
167 * done in OOM case since app won't retry close on error either. */
169 GOTO(out, rc = -ENOMEM);
171 ll_prepare_close(inode, op_data, och);
173 case MDS_CLOSE_LAYOUT_MERGE:
174 /* merge blocks from the victim inode */
175 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
176 op_data->op_attr.ia_valid |= ATTR_SIZE;
177 op_data->op_xvalid |= OP_XVALID_BLOCKS;
179 case MDS_CLOSE_LAYOUT_SPLIT:
180 case MDS_CLOSE_LAYOUT_SWAP: {
181 struct split_param *sp = data;
183 LASSERT(data != NULL);
184 op_data->op_bias |= bias;
185 op_data->op_data_version = 0;
186 op_data->op_lease_handle = och->och_lease_handle;
187 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
188 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
189 op_data->op_mirror_id = sp->sp_mirror_id;
191 op_data->op_fid2 = *ll_inode2fid(data);
196 case MDS_CLOSE_RESYNC_DONE: {
197 struct ll_ioc_lease *ioc = data;
199 LASSERT(data != NULL);
200 op_data->op_attr_blocks +=
201 ioc->lil_count * op_data->op_attr_blocks;
202 op_data->op_attr.ia_valid |= ATTR_SIZE;
203 op_data->op_xvalid |= OP_XVALID_BLOCKS;
204 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
206 op_data->op_lease_handle = och->och_lease_handle;
207 op_data->op_data = &ioc->lil_ids[0];
208 op_data->op_data_size =
209 ioc->lil_count * sizeof(ioc->lil_ids[0]);
213 case MDS_PCC_ATTACH: {
214 struct pcc_param *param = data;
216 LASSERT(data != NULL);
217 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
218 op_data->op_archive_id = param->pa_archive_id;
219 op_data->op_data_version = param->pa_data_version;
220 op_data->op_lease_handle = och->och_lease_handle;
224 case MDS_HSM_RELEASE:
225 LASSERT(data != NULL);
226 op_data->op_bias |= MDS_HSM_RELEASE;
227 op_data->op_data_version = *(__u64 *)data;
228 op_data->op_lease_handle = och->och_lease_handle;
229 op_data->op_attr.ia_valid |= ATTR_SIZE;
230 op_data->op_xvalid |= OP_XVALID_BLOCKS;
234 LASSERT(data == NULL);
238 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
239 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
240 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
241 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
243 rc = md_close(md_exp, op_data, och->och_mod, &req);
244 if (rc != 0 && rc != -EINTR)
245 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
246 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
248 if (rc == 0 && op_data->op_bias & bias) {
249 struct mdt_body *body;
251 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
252 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
255 if (bias & MDS_PCC_ATTACH) {
256 struct pcc_param *param = data;
258 param->pa_layout_gen = body->mbo_layout_gen;
262 ll_finish_md_op_data(op_data);
266 md_clear_open_replay_data(md_exp, och);
267 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
270 ptlrpc_req_finished(req); /* This is close request */
274 int ll_md_real_close(struct inode *inode, fmode_t fmode)
276 struct ll_inode_info *lli = ll_i2info(inode);
277 struct obd_client_handle **och_p;
278 struct obd_client_handle *och;
283 if (fmode & FMODE_WRITE) {
284 och_p = &lli->lli_mds_write_och;
285 och_usecount = &lli->lli_open_fd_write_count;
286 } else if (fmode & FMODE_EXEC) {
287 och_p = &lli->lli_mds_exec_och;
288 och_usecount = &lli->lli_open_fd_exec_count;
290 LASSERT(fmode & FMODE_READ);
291 och_p = &lli->lli_mds_read_och;
292 och_usecount = &lli->lli_open_fd_read_count;
295 mutex_lock(&lli->lli_och_mutex);
296 if (*och_usecount > 0) {
297 /* There are still users of this handle, so skip
299 mutex_unlock(&lli->lli_och_mutex);
305 mutex_unlock(&lli->lli_och_mutex);
308 /* There might be a race and this handle may already
310 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
316 static int ll_md_close(struct inode *inode, struct file *file)
318 union ldlm_policy_data policy = {
319 .l_inodebits = { MDS_INODELOCK_OPEN },
321 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
322 struct ll_file_data *fd = file->private_data;
323 struct ll_inode_info *lli = ll_i2info(inode);
324 struct lustre_handle lockh;
325 enum ldlm_mode lockmode;
329 /* clear group lock, if present */
330 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
331 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
333 mutex_lock(&lli->lli_och_mutex);
334 if (fd->fd_lease_och != NULL) {
336 struct obd_client_handle *lease_och;
338 lease_och = fd->fd_lease_och;
339 fd->fd_lease_och = NULL;
340 mutex_unlock(&lli->lli_och_mutex);
342 /* Usually the lease is not released when the
343 * application crashed, we need to release here. */
344 rc = ll_lease_close(lease_och, inode, &lease_broken);
346 mutex_lock(&lli->lli_och_mutex);
348 CDEBUG_LIMIT(rc ? D_ERROR : D_INODE,
349 "Clean up lease "DFID" %d/%d\n",
350 PFID(&lli->lli_fid), rc, lease_broken);
353 if (fd->fd_och != NULL) {
354 struct obd_client_handle *och;
358 mutex_unlock(&lli->lli_och_mutex);
360 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
364 /* Let's see if we have good enough OPEN lock on the file and if
365 we can skip talking to MDS */
366 if (fd->fd_omode & FMODE_WRITE) {
368 LASSERT(lli->lli_open_fd_write_count);
369 lli->lli_open_fd_write_count--;
370 } else if (fd->fd_omode & FMODE_EXEC) {
372 LASSERT(lli->lli_open_fd_exec_count);
373 lli->lli_open_fd_exec_count--;
376 LASSERT(lli->lli_open_fd_read_count);
377 lli->lli_open_fd_read_count--;
379 mutex_unlock(&lli->lli_och_mutex);
381 /* LU-4398: do not cache write open lock if the file has exec bit */
382 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
383 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
384 LDLM_IBITS, &policy, lockmode, &lockh))
385 rc = ll_md_real_close(inode, fd->fd_omode);
388 file->private_data = NULL;
389 ll_file_data_put(fd);
394 /* While this returns an error code, fput() the caller does not, so we need
395 * to make every effort to clean up all of our state here. Also, applications
396 * rarely check close errors and even if an error is returned they will not
397 * re-try the close call.
399 int ll_file_release(struct inode *inode, struct file *file)
401 struct ll_file_data *fd;
402 struct ll_sb_info *sbi = ll_i2sbi(inode);
403 struct ll_inode_info *lli = ll_i2info(inode);
404 ktime_t kstart = ktime_get();
409 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
410 PFID(ll_inode2fid(inode)), inode);
412 fd = file->private_data;
415 /* The last ref on @file, maybe not the the owner pid of statahead,
416 * because parent and child process can share the same file handle. */
417 if (S_ISDIR(inode->i_mode) &&
418 (lli->lli_opendir_key == fd || fd->fd_sai))
419 ll_deauthorize_statahead(inode, fd);
421 if (is_root_inode(inode)) {
422 file->private_data = NULL;
423 ll_file_data_put(fd);
427 pcc_file_release(inode, file);
429 if (!S_ISDIR(inode->i_mode)) {
430 if (lli->lli_clob != NULL)
431 lov_read_and_clear_async_rc(lli->lli_clob);
432 lli->lli_async_rc = 0;
435 lli->lli_close_fd_time = ktime_get();
437 rc = ll_md_close(inode, file);
439 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
440 libcfs_debug_dumplog();
443 if (!rc && !is_root_inode(inode))
444 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
445 ktime_us_delta(ktime_get(), kstart));
449 static inline int ll_dom_readpage(void *data, struct page *page)
451 /* since ll_dom_readpage is a page cache helper, it is safe to assume
452 * mapping and host pointers are set here
455 struct niobuf_local *lnb = data;
459 inode = page2inode(page);
461 kaddr = kmap_atomic(page);
462 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
463 if (lnb->lnb_len < PAGE_SIZE)
464 memset(kaddr + lnb->lnb_len, 0,
465 PAGE_SIZE - lnb->lnb_len);
466 kunmap_atomic(kaddr);
468 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
469 if (!llcrypt_has_encryption_key(inode)) {
470 CDEBUG(D_SEC, "no enc key for "DFID"\n",
471 PFID(ll_inode2fid(inode)));
474 unsigned int offs = 0;
476 while (offs < PAGE_SIZE) {
477 /* decrypt only if page is not empty */
478 if (memcmp(page_address(page) + offs,
479 page_address(ZERO_PAGE(0)),
480 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
483 rc = llcrypt_decrypt_pagecache_blocks(page,
484 LUSTRE_ENCRYPTION_UNIT_SIZE,
489 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
494 flush_dcache_page(page);
495 SetPageUptodate(page);
502 #ifdef HAVE_READ_CACHE_PAGE_WANTS_FILE
503 static inline int ll_dom_read_folio(struct file *file, struct folio *folio0)
505 return ll_dom_readpage(file->private_data, folio_page(folio0, 0));
508 #define ll_dom_read_folio ll_dom_readpage
511 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
515 struct ll_inode_info *lli = ll_i2info(inode);
516 struct cl_object *obj = lli->lli_clob;
517 struct address_space *mapping = inode->i_mapping;
519 struct niobuf_remote *rnb;
520 struct mdt_body *body;
522 unsigned long index, start;
523 struct niobuf_local lnb;
532 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
536 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
537 if (rnb == NULL || rnb->rnb_len == 0)
540 /* LU-11595: Server may return whole file and that is OK always or
541 * it may return just file tail and its offset must be aligned with
542 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
543 * smaller then offset may be not aligned and that data is just ignored.
545 if (rnb->rnb_offset & ~PAGE_MASK)
548 /* Server returns whole file or just file tail if it fills in reply
549 * buffer, in both cases total size should be equal to the file size.
551 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
552 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
553 !(inode && IS_ENCRYPTED(inode))) {
554 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
555 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
556 rnb->rnb_len, body->mbo_dom_size);
560 env = cl_env_get(&refcheck);
563 io = vvp_env_thread_io(env);
565 io->ci_ignore_layout = 1;
566 rc = cl_io_init(env, io, CIT_MISC, obj);
570 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
571 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
573 data = (char *)rnb + sizeof(*rnb);
575 lnb.lnb_file_offset = rnb->rnb_offset;
576 start = lnb.lnb_file_offset >> PAGE_SHIFT;
578 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
579 lnb.lnb_page_offset = 0;
581 struct cl_page *page;
583 lnb.lnb_data = data + (index << PAGE_SHIFT);
584 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
585 if (lnb.lnb_len > PAGE_SIZE)
586 lnb.lnb_len = PAGE_SIZE;
588 vmpage = ll_read_cache_page(mapping, index + start,
589 ll_dom_read_folio, &lnb);
590 if (IS_ERR(vmpage)) {
591 CWARN("%s: cannot fill page %lu for "DFID
592 " with data: rc = %li\n",
593 ll_i2sbi(inode)->ll_fsname, index + start,
594 PFID(lu_object_fid(&obj->co_lu)),
599 if (vmpage->mapping == NULL) {
602 /* page was truncated */
605 /* attach VM page to CL page cache */
606 page = cl_page_find(env, obj, vmpage->index, vmpage,
609 ClearPageUptodate(vmpage);
614 SetPageUptodate(vmpage);
615 cl_page_put(env, page);
619 } while (rnb->rnb_len > (index << PAGE_SHIFT));
623 cl_env_put(env, &refcheck);
628 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
629 struct lookup_intent *itp)
631 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
632 struct dentry *parent = de->d_parent;
635 struct md_op_data *op_data;
636 struct ptlrpc_request *req = NULL;
640 LASSERT(parent != NULL);
641 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
643 /* if server supports open-by-fid, or file name is invalid, don't pack
644 * name in open request */
645 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
646 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
648 len = de->d_name.len;
649 name = kmalloc(len + 1, GFP_NOFS);
654 spin_lock(&de->d_lock);
655 if (len != de->d_name.len) {
656 spin_unlock(&de->d_lock);
660 memcpy(name, de->d_name.name, len);
662 spin_unlock(&de->d_lock);
664 if (!lu_name_is_valid_2(name, len)) {
670 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
671 name, len, 0, LUSTRE_OPC_OPEN, NULL);
672 if (IS_ERR(op_data)) {
674 RETURN(PTR_ERR(op_data));
676 op_data->op_data = lmm;
677 op_data->op_data_size = lmmsize;
679 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_OPEN_DELAY, cfs_fail_val);
681 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
682 &ll_md_blocking_ast, 0);
684 ll_finish_md_op_data(op_data);
686 /* reason for keep own exit path - don`t flood log
687 * with messages with -ESTALE errors.
689 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
690 it_open_error(DISP_OPEN_OPEN, itp))
692 ll_release_openhandle(de, itp);
696 if (it_disposition(itp, DISP_LOOKUP_NEG))
697 GOTO(out, rc = -ENOENT);
699 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
700 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
701 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
705 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
707 if (!rc && itp->it_lock_mode) {
710 /* If we got a lock back and it has a LOOKUP bit set,
711 * make sure the dentry is marked as valid so we can find it.
712 * We don't need to care about actual hashing since other bits
713 * of kernel will deal with that later.
715 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
716 if (bits & MDS_INODELOCK_LOOKUP)
717 d_lustre_revalidate(de);
719 /* if DoM bit returned along with LAYOUT bit then there
720 * can be read-on-open data returned.
722 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
723 ll_dom_finish_open(de->d_inode, req);
725 /* open may not fetch LOOKUP lock, update dir depth and default LMV
728 if (!rc && S_ISDIR(de->d_inode->i_mode))
729 ll_update_dir_depth_dmv(parent->d_inode, de);
732 ptlrpc_req_finished(req);
733 ll_intent_drop_lock(itp);
735 /* We did open by fid, but by the time we got to the server, the object
736 * disappeared. This is possible if the object was unlinked, but it's
737 * also possible if the object was unlinked by a rename. In the case
738 * of an object renamed over our existing one, we can't fail this open.
739 * O_CREAT also goes through this path if we had an existing dentry,
740 * and it's obviously wrong to return ENOENT for O_CREAT.
742 * Instead let's return -ESTALE, and the VFS will retry the open with
743 * LOOKUP_REVAL, which we catch in ll_revalidate_dentry and fail to
744 * revalidate, causing a lookup. This causes extra lookups in the case
745 * where we had a dentry in cache but the file is being unlinked and we
746 * lose the race with unlink, but this should be very rare.
754 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
755 struct obd_client_handle *och)
757 struct mdt_body *body;
759 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
760 och->och_open_handle = body->mbo_open_handle;
761 och->och_fid = body->mbo_fid1;
762 och->och_lease_handle.cookie = it->it_lock_handle;
763 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
764 och->och_flags = it->it_flags;
766 return md_set_open_replay_data(md_exp, och, it);
769 static int ll_local_open(struct file *file, struct lookup_intent *it,
770 struct ll_file_data *fd, struct obd_client_handle *och)
772 struct inode *inode = file_inode(file);
775 LASSERT(!file->private_data);
782 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
787 file->private_data = fd;
788 ll_readahead_init(inode, &fd->fd_ras);
789 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
794 void ll_track_file_opens(struct inode *inode)
796 struct ll_inode_info *lli = ll_i2info(inode);
797 struct ll_sb_info *sbi = ll_i2sbi(inode);
799 /* do not skew results with delays from never-opened inodes */
800 if (ktime_to_ns(lli->lli_close_fd_time))
801 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
802 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
804 if (ktime_after(ktime_get(),
805 ktime_add_ms(lli->lli_close_fd_time,
806 sbi->ll_oc_max_ms))) {
807 lli->lli_open_fd_count = 1;
808 lli->lli_close_fd_time = ns_to_ktime(0);
810 lli->lli_open_fd_count++;
813 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
814 lli->lli_open_fd_count);
817 /* Open a file, and (for the very first open) create objects on the OSTs at
818 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
819 * creation or open until ll_lov_setstripe() ioctl is called.
821 * If we already have the stripe MD locally then we don't request it in
822 * md_open(), by passing a lmm_size = 0.
824 * It is up to the application to ensure no other processes open this file
825 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
826 * used. We might be able to avoid races of that sort by getting lli_open_sem
827 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
828 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
830 int ll_file_open(struct inode *inode, struct file *file)
832 struct ll_inode_info *lli = ll_i2info(inode);
833 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
834 .it_flags = file->f_flags };
835 struct obd_client_handle **och_p = NULL;
836 __u64 *och_usecount = NULL;
837 struct ll_file_data *fd;
838 ktime_t kstart = ktime_get();
842 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
843 PFID(ll_inode2fid(inode)), inode, file->f_flags);
845 it = file->private_data; /* XXX: compat macro */
846 file->private_data = NULL; /* prevent ll_local_open assertion */
848 if (S_ISREG(inode->i_mode)) {
849 rc = ll_file_open_encrypt(inode, file);
851 if (it && it->it_disposition)
852 ll_release_openhandle(file_dentry(file), it);
853 GOTO(out_nofiledata, rc);
857 fd = ll_file_data_get();
859 GOTO(out_nofiledata, rc = -ENOMEM);
862 if (S_ISDIR(inode->i_mode))
863 ll_authorize_statahead(inode, fd);
865 ll_track_file_opens(inode);
866 if (is_root_inode(inode)) {
867 file->private_data = fd;
871 if (!it || !it->it_disposition) {
872 /* Convert f_flags into access mode. We cannot use file->f_mode,
873 * because everything but O_ACCMODE mask was stripped from
875 if ((oit.it_flags + 1) & O_ACCMODE)
877 if (file->f_flags & O_TRUNC)
878 oit.it_flags |= FMODE_WRITE;
880 /* kernel only call f_op->open in dentry_open. filp_open calls
881 * dentry_open after call to open_namei that checks permissions.
882 * Only nfsd_open call dentry_open directly without checking
883 * permissions and because of that this code below is safe.
885 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
886 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
888 /* We do not want O_EXCL here, presumably we opened the file
889 * already? XXX - NFS implications? */
890 oit.it_flags &= ~O_EXCL;
892 /* bug20584, if "it_flags" contains O_CREAT, the file will be
893 * created if necessary, then "IT_CREAT" should be set to keep
894 * consistent with it */
895 if (oit.it_flags & O_CREAT)
896 oit.it_op |= IT_CREAT;
902 /* Let's see if we have file open on MDS already. */
903 if (it->it_flags & FMODE_WRITE) {
904 och_p = &lli->lli_mds_write_och;
905 och_usecount = &lli->lli_open_fd_write_count;
906 } else if (it->it_flags & FMODE_EXEC) {
907 och_p = &lli->lli_mds_exec_och;
908 och_usecount = &lli->lli_open_fd_exec_count;
910 och_p = &lli->lli_mds_read_och;
911 och_usecount = &lli->lli_open_fd_read_count;
914 mutex_lock(&lli->lli_och_mutex);
915 if (*och_p) { /* Open handle is present */
916 if (it_disposition(it, DISP_OPEN_OPEN)) {
917 /* Well, there's extra open request that we do not need,
918 * let's close it somehow. This will decref request. */
919 rc = it_open_error(DISP_OPEN_OPEN, it);
921 mutex_unlock(&lli->lli_och_mutex);
922 GOTO(out_openerr, rc);
925 ll_release_openhandle(file_dentry(file), it);
929 rc = ll_local_open(file, it, fd, NULL);
932 mutex_unlock(&lli->lli_och_mutex);
933 GOTO(out_openerr, rc);
936 LASSERT(*och_usecount == 0);
937 if (!it->it_disposition) {
938 struct dentry *dentry = file_dentry(file);
939 struct ll_sb_info *sbi = ll_i2sbi(inode);
940 int open_threshold = sbi->ll_oc_thrsh_count;
942 /* We cannot just request lock handle now, new ELC code
943 * means that one of other OPEN locks for this file
944 * could be cancelled, and since blocking ast handler
945 * would attempt to grab och_mutex as well, that would
946 * result in a deadlock
948 mutex_unlock(&lli->lli_och_mutex);
950 * Normally called under two situations:
951 * 1. fhandle / NFS export.
952 * 2. A race/condition on MDS resulting in no open
953 * handle to be returned from LOOKUP|OPEN request,
954 * for example if the target entry was a symlink.
956 * For NFSv3 we need to always cache the open lock
957 * for pre 5.5 Linux kernels.
959 * After reaching number of opens of this inode
960 * we always ask for an open lock on it to handle
961 * bad userspace actors that open and close files
962 * in a loop for absolutely no good reason
964 /* fhandle / NFS path. */
965 if (lli->lli_open_thrsh_count != UINT_MAX)
966 open_threshold = lli->lli_open_thrsh_count;
968 if (filename_is_volatile(dentry->d_name.name,
971 /* There really is nothing here, but this
972 * make this more readable I think.
973 * We do not want openlock for volatile
974 * files under any circumstances
976 } else if (open_threshold > 0) {
977 /* Take MDS_OPEN_LOCK with many opens */
978 if (lli->lli_open_fd_count >= open_threshold)
979 it->it_flags |= MDS_OPEN_LOCK;
981 /* If this is open after we just closed */
982 else if (ktime_before(ktime_get(),
983 ktime_add_ms(lli->lli_close_fd_time,
984 sbi->ll_oc_thrsh_ms)))
985 it->it_flags |= MDS_OPEN_LOCK;
989 * Always specify MDS_OPEN_BY_FID because we don't want
990 * to get file with different fid.
992 it->it_flags |= MDS_OPEN_BY_FID;
993 rc = ll_intent_file_open(dentry, NULL, 0, it);
995 GOTO(out_openerr, rc);
999 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
1001 GOTO(out_och_free, rc = -ENOMEM);
1005 /* md_intent_lock() didn't get a request ref if there was an
1006 * open error, so don't do cleanup on the request here
1008 /* XXX (green): Should not we bail out on any error here, not
1009 * just open error? */
1010 rc = it_open_error(DISP_OPEN_OPEN, it);
1012 GOTO(out_och_free, rc);
1014 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
1015 "inode %p: disposition %x, status %d\n", inode,
1016 it_disposition(it, ~0), it->it_status);
1018 rc = ll_local_open(file, it, fd, *och_p);
1020 GOTO(out_och_free, rc);
1023 rc = pcc_file_open(inode, file);
1025 GOTO(out_och_free, rc);
1027 mutex_unlock(&lli->lli_och_mutex);
1031 /* Must do this outside lli_och_mutex lock to prevent deadlock where
1032 different kind of OPEN lock for this same inode gets cancelled
1033 by ldlm_cancel_lru */
1034 if (!S_ISREG(inode->i_mode))
1035 GOTO(out_och_free, rc);
1036 cl_lov_delay_create_clear(&file->f_flags);
1037 GOTO(out_och_free, rc);
1041 if (och_p && *och_p) {
1042 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1043 *och_p = NULL; /* OBD_FREE writes some magic there */
1046 mutex_unlock(&lli->lli_och_mutex);
1049 if (lli->lli_opendir_key == fd)
1050 ll_deauthorize_statahead(inode, fd);
1053 ll_file_data_put(fd);
1055 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1056 ktime_us_delta(ktime_get(), kstart));
1060 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1061 ptlrpc_req_finished(it->it_request);
1062 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1068 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1069 struct ldlm_lock_desc *desc, void *data, int flag)
1072 struct lustre_handle lockh;
1076 case LDLM_CB_BLOCKING:
1077 ldlm_lock2handle(lock, &lockh);
1078 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1080 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1084 case LDLM_CB_CANCELING:
1092 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1093 * and save it as fd->fd_och so as to force client to reopen the file even
1094 * if it has an open lock in cache already.
1096 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1097 struct lustre_handle *old_open_handle)
1099 struct ll_inode_info *lli = ll_i2info(inode);
1100 struct ll_file_data *fd = file->private_data;
1101 struct obd_client_handle **och_p;
1102 __u64 *och_usecount;
1106 /* Get the openhandle of the file */
1107 mutex_lock(&lli->lli_och_mutex);
1108 if (fd->fd_lease_och != NULL)
1109 GOTO(out_unlock, rc = -EBUSY);
1111 if (fd->fd_och == NULL) {
1112 if (file->f_mode & FMODE_WRITE) {
1113 LASSERT(lli->lli_mds_write_och != NULL);
1114 och_p = &lli->lli_mds_write_och;
1115 och_usecount = &lli->lli_open_fd_write_count;
1117 LASSERT(lli->lli_mds_read_och != NULL);
1118 och_p = &lli->lli_mds_read_och;
1119 och_usecount = &lli->lli_open_fd_read_count;
1122 if (*och_usecount > 1)
1123 GOTO(out_unlock, rc = -EBUSY);
1125 fd->fd_och = *och_p;
1130 *old_open_handle = fd->fd_och->och_open_handle;
1134 mutex_unlock(&lli->lli_och_mutex);
1139 * Release ownership on lli_mds_*_och when putting back a file lease.
1141 static int ll_lease_och_release(struct inode *inode, struct file *file)
1143 struct ll_inode_info *lli = ll_i2info(inode);
1144 struct ll_file_data *fd = file->private_data;
1145 struct obd_client_handle **och_p;
1146 struct obd_client_handle *old_och = NULL;
1147 __u64 *och_usecount;
1151 mutex_lock(&lli->lli_och_mutex);
1152 if (file->f_mode & FMODE_WRITE) {
1153 och_p = &lli->lli_mds_write_och;
1154 och_usecount = &lli->lli_open_fd_write_count;
1156 och_p = &lli->lli_mds_read_och;
1157 och_usecount = &lli->lli_open_fd_read_count;
1160 /* The file may have been open by another process (broken lease) so
1161 * *och_p is not NULL. In this case we should simply increase usecount
1164 if (*och_p != NULL) {
1165 old_och = fd->fd_och;
1168 *och_p = fd->fd_och;
1172 mutex_unlock(&lli->lli_och_mutex);
1174 if (old_och != NULL)
1175 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1181 * Acquire a lease and open the file.
1183 static struct obd_client_handle *
1184 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1187 struct lookup_intent it = { .it_op = IT_OPEN };
1188 struct ll_sb_info *sbi = ll_i2sbi(inode);
1189 struct md_op_data *op_data;
1190 struct ptlrpc_request *req = NULL;
1191 struct lustre_handle old_open_handle = { 0 };
1192 struct obd_client_handle *och = NULL;
1197 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1198 RETURN(ERR_PTR(-EINVAL));
1201 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1202 RETURN(ERR_PTR(-EPERM));
1204 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1206 RETURN(ERR_PTR(rc));
1211 RETURN(ERR_PTR(-ENOMEM));
1213 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1214 LUSTRE_OPC_ANY, NULL);
1215 if (IS_ERR(op_data))
1216 GOTO(out, rc = PTR_ERR(op_data));
1218 /* To tell the MDT this openhandle is from the same owner */
1219 op_data->op_open_handle = old_open_handle;
1221 it.it_flags = fmode | open_flags;
1222 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1223 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1224 &ll_md_blocking_lease_ast,
1225 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1226 * it can be cancelled which may mislead applications that the lease is
1228 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1229 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1230 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1231 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1232 ll_finish_md_op_data(op_data);
1233 ptlrpc_req_finished(req);
1235 GOTO(out_release_it, rc);
1237 if (it_disposition(&it, DISP_LOOKUP_NEG))
1238 GOTO(out_release_it, rc = -ENOENT);
1240 rc = it_open_error(DISP_OPEN_OPEN, &it);
1242 GOTO(out_release_it, rc);
1244 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1245 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1247 GOTO(out_release_it, rc);
1249 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1250 GOTO(out_close, rc = -EOPNOTSUPP);
1252 /* already get lease, handle lease lock */
1253 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1254 if (!it.it_lock_mode ||
1255 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1256 /* open lock must return for lease */
1257 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1258 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1260 GOTO(out_close, rc = -EPROTO);
1263 ll_intent_release(&it);
1267 /* Cancel open lock */
1268 if (it.it_lock_mode != 0) {
1269 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1271 it.it_lock_mode = 0;
1272 och->och_lease_handle.cookie = 0ULL;
1274 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1276 CERROR("%s: error closing file "DFID": %d\n",
1277 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1278 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1280 ll_intent_release(&it);
1284 RETURN(ERR_PTR(rc));
1288 * Check whether a layout swap can be done between two inodes.
1290 * \param[in] inode1 First inode to check
1291 * \param[in] inode2 Second inode to check
1293 * \retval 0 on success, layout swap can be performed between both inodes
1294 * \retval negative error code if requirements are not met
1296 static int ll_check_swap_layouts_validity(struct inode *inode1,
1297 struct inode *inode2)
1299 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1302 if (inode_permission(&nop_mnt_idmap, inode1, MAY_WRITE) ||
1303 inode_permission(&nop_mnt_idmap, inode2, MAY_WRITE))
1306 if (inode1->i_sb != inode2->i_sb)
1312 static int ll_swap_layouts_close(struct obd_client_handle *och,
1313 struct inode *inode, struct inode *inode2)
1315 const struct lu_fid *fid1 = ll_inode2fid(inode);
1316 const struct lu_fid *fid2;
1320 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1321 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1323 rc = ll_check_swap_layouts_validity(inode, inode2);
1325 GOTO(out_free_och, rc);
1327 /* We now know that inode2 is a lustre inode */
1328 fid2 = ll_inode2fid(inode2);
1330 rc = lu_fid_cmp(fid1, fid2);
1332 GOTO(out_free_och, rc = -EINVAL);
1334 /* Close the file and {swap,merge} layouts between inode & inode2.
1335 * NB: local lease handle is released in mdc_close_intent_pack()
1336 * because we still need it to pack l_remote_handle to MDT. */
1337 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1340 och = NULL; /* freed in ll_close_inode_openhandle() */
1350 * Release lease and close the file.
1351 * It will check if the lease has ever broken.
1353 static int ll_lease_close_intent(struct obd_client_handle *och,
1354 struct inode *inode,
1355 bool *lease_broken, enum mds_op_bias bias,
1358 struct ldlm_lock *lock;
1359 bool cancelled = true;
1363 lock = ldlm_handle2lock(&och->och_lease_handle);
1365 lock_res_and_lock(lock);
1366 cancelled = ldlm_is_cancel(lock);
1367 unlock_res_and_lock(lock);
1368 LDLM_LOCK_PUT(lock);
1371 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1372 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1374 if (lease_broken != NULL)
1375 *lease_broken = cancelled;
1377 if (!cancelled && !bias)
1378 ldlm_cli_cancel(&och->och_lease_handle, 0);
1380 if (cancelled) { /* no need to excute intent */
1385 rc = ll_close_inode_openhandle(inode, och, bias, data);
1389 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1392 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1396 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1398 static int ll_lease_file_resync(struct obd_client_handle *och,
1399 struct inode *inode, void __user *uarg)
1401 struct ll_sb_info *sbi = ll_i2sbi(inode);
1402 struct md_op_data *op_data;
1403 struct ll_ioc_lease_id ioc;
1404 __u64 data_version_unused;
1408 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1409 LUSTRE_OPC_ANY, NULL);
1410 if (IS_ERR(op_data))
1411 RETURN(PTR_ERR(op_data));
1413 if (copy_from_user(&ioc, uarg, sizeof(ioc)))
1416 /* before starting file resync, it's necessary to clean up page cache
1417 * in client memory, otherwise once the layout version is increased,
1418 * writing back cached data will be denied the OSTs. */
1419 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1423 op_data->op_lease_handle = och->och_lease_handle;
1424 op_data->op_mirror_id = ioc.lil_mirror_id;
1425 rc = md_file_resync(sbi->ll_md_exp, op_data);
1431 ll_finish_md_op_data(op_data);
1435 static int ll_merge_attr_nolock(const struct lu_env *env, struct inode *inode)
1437 struct ll_inode_info *lli = ll_i2info(inode);
1438 struct cl_object *obj = lli->lli_clob;
1439 struct cl_attr *attr = vvp_env_thread_attr(env);
1447 /* Merge timestamps the most recently obtained from MDS with
1448 * timestamps obtained from OSTs.
1450 * Do not overwrite atime of inode because it may be refreshed
1451 * by file_accessed() function. If the read was served by cache
1452 * data, there is no RPC to be sent so that atime may not be
1453 * transferred to OSTs at all. MDT only updates atime at close time
1454 * if it's at least 'mdd.*.atime_diff' older.
1455 * All in all, the atime in Lustre does not strictly comply with
1456 * POSIX. Solving this problem needs to send an RPC to MDT for each
1457 * read, this will hurt performance.
1459 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1460 inode->i_atime.tv_sec < lli->lli_atime)
1461 inode->i_atime.tv_sec = lli->lli_atime;
1463 inode->i_mtime.tv_sec = lli->lli_mtime;
1464 inode->i_ctime.tv_sec = lli->lli_ctime;
1466 mtime = inode->i_mtime.tv_sec;
1467 atime = inode->i_atime.tv_sec;
1468 ctime = inode->i_ctime.tv_sec;
1470 cl_object_attr_lock(obj);
1471 if (CFS_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1474 rc = cl_object_attr_get(env, obj, attr);
1475 cl_object_attr_unlock(obj);
1478 GOTO(out, rc = (rc == -ENODATA ? 0 : rc));
1480 if (atime < attr->cat_atime)
1481 atime = attr->cat_atime;
1483 if (ctime < attr->cat_ctime)
1484 ctime = attr->cat_ctime;
1486 if (mtime < attr->cat_mtime)
1487 mtime = attr->cat_mtime;
1489 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu i_blocks %llu\n",
1490 PFID(&lli->lli_fid), attr->cat_size, attr->cat_blocks);
1492 if (llcrypt_require_key(inode) == -ENOKEY) {
1493 /* Without the key, round up encrypted file size to next
1494 * LUSTRE_ENCRYPTION_UNIT_SIZE. Clear text size is put in
1495 * lli_lazysize for proper file size setting at close time.
1497 lli->lli_attr_valid |= OBD_MD_FLLAZYSIZE;
1498 lli->lli_lazysize = attr->cat_size;
1499 attr->cat_size = round_up(attr->cat_size,
1500 LUSTRE_ENCRYPTION_UNIT_SIZE);
1502 i_size_write(inode, attr->cat_size);
1503 inode->i_blocks = attr->cat_blocks;
1505 inode->i_mtime.tv_sec = mtime;
1506 inode->i_atime.tv_sec = atime;
1507 inode->i_ctime.tv_sec = ctime;
1514 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1518 ll_inode_size_lock(inode);
1519 rc = ll_merge_attr_nolock(env, inode);
1520 ll_inode_size_unlock(inode);
1525 /* Use to update size and blocks on inode for LSOM if there is no contention */
1526 int ll_merge_attr_try(const struct lu_env *env, struct inode *inode)
1530 if (ll_inode_size_trylock(inode)) {
1531 rc = ll_merge_attr_nolock(env, inode);
1532 ll_inode_size_unlock(inode);
1539 * Set designated mirror for I/O.
1541 * So far only read, write, and truncated can support to issue I/O to
1542 * designated mirror.
1544 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1546 struct ll_file_data *fd = file->private_data;
1548 /* clear layout version for generic(non-resync) I/O in case it carries
1549 * stale layout version due to I/O restart */
1550 io->ci_layout_version = 0;
1552 /* FLR: disable non-delay for designated mirror I/O because obviously
1553 * only one mirror is available */
1554 if (fd->fd_designated_mirror > 0) {
1556 io->ci_designated_mirror = fd->fd_designated_mirror;
1557 io->ci_layout_version = fd->fd_layout_version;
1560 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1561 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1565 * This is relatime_need_update() from Linux 5.17, which is not exported.
1567 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1568 struct timespec64 now)
1571 if (!(mnt->mnt_flags & MNT_RELATIME))
1574 * Is mtime younger than atime? If yes, update atime:
1576 if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1579 * Is ctime younger than atime? If yes, update atime:
1581 if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1585 * Is the previous atime value older than a day? If yes,
1588 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1591 * Good, we can skip the atime update:
1597 * Very similar to kernel function: !__atime_needs_update()
1599 static bool file_is_noatime(const struct file *file)
1601 struct vfsmount *mnt = file->f_path.mnt;
1602 struct inode *inode = file_inode((struct file *)file);
1603 struct timespec64 now;
1605 if (file->f_flags & O_NOATIME)
1608 if (inode->i_flags & S_NOATIME)
1611 if (IS_NOATIME(inode))
1614 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1617 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1620 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1623 now = current_time(inode);
1625 if (!relatime_need_update(mnt, inode, now))
1631 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1632 struct vvp_io_args *args)
1634 struct inode *inode = file_inode(file);
1635 struct ll_file_data *fd = file->private_data;
1637 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1638 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1640 if (iot == CIT_WRITE) {
1641 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1642 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1643 file->f_flags & O_DIRECT ||
1645 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1646 io->u.ci_wr.wr_sync |= !!(args &&
1647 (args->u.normal.via_iocb->ki_flags &
1653 io->ci_iocb_nowait = !!(args &&
1654 (args->u.normal.via_iocb->ki_flags &
1658 io->ci_obj = ll_i2info(inode)->lli_clob;
1659 io->ci_lockreq = CILR_MAYBE;
1660 if (ll_file_nolock(file)) {
1661 io->ci_lockreq = CILR_NEVER;
1662 io->ci_no_srvlock = 1;
1663 } else if (file->f_flags & O_APPEND) {
1664 io->ci_lockreq = CILR_MANDATORY;
1666 io->ci_noatime = file_is_noatime(file);
1667 io->ci_async_readahead = false;
1669 /* FLR: only use non-delay I/O for read as there is only one
1670 * avaliable mirror for write. */
1671 io->ci_ndelay = !(iot == CIT_WRITE);
1673 ll_io_set_mirror(io, file);
1676 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1679 struct ll_inode_info *lli = ll_i2info(inode);
1680 struct ll_sb_info *sbi = ll_i2sbi(inode);
1681 enum obd_heat_type sample_type;
1682 enum obd_heat_type iobyte_type;
1683 __u64 now = ktime_get_real_seconds();
1685 if (!ll_sbi_has_file_heat(sbi) ||
1686 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1689 if (iot == CIT_READ) {
1690 sample_type = OBD_HEAT_READSAMPLE;
1691 iobyte_type = OBD_HEAT_READBYTE;
1692 } else if (iot == CIT_WRITE) {
1693 sample_type = OBD_HEAT_WRITESAMPLE;
1694 iobyte_type = OBD_HEAT_WRITEBYTE;
1699 spin_lock(&lli->lli_heat_lock);
1700 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1701 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1702 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1703 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1704 spin_unlock(&lli->lli_heat_lock);
1708 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1709 struct file *file, enum cl_io_type iot,
1710 loff_t *ppos, size_t bytes)
1712 struct vvp_io *vio = vvp_env_io(env);
1713 struct inode *inode = file_inode(file);
1714 struct ll_inode_info *lli = ll_i2info(inode);
1715 struct ll_sb_info *sbi = ll_i2sbi(inode);
1716 struct ll_file_data *fd = file->private_data;
1717 struct range_lock range;
1718 bool range_locked = false;
1724 unsigned int retried = 0, dio_lock = 0;
1725 bool is_aio = false;
1726 bool is_parallel_dio = false;
1727 struct cl_dio_aio *ci_dio_aio = NULL;
1728 size_t per_bytes, max_io_bytes;
1733 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, bytes: %zu\n",
1734 file_dentry(file)->d_name.name,
1735 iot == CIT_READ ? "read" : "write", *ppos, bytes);
1737 max_io_bytes = min_t(size_t, PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT,
1738 sbi->ll_cache->ccc_lru_max >> 2) << PAGE_SHIFT;
1740 io = vvp_env_thread_io(env);
1741 if (file->f_flags & O_DIRECT) {
1742 if (file->f_flags & O_APPEND)
1744 if (!is_sync_kiocb(args->u.normal.via_iocb))
1747 /* the kernel does not support AIO on pipes, and parallel DIO
1748 * uses part of the AIO path, so we must not do parallel dio
1751 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1754 if (!ll_sbi_has_parallel_dio(sbi))
1755 is_parallel_dio = false;
1757 ci_dio_aio = cl_dio_aio_alloc(args->u.normal.via_iocb,
1758 ll_i2info(inode)->lli_clob, is_aio);
1760 GOTO(out, rc = -ENOMEM);
1765 * IO block size need be aware of cached page limit, otherwise
1766 * if we have small max_cached_mb but large block IO issued, io
1767 * could not be finished and blocked whole client.
1769 if (file->f_flags & O_DIRECT || bytes < max_io_bytes) {
1773 per_bytes = max_io_bytes;
1776 io = vvp_env_thread_io(env);
1777 ll_io_init(io, file, iot, args);
1778 io->ci_dio_aio = ci_dio_aio;
1779 io->ci_dio_lock = dio_lock;
1780 io->ci_ndelay_tried = retried;
1781 io->ci_parallel_dio = is_parallel_dio;
1783 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1784 if (file->f_flags & O_APPEND)
1785 range_lock_init(&range, 0, LUSTRE_EOF);
1787 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1789 vio->vui_fd = file->private_data;
1790 vio->vui_iter = args->u.normal.via_iter;
1791 vio->vui_iocb = args->u.normal.via_iocb;
1792 /* Direct IO reads must also take range lock,
1793 * or multiple reads will try to work on the same pages
1794 * See LU-6227 for details.
1796 if (((iot == CIT_WRITE) ||
1797 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1798 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1799 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1801 rc = range_lock(&lli->lli_write_tree, &range);
1805 range_locked = true;
1808 ll_cl_add(inode, env, io, LCC_RW);
1809 rc = cl_io_loop(env, io);
1810 ll_cl_remove(inode, env);
1812 /* cl_io_rw_init() handled IO */
1816 if (io->ci_dio_aio && !is_aio) {
1817 struct cl_sync_io *anchor = &io->ci_dio_aio->cda_sync;
1819 /* for dio, EIOCBQUEUED is an implementation detail,
1820 * and we don't return it to userspace
1822 if (rc == -EIOCBQUEUED)
1825 /* N/B: parallel DIO may be disabled during i/o submission;
1826 * if that occurs, I/O shifts to sync, so it's all resolved
1827 * before we get here, and this wait call completes
1830 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1836 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1838 range_unlock(&lli->lli_write_tree, &range);
1839 range_locked = false;
1842 if (io->ci_bytes > 0) {
1844 result += io->ci_bytes;
1845 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1849 bytes -= io->ci_bytes;
1851 /* prepare IO restart */
1853 args->u.normal.via_iter = vio->vui_iter;
1857 * Reexpand iov count because it was zero
1860 iov_iter_reexpand(vio->vui_iter, bytes);
1861 if (per_bytes == io->ci_bytes)
1862 io->ci_need_restart = 1;
1866 cl_io_fini(env, io);
1869 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1870 file->f_path.dentry->d_name.name,
1871 iot, rc, result, io->ci_need_restart);
1873 if ((!rc || rc == -ENODATA || rc == -ENOLCK || rc == -EIOCBQUEUED) &&
1874 bytes > 0 && io->ci_need_restart && retries-- > 0) {
1876 "%s: restart %s from ppos=%lld bytes=%zu retries=%u ret=%zd: rc = %d\n",
1877 file_dentry(file)->d_name.name,
1878 iot == CIT_READ ? "read" : "write",
1879 *ppos, bytes, retries, result, rc);
1880 /* preserve the tried count for FLR */
1881 retried = io->ci_ndelay_tried;
1882 dio_lock = io->ci_dio_lock;
1886 if (io->ci_dio_aio) {
1887 /* set the number of bytes successfully moved in the aio */
1889 io->ci_dio_aio->cda_bytes = result;
1891 * VFS will call aio_complete() if no -EIOCBQUEUED
1892 * is returned for AIO, so we can not call aio_complete()
1893 * in our end_io(). (cda_no_aio_complete is always set for
1896 * NB: Setting cda_no_aio_complete like this is safe because
1897 * the atomic_dec_and_lock in cl_sync_io_note has implicit
1898 * memory barriers, so this will be seen by whichever thread
1899 * completes the DIO/AIO, even if it's not this one.
1901 if (is_aio && rc != -EIOCBQUEUED)
1902 io->ci_dio_aio->cda_no_aio_complete = 1;
1903 /* if an aio enqueued successfully (-EIOCBQUEUED), then Lustre
1904 * will call aio_complete rather than the vfs, so we return 0
1905 * to tell the VFS we're handling it
1907 else if (is_aio) /* rc == -EIOCBQUEUED */
1910 * Drop the reference held by the llite layer on this top level
1913 * For DIO, this frees it here, since IO is complete, and for
1914 * AIO, we will call aio_complete() (and then free this top
1915 * level context) once all the outstanding chunks of this AIO
1918 cl_sync_io_note(env, &io->ci_dio_aio->cda_sync,
1919 rc == -EIOCBQUEUED ? 0 : rc);
1921 LASSERT(io->ci_dio_aio->cda_creator_free);
1922 cl_dio_aio_free(env, io->ci_dio_aio);
1923 io->ci_dio_aio = NULL;
1927 if (iot == CIT_READ) {
1929 ll_stats_ops_tally(ll_i2sbi(inode),
1930 LPROC_LL_READ_BYTES, result);
1931 } else if (iot == CIT_WRITE) {
1933 ll_stats_ops_tally(ll_i2sbi(inode),
1934 LPROC_LL_WRITE_BYTES, result);
1935 fd->fd_write_failed = false;
1936 } else if (result == 0 && rc == 0) {
1939 fd->fd_write_failed = true;
1941 fd->fd_write_failed = false;
1942 } else if (rc != -ERESTARTSYS) {
1943 fd->fd_write_failed = true;
1947 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1949 ll_heat_add(inode, iot, result);
1951 RETURN(result > 0 ? result : rc);
1955 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1956 * especially for small I/O.
1958 * To serve a read request, CLIO has to create and initialize a cl_io and
1959 * then request DLM lock. This has turned out to have siginificant overhead
1960 * and affects the performance of small I/O dramatically.
1962 * It's not necessary to create a cl_io for each I/O. Under the help of read
1963 * ahead, most of the pages being read are already in memory cache and we can
1964 * read those pages directly because if the pages exist, the corresponding DLM
1965 * lock must exist so that page content must be valid.
1967 * In fast read implementation, the llite speculatively finds and reads pages
1968 * in memory cache. There are three scenarios for fast read:
1969 * - If the page exists and is uptodate, kernel VM will provide the data and
1970 * CLIO won't be intervened;
1971 * - If the page was brought into memory by read ahead, it will be exported
1972 * and read ahead parameters will be updated;
1973 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1974 * it will go back and invoke normal read, i.e., a cl_io will be created
1975 * and DLM lock will be requested.
1977 * POSIX compliance: posix standard states that read is intended to be atomic.
1978 * Lustre read implementation is in line with Linux kernel read implementation
1979 * and neither of them complies with POSIX standard in this matter. Fast read
1980 * doesn't make the situation worse on single node but it may interleave write
1981 * results from multiple nodes due to short read handling in ll_file_aio_read().
1983 * \param env - lu_env
1984 * \param iocb - kiocb from kernel
1985 * \param iter - user space buffers where the data will be copied
1987 * \retval - number of bytes have been read, or error code if error occurred.
1990 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1992 struct ll_inode_info *lli = ll_i2info(file_inode(iocb->ki_filp));
1995 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1998 /* NB: we can't do direct IO for fast read because it will need a lock
1999 * to make IO engine happy. */
2000 if (iocb->ki_filp->f_flags & O_DIRECT)
2003 if (ll_layout_version_get(lli) == CL_LAYOUT_GEN_NONE)
2006 result = generic_file_read_iter(iocb, iter);
2008 /* If the first page is not in cache, generic_file_aio_read() will be
2009 * returned with -ENODATA. Fall back to full read path.
2010 * See corresponding code in ll_readpage().
2012 * if we raced with page deletion, we might get EIO. Rather than add
2013 * locking to the fast path for this rare case, fall back to the full
2014 * read path. (See vvp_io_read_start() for rest of handling.
2016 if (result == -ENODATA || result == -EIO)
2020 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
2021 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
2022 LPROC_LL_READ_BYTES, result);
2029 * Confine read iter lest read beyond the EOF
2031 * \param iocb [in] kernel iocb
2032 * \param to [in] reader iov_iter
2034 * \retval <0 failure
2036 * \retval >0 @iocb->ki_pos has passed the EOF
2038 static int file_read_confine_iter(struct lu_env *env, struct kiocb *iocb,
2039 struct iov_iter *to)
2041 struct cl_attr *attr = vvp_env_thread_attr(env);
2042 struct file *file = iocb->ki_filp;
2043 struct inode *inode = file_inode(file);
2044 struct ll_inode_info *lli = ll_i2info(inode);
2045 loff_t read_end = iocb->ki_pos + iov_iter_count(to);
2050 cl_object_attr_lock(lli->lli_clob);
2051 rc = cl_object_attr_get(env, lli->lli_clob, attr);
2052 cl_object_attr_unlock(lli->lli_clob);
2056 kms = attr->cat_kms;
2057 /* if read beyond end-of-file, adjust read count */
2058 if (kms > 0 && (iocb->ki_pos >= kms || read_end > kms)) {
2059 rc = ll_glimpse_size(inode);
2063 size = i_size_read(inode);
2064 if (iocb->ki_pos >= size || read_end > size) {
2066 "%s: read [%llu, %llu] over eof, kms %llu, file_size %llu.\n",
2067 file_dentry(file)->d_name.name,
2068 iocb->ki_pos, read_end, kms, size);
2070 if (iocb->ki_pos >= size)
2073 if (read_end > size)
2074 iov_iter_truncate(to, size - iocb->ki_pos);
2082 * Read from a file (through the page cache).
2084 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
2087 struct vvp_io_args *args;
2088 struct file *file = iocb->ki_filp;
2092 ktime_t kstart = ktime_get();
2094 bool stale_data = false;
2098 CDEBUG(D_VFSTRACE|D_IOTRACE,
2099 "START file %s:"DFID", ppos: %lld, count: %zu\n",
2100 file_dentry(file)->d_name.name,
2101 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2102 iov_iter_count(to));
2104 if (!iov_iter_count(to))
2107 env = cl_env_get(&refcheck);
2109 RETURN(PTR_ERR(env));
2111 result = file_read_confine_iter(env, iocb, to);
2114 else if (result > 0)
2118 * Currently when PCC read failed, we do not fall back to the
2119 * normal read path, just return the error.
2120 * The resaon is that: for RW-PCC, the file data may be modified
2121 * in the PCC and inconsistent with the data on OSTs (or file
2122 * data has been removed from the Lustre file system), at this
2123 * time, fallback to the normal read path may read the wrong
2125 * TODO: for RO-PCC (readonly PCC), fall back to normal read
2126 * path: read data from data copy on OSTs.
2128 result = pcc_file_read_iter(iocb, to, &cached);
2132 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
2134 result = ll_do_fast_read(iocb, to);
2135 if (result < 0 || iov_iter_count(to) == 0)
2138 args = ll_env_args(env);
2139 args->u.normal.via_iter = to;
2140 args->u.normal.via_iocb = iocb;
2142 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
2143 &iocb->ki_pos, iov_iter_count(to));
2146 else if (result == 0)
2150 cl_env_put(env, &refcheck);
2152 if (stale_data && result > 0) {
2154 * we've reached EOF before the read, the data read are cached
2157 iov_iter_truncate(to, 0);
2162 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2163 file->private_data, iocb->ki_pos, result,
2165 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
2166 ktime_us_delta(ktime_get(), kstart));
2170 "COMPLETED: file %s:"DFID", ppos: %lld, count: %zu\n",
2171 file_dentry(file)->d_name.name,
2172 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2173 iov_iter_count(to));
2179 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
2180 * If a page is already in the page cache and dirty (and some other things -
2181 * See ll_tiny_write_begin for the instantiation of these rules), then we can
2182 * write to it without doing a full I/O, because Lustre already knows about it
2183 * and will write it out. This saves a lot of processing time.
2185 * All writes here are within one page, so exclusion is handled by the page
2186 * lock on the vm page. We do not do tiny writes for writes which touch
2187 * multiple pages because it's very unlikely multiple sequential pages are
2188 * are already dirty.
2190 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
2191 * and are unlikely to be to already dirty pages.
2193 * Attribute updates are important here, we do them in ll_tiny_write_end.
2195 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
2197 ssize_t count = iov_iter_count(iter);
2198 struct file *file = iocb->ki_filp;
2199 struct inode *inode = file_inode(file);
2200 bool lock_inode = !IS_NOSEC(inode);
2205 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
2206 * of function for why.
2208 if (count >= PAGE_SIZE ||
2209 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
2212 if (unlikely(lock_inode))
2213 ll_inode_lock(inode);
2214 result = __generic_file_write_iter(iocb, iter);
2216 if (unlikely(lock_inode))
2217 ll_inode_unlock(inode);
2219 /* If the page is not already dirty, ll_tiny_write_begin returns
2220 * -ENODATA. We continue on to normal write.
2222 if (result == -ENODATA)
2226 ll_heat_add(inode, CIT_WRITE, result);
2227 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2230 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2236 * Write to a file (through the page cache).
2238 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2240 struct vvp_io_args *args;
2242 ssize_t rc_tiny = 0, rc_normal;
2243 struct file *file = iocb->ki_filp;
2246 ktime_t kstart = ktime_get();
2251 CDEBUG(D_VFSTRACE|D_IOTRACE,
2252 "START file %s:"DFID", ppos: %lld, count: %zu\n",
2253 file_dentry(file)->d_name.name,
2254 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2255 iov_iter_count(from));
2257 if (!iov_iter_count(from))
2258 GOTO(out, rc_normal = 0);
2261 * When PCC write failed, we usually do not fall back to the normal
2262 * write path, just return the error. But there is a special case when
2263 * returned error code is -ENOSPC due to running out of space on PCC HSM
2264 * bakcend. At this time, it will fall back to normal I/O path and
2265 * retry the I/O. As the file is in HSM released state, it will restore
2266 * the file data to OSTs first and redo the write again. And the
2267 * restore process will revoke the layout lock and detach the file
2268 * from PCC cache automatically.
2270 result = pcc_file_write_iter(iocb, from, &cached);
2271 if (cached && result != -ENOSPC && result != -EDQUOT)
2272 GOTO(out, rc_normal = result);
2274 /* NB: we can't do direct IO for tiny writes because they use the page
2275 * cache, we can't do sync writes because tiny writes can't flush
2276 * pages, and we can't do append writes because we can't guarantee the
2277 * required DLM locks are held to protect file size.
2279 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2280 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2281 rc_tiny = ll_do_tiny_write(iocb, from);
2283 /* In case of error, go on and try normal write - Only stop if tiny
2284 * write completed I/O.
2286 if (iov_iter_count(from) == 0)
2287 GOTO(out, rc_normal = rc_tiny);
2289 env = cl_env_get(&refcheck);
2291 RETURN(PTR_ERR(env));
2293 args = ll_env_args(env);
2294 args->u.normal.via_iter = from;
2295 args->u.normal.via_iocb = iocb;
2297 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2298 &iocb->ki_pos, iov_iter_count(from));
2300 /* On success, combine bytes written. */
2301 if (rc_tiny >= 0 && rc_normal > 0)
2302 rc_normal += rc_tiny;
2303 /* On error, only return error from normal write if tiny write did not
2304 * write any bytes. Otherwise return bytes written by tiny write.
2306 else if (rc_tiny > 0)
2307 rc_normal = rc_tiny;
2309 cl_env_put(env, &refcheck);
2311 if (rc_normal > 0) {
2312 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2313 file->private_data, iocb->ki_pos,
2315 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2316 ktime_us_delta(ktime_get(), kstart));
2320 "COMPLETED: file %s:"DFID", ppos: %lld, count: %zu\n",
2321 file_dentry(file)->d_name.name,
2322 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2323 iov_iter_count(from));
2328 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2330 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2332 static int ll_file_get_iov_count(const struct iovec *iov,
2333 unsigned long *nr_segs, size_t *count,
2339 for (seg = 0; seg < *nr_segs; seg++) {
2340 const struct iovec *iv = &iov[seg];
2343 * If any segment has a negative length, or the cumulative
2344 * length ever wraps negative then return -EINVAL.
2347 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2349 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2354 cnt -= iv->iov_len; /* This segment is no good */
2361 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2362 unsigned long nr_segs, loff_t pos)
2369 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2376 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2377 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2378 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2379 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2380 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2382 result = ll_file_read_iter(iocb, &to);
2387 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2390 struct iovec iov = { .iov_base = buf, .iov_len = count };
2399 init_sync_kiocb(&kiocb, file);
2400 kiocb.ki_pos = *ppos;
2401 #ifdef HAVE_KIOCB_KI_LEFT
2402 kiocb.ki_left = count;
2403 #elif defined(HAVE_KI_NBYTES)
2404 kiocb.i_nbytes = count;
2407 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2408 *ppos = kiocb.ki_pos;
2414 * Write to a file (through the page cache).
2417 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2418 unsigned long nr_segs, loff_t pos)
2420 struct iov_iter from;
2425 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2432 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2433 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2434 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2435 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2436 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2438 result = ll_file_write_iter(iocb, &from);
2443 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2444 size_t count, loff_t *ppos)
2446 struct iovec iov = { .iov_base = (void __user *)buf,
2456 init_sync_kiocb(&kiocb, file);
2457 kiocb.ki_pos = *ppos;
2458 #ifdef HAVE_KIOCB_KI_LEFT
2459 kiocb.ki_left = count;
2460 #elif defined(HAVE_KI_NBYTES)
2461 kiocb.ki_nbytes = count;
2464 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2465 *ppos = kiocb.ki_pos;
2469 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2471 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2472 __u64 flags, struct lov_user_md *lum, int lum_size)
2474 struct lookup_intent oit = {
2476 .it_flags = flags | MDS_OPEN_BY_FID,
2481 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2482 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2483 /* this code will only exist for big-endian systems */
2484 lustre_swab_lov_user_md(lum, 0);
2487 ll_inode_size_lock(inode);
2488 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2490 GOTO(out_unlock, rc);
2492 ll_release_openhandle(dentry, &oit);
2495 ll_inode_size_unlock(inode);
2496 ll_intent_release(&oit);
2501 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2502 struct lov_mds_md **lmmp, int *lmm_size,
2503 struct ptlrpc_request **request)
2505 struct ll_sb_info *sbi = ll_i2sbi(inode);
2506 struct mdt_body *body;
2507 struct lov_mds_md *lmm = NULL;
2508 struct ptlrpc_request *req = NULL;
2509 struct md_op_data *op_data;
2514 rc = ll_get_default_mdsize(sbi, &lmmsize);
2518 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2519 strlen(filename), lmmsize,
2520 LUSTRE_OPC_ANY, NULL);
2521 if (IS_ERR(op_data))
2522 RETURN(PTR_ERR(op_data));
2524 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2525 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2526 ll_finish_md_op_data(op_data);
2528 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
2533 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2534 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2536 lmmsize = body->mbo_eadatasize;
2538 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2540 GOTO(out, rc = -ENODATA);
2542 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2543 LASSERT(lmm != NULL);
2545 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2546 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2547 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2548 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2549 GOTO(out, rc = -EPROTO);
2552 * This is coming from the MDS, so is probably in
2553 * little endian. We convert it to host endian before
2554 * passing it to userspace.
2556 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2557 int stripe_count = 0;
2559 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2560 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2561 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2562 if (le32_to_cpu(lmm->lmm_pattern) &
2563 LOV_PATTERN_F_RELEASED)
2565 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2567 /* if function called for directory - we should
2568 * avoid swab not existent lsm objects
2570 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2571 S_ISREG(body->mbo_mode))
2572 lustre_swab_lov_user_md_objects(
2573 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2575 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2576 S_ISREG(body->mbo_mode))
2577 lustre_swab_lov_user_md_objects(
2578 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2580 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2581 lustre_swab_lov_comp_md_v1(
2582 (struct lov_comp_md_v1 *)lmm);
2586 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2587 struct lov_comp_md_v1 *comp_v1 = NULL;
2588 struct lov_comp_md_entry_v1 *ent;
2589 struct lov_user_md_v1 *v1 = NULL;
2593 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2594 /* Dump the striping information */
2595 for (; i < comp_v1->lcm_entry_count; i++) {
2596 ent = &comp_v1->lcm_entries[i];
2597 off = ent->lcme_offset;
2598 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2600 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2601 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2605 GOTO(out, rc = -EINVAL);
2607 lmm->lmm_stripe_count = v1->lmm_stripe_count;
2608 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2610 * Return valid stripe_count and stripe_size instead of 0 for
2611 * DoM files to avoid divide-by-zero for older userspace that
2612 * calls this ioctl, e.g. lustre ADIO driver.
2614 if (lmm->lmm_stripe_count == 0)
2615 lmm->lmm_stripe_count = 1;
2616 if (lmm->lmm_stripe_size == 0) {
2617 /* Since the first component of the file data is placed
2618 * on the MDT for faster access, the stripe_size of the
2619 * second one is always that applications which are
2622 if (lmm->lmm_pattern & LOV_PATTERN_MDT)
2623 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2625 i = comp_v1->lcm_entry_count > 1 ?
2626 comp_v1->lcm_entry_count - 1 : 0;
2627 ent = &comp_v1->lcm_entries[i];
2628 off = ent->lcme_offset;
2629 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2630 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2635 *lmm_size = lmmsize;
2640 static int ll_lov_setea(struct inode *inode, struct file *file,
2643 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2644 struct lov_user_md *lump;
2645 int lum_size = sizeof(*lump) + sizeof(struct lov_user_ost_data);
2649 if (!capable(CAP_SYS_ADMIN))
2652 OBD_ALLOC_LARGE(lump, lum_size);
2656 if (copy_from_user(lump, arg, lum_size))
2657 GOTO(out_lump, rc = -EFAULT);
2659 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2661 cl_lov_delay_create_clear(&file->f_flags);
2664 OBD_FREE_LARGE(lump, lum_size);
2668 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2675 /* exit before doing any work if pointer is bad */
2676 if (unlikely(!ll_access_ok(lum, sizeof(struct lov_user_md))))
2679 env = cl_env_get(&refcheck);
2681 RETURN(PTR_ERR(env));
2683 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2684 cl_env_put(env, &refcheck);
2688 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2691 struct lov_user_md __user *lum = arg;
2692 struct lov_user_md *klum;
2694 __u64 flags = FMODE_WRITE;
2697 rc = ll_copy_user_md(lum, &klum);
2702 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2707 rc = put_user(0, &lum->lmm_stripe_count);
2711 rc = ll_layout_refresh(inode, &gen);
2715 rc = ll_file_getstripe(inode, arg, lum_size);
2716 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2717 ll_i2info(inode)->lli_clob) {
2718 struct iattr attr = { 0 };
2720 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2721 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2724 cl_lov_delay_create_clear(&file->f_flags);
2727 OBD_FREE_LARGE(klum, lum_size);
2733 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2735 struct ll_inode_info *lli = ll_i2info(inode);
2736 struct cl_object *obj = lli->lli_clob;
2737 struct ll_file_data *fd = file->private_data;
2738 struct ll_grouplock grouplock;
2743 CWARN("group id for group lock must not be 0\n");
2747 if (ll_file_nolock(file))
2748 RETURN(-EOPNOTSUPP);
2750 if (file->f_flags & O_NONBLOCK) {
2751 if (!mutex_trylock(&lli->lli_group_mutex))
2754 mutex_lock(&lli->lli_group_mutex);
2757 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2758 CWARN("group lock already existed with gid %lu\n",
2759 fd->fd_grouplock.lg_gid);
2760 GOTO(out, rc = -EINVAL);
2762 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2763 if (file->f_flags & O_NONBLOCK)
2764 GOTO(out, rc = -EAGAIN);
2765 mutex_unlock(&lli->lli_group_mutex);
2766 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2767 GOTO(retry, rc = 0);
2769 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2772 * XXX: group lock needs to protect all OST objects while PFL
2773 * can add new OST objects during the IO, so we'd instantiate
2774 * all OST objects before getting its group lock.
2779 struct cl_layout cl = {
2780 .cl_is_composite = false,
2782 struct lu_extent ext = {
2784 .e_end = OBD_OBJECT_EOF,
2787 env = cl_env_get(&refcheck);
2789 GOTO(out, rc = PTR_ERR(env));
2791 rc = cl_object_layout_get(env, obj, &cl);
2792 if (rc >= 0 && cl.cl_is_composite)
2793 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2796 cl_env_put(env, &refcheck);
2801 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2802 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2807 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2808 fd->fd_grouplock = grouplock;
2809 if (lli->lli_group_users == 0)
2810 lli->lli_group_gid = grouplock.lg_gid;
2811 lli->lli_group_users++;
2813 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2815 mutex_unlock(&lli->lli_group_mutex);
2820 static int ll_put_grouplock(struct inode *inode, struct file *file,
2823 struct ll_inode_info *lli = ll_i2info(inode);
2824 struct ll_file_data *fd = file->private_data;
2825 struct ll_grouplock grouplock;
2829 mutex_lock(&lli->lli_group_mutex);
2830 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2831 CWARN("no group lock held\n");
2832 GOTO(out, rc = -EINVAL);
2835 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2837 if (fd->fd_grouplock.lg_gid != arg) {
2838 CWARN("group lock %lu doesn't match current id %lu\n",
2839 arg, fd->fd_grouplock.lg_gid);
2840 GOTO(out, rc = -EINVAL);
2843 grouplock = fd->fd_grouplock;
2844 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2845 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2847 cl_put_grouplock(&grouplock);
2849 lli->lli_group_users--;
2850 if (lli->lli_group_users == 0) {
2851 lli->lli_group_gid = 0;
2852 wake_up_var(&lli->lli_group_users);
2854 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2857 mutex_unlock(&lli->lli_group_mutex);
2863 * Close inode open handle
2865 * \param dentry [in] dentry which contains the inode
2866 * \param it [in,out] intent which contains open info and result
2869 * \retval <0 failure
2871 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2873 struct inode *inode = dentry->d_inode;
2874 struct obd_client_handle *och;
2880 /* Root ? Do nothing. */
2881 if (is_root_inode(inode))
2884 /* No open handle to close? Move away */
2885 if (!it_disposition(it, DISP_OPEN_OPEN))
2888 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2890 OBD_ALLOC(och, sizeof(*och));
2892 GOTO(out, rc = -ENOMEM);
2894 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2898 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2900 /* this one is in place of ll_file_open */
2901 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2902 ptlrpc_req_finished(it->it_request);
2903 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2909 * Get size for inode for which FIEMAP mapping is requested.
2910 * Make the FIEMAP get_info call and returns the result.
2911 * \param fiemap kernel buffer to hold extens
2912 * \param num_bytes kernel buffer size
2914 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2920 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2923 /* Checks for fiemap flags */
2924 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2925 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2929 /* Check for FIEMAP_FLAG_SYNC */
2930 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2931 rc = filemap_fdatawrite(inode->i_mapping);
2936 env = cl_env_get(&refcheck);
2938 RETURN(PTR_ERR(env));
2940 if (i_size_read(inode) == 0) {
2941 rc = ll_glimpse_size(inode);
2946 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLPROJID;
2947 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2948 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2950 /* If filesize is 0, then there would be no objects for mapping */
2951 if (fmkey.lfik_oa.o_size == 0) {
2952 fiemap->fm_mapped_extents = 0;
2956 fmkey.lfik_fiemap = *fiemap;
2958 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2959 &fmkey, fiemap, &num_bytes);
2961 cl_env_put(env, &refcheck);
2965 static int fid2path_for_enc_file(struct inode *parent, char *gfpath,
2968 struct dentry *de = NULL, *de_parent = d_find_any_alias(parent);
2969 struct llcrypt_str lltr = LLTR_INIT(NULL, 0);
2970 struct llcrypt_str de_name;
2971 char *p, *ptr = gfpath;
2972 size_t len = 0, len_orig = 0;
2973 int enckey = -1, nameenc = -1;
2977 while ((p = strsep(&gfpath, "/")) != NULL) {
2985 len_orig = strlen(p);
2987 rc = sscanf(p, "["SFID"]", RFID(&fid));
2989 p = strchr(p, ']') + 1;
2995 if (!IS_ENCRYPTED(parent)) {
2996 if (gfpathlen < len + 1) {
3001 memmove(ptr, p, len);
3005 gfpathlen -= len + 1;
3009 /* From here, we know parent is encrypted */
3012 rc = llcrypt_prepare_readdir(parent);
3013 if (rc && rc != -ENOKEY) {
3020 if (llcrypt_has_encryption_key(parent))
3026 llcrypt_policy_has_filename_enc(parent);
3029 /* Even if names are not encrypted, we still need to call
3030 * ll_fname_disk_to_usr in order to decode names as they are
3031 * coming from the wire.
3033 rc = llcrypt_fname_alloc_buffer(parent, NAME_MAX + 1, &lltr);
3041 rc = ll_fname_disk_to_usr(parent, 0, 0, &de_name,
3044 llcrypt_fname_free_buffer(&lltr);
3048 lltr.name[lltr.len] = '\0';
3050 if (lltr.len <= len_orig && gfpathlen >= lltr.len + 1) {
3051 memcpy(ptr, lltr.name, lltr.len);
3056 gfpathlen -= lltr.len + 1;
3060 llcrypt_fname_free_buffer(&lltr);
3062 if (rc == -EOVERFLOW) {
3069 /* We reached the end of the string, which means
3070 * we are dealing with the last component in the path.
3071 * So save a useless lookup and exit.
3077 if (enckey == 0 || nameenc == 0)
3080 ll_inode_lock(parent);
3081 de = lookup_one_len(p, de_parent, len);
3082 ll_inode_unlock(parent);
3083 if (IS_ERR_OR_NULL(de) || !de->d_inode) {
3089 parent = de->d_inode;
3096 if (!IS_ERR_OR_NULL(de))
3101 int __ll_fid2path(struct inode *inode, struct getinfo_fid2path *gfout,
3102 size_t outsize, __u32 pathlen_orig)
3104 struct obd_export *exp = ll_i2mdexp(inode);
3107 /* Append root FID after gfout to let MDT know the root FID so that
3108 * it can lookup the correct path, this is mainly for fileset.
3109 * old server without fileset mount support will ignore this.
3111 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
3113 /* Call mdc_iocontrol */
3114 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
3116 if (!rc && gfout->gf_pathlen && gfout->gf_u.gf_path[0] == '/') {
3117 /* by convention, server side (mdt_path_current()) puts
3118 * a leading '/' to tell client that we are dealing with
3121 rc = fid2path_for_enc_file(inode, gfout->gf_u.gf_path,
3123 if (!rc && strlen(gfout->gf_u.gf_path) > pathlen_orig)
3130 int ll_fid2path(struct inode *inode, void __user *arg)
3132 const struct getinfo_fid2path __user *gfin = arg;
3133 __u32 pathlen, pathlen_orig;
3134 struct getinfo_fid2path *gfout;
3140 if (!capable(CAP_DAC_READ_SEARCH) &&
3141 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
3144 /* Only need to get the buflen */
3145 if (get_user(pathlen, &gfin->gf_pathlen))
3148 if (pathlen > PATH_MAX)
3150 pathlen_orig = pathlen;
3153 outsize = sizeof(*gfout) + pathlen;
3154 OBD_ALLOC(gfout, outsize);
3158 if (copy_from_user(gfout, arg, sizeof(*gfout)))
3159 GOTO(gf_free, rc = -EFAULT);
3161 gfout->gf_pathlen = pathlen;
3162 rc = __ll_fid2path(inode, gfout, outsize, pathlen_orig);
3166 if (copy_to_user(arg, gfout, sizeof(*gfout) + pathlen_orig))
3170 OBD_FREE(gfout, outsize);
3171 if (rc == -ENAMETOOLONG) {
3172 pathlen += PATH_MAX;
3179 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
3181 struct cl_object *obj = ll_i2info(inode)->lli_clob;
3189 ioc->idv_version = 0;
3190 ioc->idv_layout_version = UINT_MAX;
3192 /* If no file object initialized, we consider its version is 0. */
3196 env = cl_env_get(&refcheck);
3198 RETURN(PTR_ERR(env));
3200 io = vvp_env_thread_io(env);
3202 io->u.ci_data_version.dv_data_version = 0;
3203 io->u.ci_data_version.dv_layout_version = UINT_MAX;
3204 io->u.ci_data_version.dv_flags = ioc->idv_flags;
3207 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
3208 result = cl_io_loop(env, io);
3210 result = io->ci_result;
3212 ioc->idv_version = io->u.ci_data_version.dv_data_version;
3213 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
3214 cl_io_fini(env, io);
3216 if (unlikely(io->ci_need_restart))
3219 cl_env_put(env, &refcheck);
3225 * Read the data_version for inode.
3227 * This value is computed using stripe object version on OST.
3228 * Version is computed using server side locking.
3230 * @param flags if do sync on the OST side;
3232 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
3233 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
3235 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
3237 struct ioc_data_version ioc = { .idv_flags = flags };
3240 rc = ll_ioc_data_version(inode, &ioc);
3242 *data_version = ioc.idv_version;
3248 * Trigger a HSM release request for the provided inode.
3250 int ll_hsm_release(struct inode *inode)
3253 struct obd_client_handle *och = NULL;
3254 __u64 data_version = 0;
3260 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
3261 ll_i2sbi(inode)->ll_fsname,
3262 PFID(&ll_i2info(inode)->lli_fid));
3264 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
3266 GOTO(out, rc = PTR_ERR(och));
3268 /* Grab latest data_version and [am]time values */
3269 rc = ll_data_version(inode, &data_version,
3270 LL_DV_WR_FLUSH | LL_DV_SZ_UPDATE);
3274 env = cl_env_get(&refcheck);
3276 GOTO(out, rc = PTR_ERR(env));
3278 rc = ll_merge_attr(env, inode);
3279 cl_env_put(env, &refcheck);
3281 /* If error happen, we have the wrong size for a file.
3287 /* Release the file.
3288 * NB: lease lock handle is released in mdc_hsm_release_pack() because
3289 * we still need it to pack l_remote_handle to MDT. */
3290 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
3296 if (och != NULL && !IS_ERR(och)) /* close the file */
3297 ll_lease_close(och, inode, NULL);
3302 struct ll_swap_stack {
3305 struct inode *inode1;
3306 struct inode *inode2;
3311 static int ll_swap_layouts(struct file *file1, struct file *file2,
3312 struct lustre_swap_layouts *lsl)
3314 struct mdc_swap_layouts msl;
3315 struct md_op_data *op_data;
3318 struct ll_swap_stack *llss = NULL;
3321 OBD_ALLOC_PTR(llss);
3325 llss->inode1 = file_inode(file1);
3326 llss->inode2 = file_inode(file2);
3328 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
3332 /* we use 2 bool because it is easier to swap than 2 bits */
3333 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
3334 llss->check_dv1 = true;
3336 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
3337 llss->check_dv2 = true;
3339 /* we cannot use lsl->sl_dvX directly because we may swap them */
3340 llss->dv1 = lsl->sl_dv1;
3341 llss->dv2 = lsl->sl_dv2;
3343 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
3344 if (rc == 0) /* same file, done! */
3347 if (rc < 0) { /* sequentialize it */
3348 swap(llss->inode1, llss->inode2);
3350 swap(llss->dv1, llss->dv2);
3351 swap(llss->check_dv1, llss->check_dv2);
3355 if (gid != 0) { /* application asks to flush dirty cache */
3356 rc = ll_get_grouplock(llss->inode1, file1, gid);
3360 rc = ll_get_grouplock(llss->inode2, file2, gid);
3362 ll_put_grouplock(llss->inode1, file1, gid);
3367 /* ultimate check, before swaping the layouts we check if
3368 * dataversion has changed (if requested) */
3369 if (llss->check_dv1) {
3370 rc = ll_data_version(llss->inode1, &dv, 0);
3373 if (dv != llss->dv1)
3374 GOTO(putgl, rc = -EAGAIN);
3377 if (llss->check_dv2) {
3378 rc = ll_data_version(llss->inode2, &dv, 0);
3381 if (dv != llss->dv2)
3382 GOTO(putgl, rc = -EAGAIN);
3385 /* struct md_op_data is used to send the swap args to the mdt
3386 * only flags is missing, so we use struct mdc_swap_layouts
3387 * through the md_op_data->op_data */
3388 /* flags from user space have to be converted before they are send to
3389 * server, no flag is sent today, they are only used on the client */
3392 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
3393 0, LUSTRE_OPC_ANY, &msl);
3394 if (IS_ERR(op_data))
3395 GOTO(free, rc = PTR_ERR(op_data));
3397 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
3398 sizeof(*op_data), op_data, NULL);
3399 ll_finish_md_op_data(op_data);
3406 ll_put_grouplock(llss->inode2, file2, gid);
3407 ll_put_grouplock(llss->inode1, file1, gid);
3417 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3419 struct obd_export *exp = ll_i2mdexp(inode);
3420 struct md_op_data *op_data;
3424 /* Detect out-of range masks */
3425 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3428 /* Non-root users are forbidden to set or clear flags which are
3429 * NOT defined in HSM_USER_MASK. */
3430 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3431 !capable(CAP_SYS_ADMIN))
3434 if (!exp_connect_archive_id_array(exp)) {
3435 /* Detect out-of range archive id */
3436 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3437 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3441 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3442 LUSTRE_OPC_ANY, hss);
3443 if (IS_ERR(op_data))
3444 RETURN(PTR_ERR(op_data));
3446 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3449 ll_finish_md_op_data(op_data);
3454 static int ll_hsm_import(struct inode *inode, struct file *file,
3455 struct hsm_user_import *hui)
3457 struct hsm_state_set *hss = NULL;
3458 struct iattr *attr = NULL;
3462 if (!S_ISREG(inode->i_mode))
3468 GOTO(out, rc = -ENOMEM);
3470 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3471 hss->hss_archive_id = hui->hui_archive_id;
3472 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3473 rc = ll_hsm_state_set(inode, hss);
3477 OBD_ALLOC_PTR(attr);
3479 GOTO(out, rc = -ENOMEM);
3481 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3482 attr->ia_mode |= S_IFREG;
3483 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3484 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3485 attr->ia_size = hui->hui_size;
3486 attr->ia_mtime.tv_sec = hui->hui_mtime;
3487 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3488 attr->ia_atime.tv_sec = hui->hui_atime;
3489 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3491 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3492 ATTR_UID | ATTR_GID |
3493 ATTR_MTIME | ATTR_MTIME_SET |
3494 ATTR_ATIME | ATTR_ATIME_SET;
3497 /* inode lock owner set in ll_setattr_raw()*/
3498 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3501 inode_unlock(inode);
3513 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3515 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3516 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3519 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3521 struct inode *inode = file_inode(file);
3523 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3524 ATTR_MTIME | ATTR_MTIME_SET |
3527 .tv_sec = lfu->lfu_atime_sec,
3528 .tv_nsec = lfu->lfu_atime_nsec,
3531 .tv_sec = lfu->lfu_mtime_sec,
3532 .tv_nsec = lfu->lfu_mtime_nsec,
3535 .tv_sec = lfu->lfu_ctime_sec,
3536 .tv_nsec = lfu->lfu_ctime_nsec,
3542 if (!capable(CAP_SYS_ADMIN))
3545 if (!S_ISREG(inode->i_mode))
3549 /* inode lock owner set in ll_setattr_raw()*/
3550 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3552 inode_unlock(inode);
3557 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3560 case MODE_READ_USER:
3562 case MODE_WRITE_USER:
3569 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3571 /* Used to allow the upper layers of the client to request an LDLM lock
3572 * without doing an actual read or write.
3574 * Used for ladvise lockahead to manually request specific locks.
3576 * \param[in] file file this ladvise lock request is on
3577 * \param[in] ladvise ladvise struct describing this lock request
3579 * \retval 0 success, no detailed result available (sync requests
3580 * and requests sent to the server [not handled locally]
3581 * cannot return detailed results)
3582 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3583 * see definitions for details.
3584 * \retval negative negative errno on error
3586 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3588 struct lu_env *env = NULL;
3589 struct cl_io *io = NULL;
3590 struct cl_lock *lock = NULL;
3591 struct cl_lock_descr *descr = NULL;
3592 struct dentry *dentry = file->f_path.dentry;
3593 struct inode *inode = dentry->d_inode;
3594 enum cl_lock_mode cl_mode;
3595 off_t start = ladvise->lla_start;
3596 off_t end = ladvise->lla_end;
3603 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3604 dentry, dentry->d_inode,
3605 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3608 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3610 GOTO(out, result = cl_mode);
3612 /* Get IO environment */
3613 result = cl_io_get(inode, &env, &io, &refcheck);
3617 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3620 * nothing to do for this io. This currently happens when
3621 * stripe sub-object's are not yet created.
3623 result = io->ci_result;
3624 } else if (result == 0) {
3625 lock = vvp_env_lock(env);
3626 descr = &lock->cll_descr;
3628 descr->cld_obj = io->ci_obj;
3629 /* Convert byte offsets to pages */
3630 descr->cld_start = start >> PAGE_SHIFT;
3631 descr->cld_end = end >> PAGE_SHIFT;
3632 descr->cld_mode = cl_mode;
3633 /* CEF_MUST is used because we do not want to convert a
3634 * lockahead request to a lockless lock */
3635 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3637 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3638 descr->cld_enq_flags |= CEF_SPECULATIVE;
3640 result = cl_lock_request(env, io, lock);
3642 /* On success, we need to release the lock */
3644 cl_lock_release(env, lock);
3646 cl_io_fini(env, io);
3647 cl_env_put(env, &refcheck);
3649 /* -ECANCELED indicates a matching lock with a different extent
3650 * was already present, and -EEXIST indicates a matching lock
3651 * on exactly the same extent was already present.
3652 * We convert them to positive values for userspace to make
3653 * recognizing true errors easier.
3654 * Note we can only return these detailed results on async requests,
3655 * as sync requests look the same as i/o requests for locking. */
3656 if (result == -ECANCELED)
3657 result = LLA_RESULT_DIFFERENT;
3658 else if (result == -EEXIST)
3659 result = LLA_RESULT_SAME;
3664 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3666 static int ll_ladvise_sanity(struct inode *inode,
3667 struct llapi_lu_ladvise *ladvise)
3669 struct ll_sb_info *sbi = ll_i2sbi(inode);
3670 enum lu_ladvise_type advice = ladvise->lla_advice;
3671 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3672 * be in the first 32 bits of enum ladvise_flags */
3673 __u32 flags = ladvise->lla_peradvice_flags;
3674 /* 3 lines at 80 characters per line, should be plenty */
3677 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3680 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3681 sbi->ll_fsname, advice,
3682 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3686 /* Per-advice checks */
3688 case LU_LADVISE_LOCKNOEXPAND:
3689 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3691 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3692 "rc = %d\n", sbi->ll_fsname, flags,
3693 ladvise_names[advice], rc);
3697 case LU_LADVISE_LOCKAHEAD:
3698 /* Currently only READ and WRITE modes can be requested */
3699 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3700 ladvise->lla_lockahead_mode == 0) {
3702 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3703 "rc = %d\n", sbi->ll_fsname,
3704 ladvise->lla_lockahead_mode,
3705 ladvise_names[advice], rc);
3709 case LU_LADVISE_WILLREAD:
3710 case LU_LADVISE_DONTNEED:
3712 /* Note fall through above - These checks apply to all advices
3713 * except LOCKNOEXPAND */
3714 if (flags & ~LF_DEFAULT_MASK) {
3716 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3717 "rc = %d\n", sbi->ll_fsname, flags,
3718 ladvise_names[advice], rc);
3721 if (ladvise->lla_start >= ladvise->lla_end) {
3723 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3724 "for %s: rc = %d\n", sbi->ll_fsname,
3725 ladvise->lla_start, ladvise->lla_end,
3726 ladvise_names[advice], rc);
3738 * Give file access advices
3740 * The ladvise interface is similar to Linux fadvise() system call, except it
3741 * forwards the advices directly from Lustre client to server. The server side
3742 * codes will apply appropriate read-ahead and caching techniques for the
3743 * corresponding files.
3745 * A typical workload for ladvise is e.g. a bunch of different clients are
3746 * doing small random reads of a file, so prefetching pages into OSS cache
3747 * with big linear reads before the random IO is a net benefit. Fetching
3748 * all that data into each client cache with fadvise() may not be, due to
3749 * much more data being sent to the client.
3751 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3752 struct llapi_lu_ladvise *ladvise)
3756 struct cl_ladvise_io *lio;
3761 env = cl_env_get(&refcheck);
3763 RETURN(PTR_ERR(env));
3765 io = vvp_env_thread_io(env);
3766 io->ci_obj = ll_i2info(inode)->lli_clob;
3768 /* initialize parameters for ladvise */
3769 lio = &io->u.ci_ladvise;
3770 lio->li_start = ladvise->lla_start;
3771 lio->li_end = ladvise->lla_end;
3772 lio->li_fid = ll_inode2fid(inode);
3773 lio->li_advice = ladvise->lla_advice;
3774 lio->li_flags = flags;
3776 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3777 rc = cl_io_loop(env, io);
3781 cl_io_fini(env, io);
3782 cl_env_put(env, &refcheck);
3786 static int ll_lock_noexpand(struct file *file, int flags)
3788 struct ll_file_data *fd = file->private_data;
3790 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3795 #ifndef HAVE_FILEATTR_GET
3796 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3799 struct fsxattr fsxattr;
3801 if (copy_from_user(&fsxattr, uarg, sizeof(fsxattr)))
3804 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3805 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3806 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3807 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3808 if (copy_to_user(uarg, &fsxattr, sizeof(fsxattr)))
3815 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3819 * Project Quota ID state is only allowed to change from within the init
3820 * namespace. Enforce that restriction only if we are trying to change
3821 * the quota ID state. Everything else is allowed in user namespaces.
3823 if (current_user_ns() == &init_user_ns) {
3825 * Caller is allowed to change the project ID. if it is being
3826 * changed, make sure that the new value is valid.
3828 if (ll_i2info(inode)->lli_projid != projid &&
3829 !projid_valid(make_kprojid(&init_user_ns, projid)))
3835 if (ll_i2info(inode)->lli_projid != projid)
3838 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3839 if (!(xflags & FS_XFLAG_PROJINHERIT))
3842 if (xflags & FS_XFLAG_PROJINHERIT)
3849 int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3851 struct ptlrpc_request *req = NULL;
3852 struct md_op_data *op_data;
3853 struct cl_object *obj;
3854 unsigned int inode_flags;
3857 CDEBUG(D_QUOTA, DFID" xflags=%x projid=%u\n",
3858 PFID(ll_inode2fid(inode)), xflags, projid);
3859 rc = ll_ioctl_check_project(inode, xflags, projid);
3863 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3864 LUSTRE_OPC_ANY, NULL);
3865 if (IS_ERR(op_data))
3866 RETURN(PTR_ERR(op_data));
3868 inode_flags = ll_xflags_to_inode_flags(xflags);
3869 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3870 if (xflags & FS_XFLAG_PROJINHERIT)
3871 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3873 /* pass projid to md_op_data */
3874 op_data->op_projid = projid;
3876 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3877 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3878 ptlrpc_req_finished(req);
3880 GOTO(out_fsxattr, rc);
3881 ll_update_inode_flags(inode, op_data->op_attr_flags);
3883 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3884 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3885 GOTO(out_fsxattr, rc);
3887 obj = ll_i2info(inode)->lli_clob;
3889 struct iattr attr = { 0 };
3891 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3895 ll_finish_md_op_data(op_data);
3899 #ifndef HAVE_FILEATTR_GET
3900 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3903 struct fsxattr fsxattr;
3907 if (copy_from_user(&fsxattr, uarg, sizeof(fsxattr)))
3910 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3911 fsxattr.fsx_projid));
3915 int ll_ioctl_project(struct file *file, unsigned int cmd, void __user *uarg)
3917 struct lu_project lu_project;
3918 struct dentry *dentry = file_dentry(file);
3919 struct inode *inode = file_inode(file);
3920 struct dentry *child_dentry = NULL;
3921 int rc = 0, name_len;
3923 if (copy_from_user(&lu_project, uarg, sizeof(lu_project)))
3926 /* apply child dentry if name is valid */
3927 name_len = strnlen(lu_project.project_name, NAME_MAX);
3928 if (name_len > 0 && name_len <= NAME_MAX) {
3929 ll_inode_lock(inode);
3930 child_dentry = lookup_one_len(lu_project.project_name,
3932 ll_inode_unlock(inode);
3933 if (IS_ERR(child_dentry)) {
3934 rc = PTR_ERR(child_dentry);
3937 inode = child_dentry->d_inode;
3942 } else if (name_len > NAME_MAX) {
3947 switch (lu_project.project_type) {
3948 case LU_PROJECT_SET:
3949 rc = ll_set_project(inode, lu_project.project_xflags,
3950 lu_project.project_id);
3952 case LU_PROJECT_GET:
3953 lu_project.project_xflags =
3954 ll_inode_flags_to_xflags(inode->i_flags);
3955 if (test_bit(LLIF_PROJECT_INHERIT,
3956 &ll_i2info(inode)->lli_flags))
3957 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3958 lu_project.project_id = ll_i2info(inode)->lli_projid;
3959 if (copy_to_user(uarg, &lu_project, sizeof(lu_project))) {
3969 if (!IS_ERR_OR_NULL(child_dentry))
3974 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3977 struct inode *inode = file_inode(file);
3978 struct ll_file_data *fd = file->private_data;
3979 struct ll_inode_info *lli = ll_i2info(inode);
3980 struct obd_client_handle *och = NULL;
3981 struct split_param sp;
3982 struct pcc_param param;
3983 bool lease_broken = false;
3985 enum mds_op_bias bias = 0;
3987 struct file *layout_file = NULL;
3989 size_t data_size = 0;
3990 bool attached = false;
3995 mutex_lock(&lli->lli_och_mutex);
3996 if (fd->fd_lease_och != NULL) {
3997 och = fd->fd_lease_och;
3998 fd->fd_lease_och = NULL;
4000 mutex_unlock(&lli->lli_och_mutex);
4005 fmode = och->och_flags;
4007 switch (ioc->lil_flags) {
4008 case LL_LEASE_RESYNC_DONE:
4009 if (ioc->lil_count > IOC_IDS_MAX)
4010 GOTO(out_lease_close, rc = -EINVAL);
4012 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
4013 OBD_ALLOC(data, data_size);
4015 GOTO(out_lease_close, rc = -ENOMEM);
4017 if (copy_from_user(data, uarg, data_size))
4018 GOTO(out_lease_close, rc = -EFAULT);
4020 bias = MDS_CLOSE_RESYNC_DONE;
4022 case LL_LEASE_LAYOUT_MERGE:
4023 if (ioc->lil_count != 1)
4024 GOTO(out_lease_close, rc = -EINVAL);
4026 uarg += sizeof(*ioc);
4027 if (copy_from_user(&fdv, uarg, sizeof(fdv)))
4028 GOTO(out_lease_close, rc = -EFAULT);
4030 layout_file = fget(fdv);
4032 GOTO(out_lease_close, rc = -EBADF);
4034 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
4035 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
4036 GOTO(out_lease_close, rc = -EPERM);
4038 data = file_inode(layout_file);
4039 bias = MDS_CLOSE_LAYOUT_MERGE;
4041 case LL_LEASE_LAYOUT_SPLIT: {
4044 if (ioc->lil_count != 2)
4045 GOTO(out_lease_close, rc = -EINVAL);
4047 uarg += sizeof(*ioc);
4048 if (copy_from_user(&fdv, uarg, sizeof(fdv)))
4049 GOTO(out_lease_close, rc = -EFAULT);
4051 uarg += sizeof(fdv);
4052 if (copy_from_user(&mirror_id, uarg, sizeof(mirror_id)))
4053 GOTO(out_lease_close, rc = -EFAULT);
4054 if (mirror_id >= MIRROR_ID_NEG)
4055 GOTO(out_lease_close, rc = -EINVAL);
4057 layout_file = fget(fdv);
4059 GOTO(out_lease_close, rc = -EBADF);
4061 /* if layout_file == file, it means to destroy the mirror */
4062 sp.sp_inode = file_inode(layout_file);
4063 sp.sp_mirror_id = (__u16)mirror_id;
4065 bias = MDS_CLOSE_LAYOUT_SPLIT;
4068 case LL_LEASE_PCC_ATTACH:
4069 if (ioc->lil_count != 1)
4072 if (IS_ENCRYPTED(inode))
4073 RETURN(-EOPNOTSUPP);
4075 uarg += sizeof(*ioc);
4076 if (copy_from_user(¶m.pa_archive_id, uarg, sizeof(__u32)))
4077 GOTO(out_lease_close, rc2 = -EFAULT);
4079 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
4081 GOTO(out_lease_close, rc2);
4084 /* Grab latest data version */
4085 rc2 = ll_data_version(inode, ¶m.pa_data_version,
4088 GOTO(out_lease_close, rc2);
4091 bias = MDS_PCC_ATTACH;
4094 /* without close intent */
4099 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
4103 rc = ll_lease_och_release(inode, file);
4112 if (ioc->lil_flags == LL_LEASE_RESYNC_DONE && data)
4113 OBD_FREE(data, data_size);
4118 if (ioc->lil_flags == LL_LEASE_PCC_ATTACH) {
4121 rc = pcc_readwrite_attach_fini(file, inode,
4122 param.pa_layout_gen,
4127 ll_layout_refresh(inode, &fd->fd_layout_version);
4130 rc = ll_lease_type_from_fmode(fmode);
4134 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
4137 struct inode *inode = file_inode(file);
4138 struct ll_inode_info *lli = ll_i2info(inode);
4139 struct ll_file_data *fd = file->private_data;
4140 struct obd_client_handle *och = NULL;
4141 __u64 open_flags = 0;
4147 switch (ioc->lil_mode) {
4148 case LL_LEASE_WRLCK:
4149 if (!(file->f_mode & FMODE_WRITE))
4151 fmode = FMODE_WRITE;
4153 case LL_LEASE_RDLCK:
4154 if (!(file->f_mode & FMODE_READ))
4158 case LL_LEASE_UNLCK:
4159 RETURN(ll_file_unlock_lease(file, ioc, uarg));
4164 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
4166 /* apply for lease */
4167 if (ioc->lil_flags & LL_LEASE_RESYNC)
4168 open_flags = MDS_OPEN_RESYNC;
4169 och = ll_lease_open(inode, file, fmode, open_flags);
4171 RETURN(PTR_ERR(och));
4173 if (ioc->lil_flags & LL_LEASE_RESYNC) {
4174 rc = ll_lease_file_resync(och, inode, uarg);
4176 ll_lease_close(och, inode, NULL);
4179 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
4181 ll_lease_close(och, inode, NULL);
4187 mutex_lock(&lli->lli_och_mutex);
4188 if (fd->fd_lease_och == NULL) {
4189 fd->fd_lease_och = och;
4192 mutex_unlock(&lli->lli_och_mutex);
4194 /* impossible now that only excl is supported for now */
4195 ll_lease_close(och, inode, &lease_broken);
4201 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
4203 struct ll_inode_info *lli = ll_i2info(inode);
4204 struct ll_sb_info *sbi = ll_i2sbi(inode);
4205 __u64 now = ktime_get_real_seconds();
4208 spin_lock(&lli->lli_heat_lock);
4209 heat->lh_flags = lli->lli_heat_flags;
4210 for (i = 0; i < heat->lh_count; i++)
4211 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
4212 now, sbi->ll_heat_decay_weight,
4213 sbi->ll_heat_period_second);
4214 spin_unlock(&lli->lli_heat_lock);
4217 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
4219 struct ll_inode_info *lli = ll_i2info(inode);
4222 spin_lock(&lli->lli_heat_lock);
4223 if (flags & LU_HEAT_FLAG_CLEAR)
4224 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
4226 if (flags & LU_HEAT_FLAG_OFF)
4227 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
4229 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
4231 spin_unlock(&lli->lli_heat_lock);
4237 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4239 struct inode *inode = file_inode(file);
4240 struct ll_file_data *fd = file->private_data;
4241 void __user *uarg = (void __user *)arg;
4245 CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
4246 PFID(ll_inode2fid(inode)), inode, cmd, arg);
4247 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
4249 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
4250 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
4253 /* can't do a generic karg == NULL check here, since it is too noisy and
4254 * we need to return -ENOTTY for unsupported ioctls instead of -EINVAL.
4257 case LL_IOC_GETFLAGS:
4258 /* Get the current value of the file flags */
4259 return put_user(fd->fd_flags, (int __user *)arg);
4260 case LL_IOC_SETFLAGS:
4261 case LL_IOC_CLRFLAGS:
4262 /* Set or clear specific file flags */
4263 /* XXX This probably needs checks to ensure the flags are
4264 * not abused, and to handle any flag side effects.
4266 if (get_user(flags, (int __user *)arg))
4269 if (cmd == LL_IOC_SETFLAGS) {
4270 if ((flags & LL_FILE_IGNORE_LOCK) &&
4271 !(file->f_flags & O_DIRECT)) {
4273 CERROR("%s: unable to disable locking on non-O_DIRECT file "DFID": rc = %d\n",
4274 current->comm, PFID(ll_inode2fid(inode)),
4279 fd->fd_flags |= flags;
4281 fd->fd_flags &= ~flags;
4284 case LL_IOC_LOV_SETSTRIPE:
4285 case LL_IOC_LOV_SETSTRIPE_NEW:
4286 RETURN(ll_lov_setstripe(inode, file, uarg));
4287 case LL_IOC_LOV_SETEA:
4288 RETURN(ll_lov_setea(inode, file, uarg));
4289 case LL_IOC_LOV_SWAP_LAYOUTS: {
4291 struct lustre_swap_layouts lsl;
4293 if (copy_from_user(&lsl, uarg, sizeof(lsl)))
4296 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
4299 file2 = fget(lsl.sl_fd);
4303 /* O_WRONLY or O_RDWR */
4304 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
4305 GOTO(out, rc = -EPERM);
4307 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
4308 struct obd_client_handle *och = NULL;
4309 struct ll_inode_info *lli;
4310 struct inode *inode2;
4312 lli = ll_i2info(inode);
4313 mutex_lock(&lli->lli_och_mutex);
4314 if (fd->fd_lease_och != NULL) {
4315 och = fd->fd_lease_och;
4316 fd->fd_lease_och = NULL;
4318 mutex_unlock(&lli->lli_och_mutex);
4320 GOTO(out, rc = -ENOLCK);
4321 inode2 = file_inode(file2);
4322 rc = ll_swap_layouts_close(och, inode, inode2);
4324 rc = ll_swap_layouts(file, file2, &lsl);
4330 case LL_IOC_LOV_GETSTRIPE:
4331 case LL_IOC_LOV_GETSTRIPE_NEW:
4332 RETURN(ll_file_getstripe(inode, uarg, 0));
4333 case LL_IOC_GROUP_LOCK:
4334 RETURN(ll_get_grouplock(inode, file, arg));
4335 case LL_IOC_GROUP_UNLOCK:
4336 RETURN(ll_put_grouplock(inode, file, arg));
4337 case LL_IOC_DATA_VERSION: {
4338 struct ioc_data_version idv;
4341 if (copy_from_user(&idv, uarg, sizeof(idv)))
4344 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
4345 rc = ll_ioc_data_version(inode, &idv);
4347 if (rc == 0 && copy_to_user(uarg, &idv, sizeof(idv)))
4352 case LL_IOC_HSM_STATE_GET: {
4353 struct md_op_data *op_data;
4354 struct hsm_user_state *hus;
4357 if (!ll_access_ok(uarg, sizeof(*hus)))
4364 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4365 LUSTRE_OPC_ANY, hus);
4366 if (IS_ERR(op_data)) {
4367 rc = PTR_ERR(op_data);
4369 rc = obd_iocontrol(cmd, ll_i2mdexp(inode),
4370 sizeof(*op_data), op_data, NULL);
4372 if (copy_to_user(uarg, hus, sizeof(*hus)))
4375 ll_finish_md_op_data(op_data);
4380 case LL_IOC_HSM_STATE_SET: {
4381 struct hsm_state_set *hss;
4388 if (copy_from_user(hss, uarg, sizeof(*hss)))
4391 rc = ll_hsm_state_set(inode, hss);
4396 case LL_IOC_HSM_ACTION: {
4397 struct md_op_data *op_data;
4398 struct hsm_current_action *hca;
4402 if (!ll_access_ok(uarg, sizeof(*hca)))
4409 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4410 LUSTRE_OPC_ANY, hca);
4411 if (IS_ERR(op_data)) {
4413 RETURN(PTR_ERR(op_data));
4416 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4419 GOTO(skip_copy, rc);
4421 /* The hsm_current_action retreived from the server could
4422 * contain corrupt information. If it is incorrect data collect
4423 * debug information. We still send the data even if incorrect
4424 * to user land to handle.
4426 action = hsm_user_action2name(hca->hca_action);
4427 if (strcmp(action, "UNKNOWN") == 0 ||
4428 hca->hca_state > HPS_DONE) {
4430 "HSM current state %s action %s, offset = %llu, length %llu\n",
4431 hsm_progress_state2name(hca->hca_state), action,
4432 hca->hca_location.offset, hca->hca_location.length);
4435 if (copy_to_user(uarg, hca, sizeof(*hca)))
4438 ll_finish_md_op_data(op_data);
4442 case LL_IOC_SET_LEASE_OLD: {
4443 struct ll_ioc_lease ioc = { .lil_mode = arg };
4445 RETURN(ll_file_set_lease(file, &ioc, 0));
4447 case LL_IOC_SET_LEASE: {
4448 struct ll_ioc_lease ioc;
4450 if (copy_from_user(&ioc, uarg, sizeof(ioc)))
4453 RETURN(ll_file_set_lease(file, &ioc, uarg));
4455 case LL_IOC_GET_LEASE: {
4456 struct ll_inode_info *lli = ll_i2info(inode);
4457 struct ldlm_lock *lock = NULL;
4460 mutex_lock(&lli->lli_och_mutex);
4461 if (fd->fd_lease_och != NULL) {
4462 struct obd_client_handle *och = fd->fd_lease_och;
4464 lock = ldlm_handle2lock(&och->och_lease_handle);
4466 lock_res_and_lock(lock);
4467 if (!ldlm_is_cancel(lock))
4468 fmode = och->och_flags;
4470 unlock_res_and_lock(lock);
4471 LDLM_LOCK_PUT(lock);
4474 mutex_unlock(&lli->lli_och_mutex);
4476 RETURN(ll_lease_type_from_fmode(fmode));
4478 case LL_IOC_HSM_IMPORT: {
4479 struct hsm_user_import *hui;
4485 if (copy_from_user(hui, uarg, sizeof(*hui)))
4488 rc = ll_hsm_import(inode, file, hui);
4493 case LL_IOC_FUTIMES_3: {
4494 struct ll_futimes_3 lfu;
4496 if (copy_from_user(&lfu, uarg, sizeof(lfu)))
4499 RETURN(ll_file_futimes_3(file, &lfu));
4501 case LL_IOC_LADVISE: {
4502 struct llapi_ladvise_hdr *k_ladvise_hdr;
4503 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4506 int alloc_size = sizeof(*k_ladvise_hdr);
4509 u_ladvise_hdr = uarg;
4510 OBD_ALLOC_PTR(k_ladvise_hdr);
4511 if (k_ladvise_hdr == NULL)
4514 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4515 GOTO(out_ladvise, rc = -EFAULT);
4517 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4518 k_ladvise_hdr->lah_count < 1)
4519 GOTO(out_ladvise, rc = -EINVAL);
4521 num_advise = k_ladvise_hdr->lah_count;
4522 if (num_advise >= LAH_COUNT_MAX)
4523 GOTO(out_ladvise, rc = -EFBIG);
4525 OBD_FREE_PTR(k_ladvise_hdr);
4526 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4527 lah_advise[num_advise]);
4528 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4529 if (k_ladvise_hdr == NULL)
4533 * TODO: submit multiple advices to one server in a single RPC
4535 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4536 GOTO(out_ladvise, rc = -EFAULT);
4538 for (i = 0; i < num_advise; i++) {
4539 struct llapi_lu_ladvise *k_ladvise =
4540 &k_ladvise_hdr->lah_advise[i];
4541 struct llapi_lu_ladvise __user *u_ladvise =
4542 &u_ladvise_hdr->lah_advise[i];
4544 rc = ll_ladvise_sanity(inode, k_ladvise);
4546 GOTO(out_ladvise, rc);
4548 switch (k_ladvise->lla_advice) {
4549 case LU_LADVISE_LOCKNOEXPAND:
4550 rc = ll_lock_noexpand(file,
4551 k_ladvise->lla_peradvice_flags);
4552 GOTO(out_ladvise, rc);
4553 case LU_LADVISE_LOCKAHEAD:
4555 rc = ll_file_lock_ahead(file, k_ladvise);
4558 GOTO(out_ladvise, rc);
4561 &u_ladvise->lla_lockahead_result))
4562 GOTO(out_ladvise, rc = -EFAULT);
4565 rc = ll_ladvise(inode, file,
4566 k_ladvise_hdr->lah_flags,
4569 GOTO(out_ladvise, rc);
4576 OBD_FREE(k_ladvise_hdr, alloc_size);
4579 case LL_IOC_FLR_SET_MIRROR: {
4580 /* mirror I/O must be direct to avoid polluting page cache
4582 if (!(file->f_flags & O_DIRECT))
4585 fd->fd_designated_mirror = arg;
4588 case LL_IOC_HEAT_GET: {
4589 struct lu_heat uheat;
4590 struct lu_heat *heat;
4593 if (copy_from_user(&uheat, uarg, sizeof(uheat)))
4596 if (uheat.lh_count > OBD_HEAT_COUNT)
4597 uheat.lh_count = OBD_HEAT_COUNT;
4599 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4600 OBD_ALLOC(heat, size);
4604 heat->lh_count = uheat.lh_count;
4605 ll_heat_get(inode, heat);
4606 rc = copy_to_user(uarg, heat, size);
4607 OBD_FREE(heat, size);
4608 RETURN(rc ? -EFAULT : 0);
4610 case LL_IOC_HEAT_SET: {
4613 if (copy_from_user(&flags, uarg, sizeof(flags)))
4616 rc = ll_heat_set(inode, flags);
4619 case LL_IOC_PCC_DETACH: {
4620 struct lu_pcc_detach *detach;
4622 OBD_ALLOC_PTR(detach);
4626 if (copy_from_user(detach, uarg, sizeof(*detach)))
4627 GOTO(out_detach_free, rc = -EFAULT);
4629 if (!S_ISREG(inode->i_mode))
4630 GOTO(out_detach_free, rc = -EINVAL);
4632 if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
4633 GOTO(out_detach_free, rc = -EPERM);
4635 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4637 OBD_FREE_PTR(detach);
4640 case LL_IOC_PCC_STATE: {
4641 struct lu_pcc_state __user *ustate = uarg;
4642 struct lu_pcc_state *state;
4644 OBD_ALLOC_PTR(state);
4648 if (copy_from_user(state, ustate, sizeof(*state)))
4649 GOTO(out_state, rc = -EFAULT);
4651 rc = pcc_ioctl_state(file, inode, state);
4653 GOTO(out_state, rc);
4655 if (copy_to_user(ustate, state, sizeof(*state)))
4656 GOTO(out_state, rc = -EFAULT);
4659 OBD_FREE_PTR(state);
4663 rc = ll_iocontrol(inode, file, cmd, uarg);
4666 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL, uarg));
4670 static loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4672 struct inode *inode = file_inode(file);
4675 struct cl_lseek_io *lsio;
4682 env = cl_env_get(&refcheck);
4684 RETURN(PTR_ERR(env));
4686 io = vvp_env_thread_io(env);
4687 io->ci_obj = ll_i2info(inode)->lli_clob;
4688 ll_io_set_mirror(io, file);
4690 lsio = &io->u.ci_lseek;
4691 lsio->ls_start = offset;
4692 lsio->ls_whence = whence;
4693 lsio->ls_result = -ENXIO;
4696 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4698 struct vvp_io *vio = vvp_env_io(env);
4700 vio->vui_fd = file->private_data;
4701 rc = cl_io_loop(env, io);
4705 retval = rc ? : lsio->ls_result;
4706 cl_io_fini(env, io);
4707 } while (unlikely(io->ci_need_restart));
4709 cl_env_put(env, &refcheck);
4711 /* Without the key, SEEK_HOLE return value has to be
4712 * rounded up to next LUSTRE_ENCRYPTION_UNIT_SIZE.
4714 if (llcrypt_require_key(inode) == -ENOKEY && whence == SEEK_HOLE)
4715 retval = round_up(retval, LUSTRE_ENCRYPTION_UNIT_SIZE);
4720 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4722 struct inode *inode = file_inode(file);
4723 loff_t retval = offset, eof = 0;
4724 ktime_t kstart = ktime_get();
4728 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4729 PFID(ll_inode2fid(inode)), inode, retval, retval,
4732 if (origin == SEEK_END) {
4733 retval = ll_glimpse_size(inode);
4736 eof = i_size_read(inode);
4739 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4743 /* flush local cache first if any */
4744 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4747 retval = ll_lseek(file, offset, origin);
4750 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4752 retval = generic_file_llseek_size(file, offset, origin,
4753 ll_file_maxbytes(inode), eof);
4756 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4757 ktime_us_delta(ktime_get(), kstart));
4761 static int ll_flush(struct file *file, fl_owner_t id)
4763 struct inode *inode = file_inode(file);
4764 struct ll_inode_info *lli = ll_i2info(inode);
4765 struct ll_file_data *fd = file->private_data;
4768 LASSERT(!S_ISDIR(inode->i_mode));
4770 /* catch async errors that were recorded back when async writeback
4771 * failed for pages in this mapping. */
4772 rc = lli->lli_async_rc;
4773 lli->lli_async_rc = 0;
4774 if (lli->lli_clob != NULL) {
4775 err = lov_read_and_clear_async_rc(lli->lli_clob);
4780 /* The application has been told write failure already.
4781 * Do not report failure again. */
4782 if (fd->fd_write_failed)
4784 return rc ? -EIO : 0;
4788 * Called to make sure a portion of file has been written out.
4789 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4791 * Return how many pages have been written.
4793 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4794 enum cl_fsync_mode mode, int ignore_layout)
4798 struct cl_fsync_io *fio;
4803 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4804 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4807 env = cl_env_get(&refcheck);
4809 RETURN(PTR_ERR(env));
4811 io = vvp_env_thread_io(env);
4812 io->ci_obj = ll_i2info(inode)->lli_clob;
4813 io->ci_ignore_layout = ignore_layout;
4815 /* initialize parameters for sync */
4816 fio = &io->u.ci_fsync;
4817 fio->fi_start = start;
4819 fio->fi_fid = ll_inode2fid(inode);
4820 fio->fi_mode = mode;
4821 fio->fi_nr_written = 0;
4823 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4824 result = cl_io_loop(env, io);
4826 result = io->ci_result;
4828 result = fio->fi_nr_written;
4829 cl_io_fini(env, io);
4830 cl_env_put(env, &refcheck);
4836 * When dentry is provided (the 'else' case), file_dentry() may be
4837 * null and dentry must be used directly rather than pulled from
4838 * file_dentry() as is done otherwise.
4841 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4843 struct dentry *dentry = file_dentry(file);
4844 struct inode *inode = dentry->d_inode;
4845 struct ll_inode_info *lli = ll_i2info(inode);
4846 struct ptlrpc_request *req;
4847 ktime_t kstart = ktime_get();
4853 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4854 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4856 /* fsync's caller has already called _fdata{sync,write}, we want
4857 * that IO to finish before calling the osc and mdc sync methods */
4858 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4860 /* catch async errors that were recorded back when async writeback
4861 * failed for pages in this mapping. */
4862 if (!S_ISDIR(inode->i_mode)) {
4863 err = lli->lli_async_rc;
4864 lli->lli_async_rc = 0;
4867 if (lli->lli_clob != NULL) {
4868 err = lov_read_and_clear_async_rc(lli->lli_clob);
4874 if (S_ISREG(inode->i_mode) && !lli->lli_synced_to_mds) {
4876 * only the first sync on MDS makes sense,
4877 * everything else is stored on OSTs
4879 err = md_fsync(ll_i2sbi(inode)->ll_md_exp,
4880 ll_inode2fid(inode), &req);
4884 lli->lli_synced_to_mds = true;
4885 ptlrpc_req_finished(req);
4889 if (S_ISREG(inode->i_mode)) {
4890 struct ll_file_data *fd = file->private_data;
4893 /* Sync metadata on MDT first, and then sync the cached data
4896 err = pcc_fsync(file, start, end, datasync, &cached);
4898 err = cl_sync_file_range(inode, start, end,
4900 if (rc == 0 && err < 0)
4903 fd->fd_write_failed = true;
4905 fd->fd_write_failed = false;
4909 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4910 ktime_us_delta(ktime_get(), kstart));
4915 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4917 struct inode *inode = file_inode(file);
4918 struct ll_sb_info *sbi = ll_i2sbi(inode);
4919 struct ldlm_enqueue_info einfo = {
4920 .ei_type = LDLM_FLOCK,
4921 .ei_cb_cp = ldlm_flock_completion_ast,
4922 .ei_cbdata = file_lock,
4924 struct md_op_data *op_data;
4925 struct lustre_handle lockh = { 0 };
4926 union ldlm_policy_data flock = { { 0 } };
4927 int fl_type = file_lock->fl_type;
4928 ktime_t kstart = ktime_get();
4934 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4935 PFID(ll_inode2fid(inode)), file_lock);
4937 if (file_lock->fl_flags & FL_FLOCK) {
4938 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4939 /* flocks are whole-file locks */
4940 flock.l_flock.end = OFFSET_MAX;
4941 /* For flocks owner is determined by the local file desctiptor*/
4942 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4943 } else if (file_lock->fl_flags & FL_POSIX) {
4944 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4945 flock.l_flock.start = file_lock->fl_start;
4946 flock.l_flock.end = file_lock->fl_end;
4950 flock.l_flock.pid = file_lock->fl_pid;
4952 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4953 /* Somewhat ugly workaround for svc lockd.
4954 * lockd installs custom fl_lmops->lm_compare_owner that checks
4955 * for the fl_owner to be the same (which it always is on local node
4956 * I guess between lockd processes) and then compares pid.
4957 * As such we assign pid to the owner field to make it all work,
4958 * conflict with normal locks is unlikely since pid space and
4959 * pointer space for current->files are not intersecting */
4960 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4961 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4966 einfo.ei_mode = LCK_PR;
4969 /* An unlock request may or may not have any relation to
4970 * existing locks so we may not be able to pass a lock handle
4971 * via a normal ldlm_lock_cancel() request. The request may even
4972 * unlock a byte range in the middle of an existing lock. In
4973 * order to process an unlock request we need all of the same
4974 * information that is given with a normal read or write record
4975 * lock request. To avoid creating another ldlm unlock (cancel)
4976 * message we'll treat a LCK_NL flock request as an unlock. */
4977 einfo.ei_mode = LCK_NL;
4980 einfo.ei_mode = LCK_PW;
4984 CERROR("%s: fcntl from '%s' unknown lock type=%d: rc = %d\n",
4985 sbi->ll_fsname, current->comm, fl_type, rc);
5000 flags = LDLM_FL_BLOCK_NOWAIT;
5006 flags = LDLM_FL_TEST_LOCK;
5010 CERROR("%s: fcntl from '%s' unknown lock command=%d: rc = %d\n",
5011 sbi->ll_fsname, current->comm, cmd, rc);
5015 /* Save the old mode so that if the mode in the lock changes we
5016 * can decrement the appropriate reader or writer refcount. */
5017 file_lock->fl_type = einfo.ei_mode;
5019 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
5020 LUSTRE_OPC_ANY, NULL);
5021 if (IS_ERR(op_data))
5022 RETURN(PTR_ERR(op_data));
5024 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
5025 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
5026 flock.l_flock.pid, flags, einfo.ei_mode,
5027 flock.l_flock.start, flock.l_flock.end);
5029 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
5032 /* Restore the file lock type if not TEST lock. */
5033 if (!(flags & LDLM_FL_TEST_LOCK))
5034 file_lock->fl_type = fl_type;
5036 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
5037 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
5038 !(flags & LDLM_FL_TEST_LOCK))
5039 rc2 = locks_lock_file_wait(file, file_lock);
5041 if ((file_lock->fl_flags & FL_FLOCK) &&
5042 (rc == 0 || file_lock->fl_type == F_UNLCK))
5043 rc2 = flock_lock_file_wait(file, file_lock);
5044 if ((file_lock->fl_flags & FL_POSIX) &&
5045 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
5046 !(flags & LDLM_FL_TEST_LOCK))
5047 rc2 = posix_lock_file_wait(file, file_lock);
5048 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
5050 if (rc2 && file_lock->fl_type != F_UNLCK) {
5051 einfo.ei_mode = LCK_NL;
5052 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
5057 ll_finish_md_op_data(op_data);
5060 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
5061 ktime_us_delta(ktime_get(), kstart));
5065 int ll_get_fid_by_name(struct inode *parent, const char *name,
5066 int namelen, struct lu_fid *fid,
5067 struct inode **inode)
5069 struct md_op_data *op_data = NULL;
5070 struct mdt_body *body;
5071 struct ptlrpc_request *req;
5075 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
5076 LUSTRE_OPC_ANY, NULL);
5077 if (IS_ERR(op_data))
5078 RETURN(PTR_ERR(op_data));
5080 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
5081 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
5082 ll_finish_md_op_data(op_data);
5086 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
5088 GOTO(out_req, rc = -EFAULT);
5090 *fid = body->mbo_fid1;
5093 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
5095 ptlrpc_req_finished(req);
5099 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
5100 const char *name, __u32 flags)
5102 struct dentry *dchild = NULL;
5103 struct inode *child_inode = NULL;
5104 struct md_op_data *op_data;
5105 struct ptlrpc_request *request = NULL;
5106 struct obd_client_handle *och = NULL;
5108 struct mdt_body *body;
5109 __u64 data_version = 0;
5110 size_t namelen = strlen(name);
5111 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
5115 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
5116 PFID(ll_inode2fid(parent)), name,
5117 lum->lum_stripe_offset, lum->lum_stripe_count);
5119 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
5120 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
5121 lustre_swab_lmv_user_md(lum);
5123 /* Get child FID first */
5124 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
5127 dchild = d_lookup(file_dentry(file), &qstr);
5129 if (dchild->d_inode)
5130 child_inode = igrab(dchild->d_inode);
5135 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
5144 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
5145 OBD_CONNECT2_DIR_MIGRATE)) {
5146 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
5147 ll_dir_striped(child_inode)) {
5148 CERROR("%s: MDT doesn't support stripe directory "
5149 "migration!\n", ll_i2sbi(parent)->ll_fsname);
5150 GOTO(out_iput, rc = -EOPNOTSUPP);
5155 * lfs migrate command needs to be blocked on the client
5156 * by checking the migrate FID against the FID of the
5159 if (is_root_inode(child_inode))
5160 GOTO(out_iput, rc = -EINVAL);
5162 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
5163 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
5164 if (IS_ERR(op_data))
5165 GOTO(out_iput, rc = PTR_ERR(op_data));
5167 ll_inode_lock(child_inode);
5168 op_data->op_fid3 = *ll_inode2fid(child_inode);
5169 if (!fid_is_sane(&op_data->op_fid3)) {
5170 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
5171 ll_i2sbi(parent)->ll_fsname, name,
5172 PFID(&op_data->op_fid3));
5173 GOTO(out_unlock, rc = -EINVAL);
5176 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
5177 op_data->op_data = lum;
5178 op_data->op_data_size = lumlen;
5180 /* migrate dirent only for subdirs if MDS_MIGRATE_NSONLY set */
5181 if (S_ISDIR(child_inode->i_mode) && (flags & MDS_MIGRATE_NSONLY) &&
5182 lmv_dir_layout_changing(op_data->op_lso1))
5183 op_data->op_bias |= MDS_MIGRATE_NSONLY;
5186 if (S_ISREG(child_inode->i_mode)) {
5187 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
5191 GOTO(out_unlock, rc);
5194 rc = ll_data_version(child_inode, &data_version,
5197 GOTO(out_close, rc);
5199 op_data->op_open_handle = och->och_open_handle;
5200 op_data->op_data_version = data_version;
5201 op_data->op_lease_handle = och->och_lease_handle;
5202 op_data->op_bias |= MDS_CLOSE_MIGRATE;
5204 spin_lock(&och->och_mod->mod_open_req->rq_lock);
5205 och->och_mod->mod_open_req->rq_replay = 0;
5206 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
5209 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data,
5210 op_data->op_name, op_data->op_namelen,
5211 op_data->op_name, op_data->op_namelen, &request);
5213 LASSERT(request != NULL);
5214 ll_update_times(request, parent);
5217 if (rc == 0 || rc == -EAGAIN) {
5218 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
5219 LASSERT(body != NULL);
5221 /* If the server does release layout lock, then we cleanup
5222 * the client och here, otherwise release it in out_close: */
5223 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
5224 obd_mod_put(och->och_mod);
5225 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
5227 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
5233 if (request != NULL) {
5234 ptlrpc_req_finished(request);
5238 /* Try again if the lease has cancelled. */
5239 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
5244 ll_lease_close(och, child_inode, NULL);
5246 clear_nlink(child_inode);
5248 ll_inode_unlock(child_inode);
5249 ll_finish_md_op_data(op_data);
5256 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
5258 struct ll_file_data *fd = file->private_data;
5262 * In order to avoid flood of warning messages, only print one message
5263 * for one file. And the entire message rate on the client is limited
5264 * by CDEBUG_LIMIT too.
5266 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
5267 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
5268 CDEBUG_LIMIT(D_CONSOLE,
5269 "flock disabled, mount with '-o [local]flock' to enable\r\n");
5275 * test if some locks matching bits and l_req_mode are acquired
5276 * - bits can be in different locks
5277 * - if found clear the common lock bits in *bits
5278 * - the bits not found, are kept in *bits
5280 * \param bits [IN] searched lock bits [IN]
5281 * \param l_req_mode [IN] searched lock mode
5282 * \retval boolean, true iff all bits are found
5284 int ll_have_md_lock(struct obd_export *exp, struct inode *inode, __u64 *bits,
5285 enum ldlm_mode l_req_mode)
5287 struct lustre_handle lockh;
5288 union ldlm_policy_data policy;
5289 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
5290 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
5299 fid = &ll_i2info(inode)->lli_fid;
5300 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
5301 ldlm_lockname[mode]);
5303 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
5304 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
5305 policy.l_inodebits.bits = *bits & BIT(i);
5306 if (policy.l_inodebits.bits == 0)
5309 if (md_lock_match(exp, flags, fid, LDLM_IBITS, &policy, mode,
5311 struct ldlm_lock *lock;
5313 lock = ldlm_handle2lock(&lockh);
5316 ~(lock->l_policy_data.l_inodebits.bits);
5317 LDLM_LOCK_PUT(lock);
5319 *bits &= ~policy.l_inodebits.bits;
5326 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5327 struct lustre_handle *lockh, __u64 flags,
5328 enum ldlm_mode mode)
5330 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5335 fid = &ll_i2info(inode)->lli_fid;
5336 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5338 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5339 fid, LDLM_IBITS, &policy, mode, lockh);
5344 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5346 /* Already unlinked. Just update nlink and return success */
5347 if (rc == -ENOENT) {
5349 /* If it is striped directory, and there is bad stripe
5350 * Let's revalidate the dentry again, instead of returning
5352 if (ll_dir_striped(inode))
5355 /* This path cannot be hit for regular files unless in
5356 * case of obscure races, so no need to to validate
5358 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5360 } else if (rc != 0) {
5361 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5362 "%s: revalidate FID "DFID" error: rc = %d\n",
5363 ll_i2sbi(inode)->ll_fsname,
5364 PFID(ll_inode2fid(inode)), rc);
5370 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5372 struct inode *parent;
5373 struct inode *inode = dentry->d_inode;
5374 struct obd_export *exp = ll_i2mdexp(inode);
5375 struct lookup_intent oit = {
5378 struct ptlrpc_request *req = NULL;
5379 struct md_op_data *op_data;
5380 const char *name = NULL;
5385 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5386 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5388 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5389 parent = dentry->d_parent->d_inode;
5390 name = dentry->d_name.name;
5391 namelen = dentry->d_name.len;
5396 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5397 LUSTRE_OPC_ANY, NULL);
5398 if (IS_ERR(op_data))
5399 RETURN(PTR_ERR(op_data));
5401 /* Call getattr by fid */
5402 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5403 op_data->op_flags = MF_GETATTR_BY_FID;
5404 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5405 ll_finish_md_op_data(op_data);
5407 rc = ll_inode_revalidate_fini(inode, rc);
5411 rc = ll_revalidate_it_finish(req, &oit, dentry);
5413 ll_intent_release(&oit);
5417 /* Unlinked? Unhash dentry, so it is not picked up later by
5418 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5419 * here to preserve get_cwd functionality on 2.6.
5421 if (!dentry->d_inode->i_nlink)
5422 d_lustre_invalidate(dentry);
5424 ll_lookup_finish_locks(&oit, dentry);
5426 ptlrpc_req_finished(req);
5431 static int ll_merge_md_attr(struct inode *inode)
5433 struct ll_inode_info *lli = ll_i2info(inode);
5434 struct lmv_stripe_object *lsm_obj;
5435 struct cl_attr attr = { 0 };
5438 if (!ll_dir_striped(inode))
5441 down_read(&lli->lli_lsm_sem);
5442 LASSERT(lli->lli_lsm_obj != NULL);
5444 lsm_obj = lmv_stripe_object_get(lli->lli_lsm_obj);
5445 up_read(&lli->lli_lsm_sem);
5447 rc = md_merge_attr(ll_i2mdexp(inode), lsm_obj,
5448 &attr, ll_md_blocking_ast);
5449 lmv_stripe_object_put(&lsm_obj);
5453 spin_lock(&inode->i_lock);
5454 set_nlink(inode, attr.cat_nlink);
5455 spin_unlock(&inode->i_lock);
5457 inode->i_blocks = attr.cat_blocks;
5458 i_size_write(inode, attr.cat_size);
5460 ll_i2info(inode)->lli_atime = attr.cat_atime;
5461 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5462 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5467 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5468 unsigned int flags, bool foreign)
5470 struct inode *inode = de->d_inode;
5471 struct ll_sb_info *sbi = ll_i2sbi(inode);
5472 struct ll_inode_info *lli = ll_i2info(inode);
5473 struct inode *dir = de->d_parent->d_inode;
5474 bool need_glimpse = true;
5475 ktime_t kstart = ktime_get();
5478 /* The OST object(s) determine the file size, blocks and mtime. */
5479 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5480 request_mask & STATX_MTIME))
5481 need_glimpse = false;
5483 if (dentry_may_statahead(dir, de))
5484 ll_start_statahead(dir, de, need_glimpse &&
5485 !(flags & AT_STATX_DONT_SYNC));
5487 if (flags & AT_STATX_DONT_SYNC)
5488 GOTO(fill_attr, rc = 0);
5490 rc = ll_inode_revalidate(de, IT_GETATTR);
5494 /* foreign file/dir are always of zero length, so don't
5495 * need to validate size.
5497 if (S_ISREG(inode->i_mode) && !foreign) {
5501 GOTO(fill_attr, rc);
5503 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5504 if (cached && rc < 0)
5508 GOTO(fill_attr, rc);
5511 * If the returned attr is masked with OBD_MD_FLSIZE &
5512 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5513 * or blocks obtained from MDT is strictly correct, and the file
5514 * is usually not being modified by clients, and the [a|m|c]time
5515 * got from MDT is also strictly correct.
5516 * Under this circumstance, it does not need to send glimpse
5517 * RPCs to OSTs for file attributes such as the size and blocks.
5519 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5520 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5521 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5522 inode->i_mtime.tv_sec = lli->lli_mtime;
5523 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5524 inode->i_atime.tv_sec = lli->lli_atime;
5525 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5526 inode->i_ctime.tv_sec = lli->lli_ctime;
5527 GOTO(fill_attr, rc);
5530 /* In case of restore, the MDT has the right size and has
5531 * already send it back without granting the layout lock,
5532 * inode is up-to-date so glimpse is useless.
5533 * Also to glimpse we need the layout, in case of a running
5534 * restore the MDT holds the layout lock so the glimpse will
5535 * block up to the end of restore (getattr will block)
5537 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5538 rc = ll_glimpse_size(inode);
5543 /* If object isn't regular a file then don't validate size. */
5544 /* foreign dir is not striped dir */
5546 rc = ll_merge_md_attr(inode);
5551 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5552 inode->i_atime.tv_sec = lli->lli_atime;
5553 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5554 inode->i_mtime.tv_sec = lli->lli_mtime;
5555 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5556 inode->i_ctime.tv_sec = lli->lli_ctime;
5560 CFS_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5562 if (ll_need_32bit_api(sbi)) {
5563 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5564 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5565 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5567 stat->ino = inode->i_ino;
5568 stat->dev = inode->i_sb->s_dev;
5569 stat->rdev = inode->i_rdev;
5572 /* foreign symlink to be exposed as a real symlink */
5574 stat->mode = inode->i_mode;
5576 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5578 stat->uid = inode->i_uid;
5579 stat->gid = inode->i_gid;
5580 stat->atime = inode->i_atime;
5581 stat->mtime = inode->i_mtime;
5582 stat->ctime = inode->i_ctime;
5583 /* stat->blksize is used to tell about preferred IO size */
5584 if (sbi->ll_stat_blksize)
5585 stat->blksize = sbi->ll_stat_blksize;
5586 else if (S_ISREG(inode->i_mode))
5587 stat->blksize = min(PTLRPC_MAX_BRW_SIZE,
5588 1U << LL_MAX_BLKSIZE_BITS);
5589 else if (S_ISDIR(inode->i_mode))
5590 stat->blksize = min(MD_MAX_BRW_SIZE,
5591 1U << LL_MAX_BLKSIZE_BITS);
5593 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5595 stat->nlink = inode->i_nlink;
5596 stat->size = i_size_read(inode);
5597 stat->blocks = inode->i_blocks;
5599 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
5600 if (flags & AT_STATX_DONT_SYNC) {
5601 if (stat->size == 0 &&
5602 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5603 stat->size = lli->lli_lazysize;
5604 if (stat->blocks == 0 &&
5605 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5606 stat->blocks = lli->lli_lazyblocks;
5609 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5610 stat->result_mask |= STATX_BTIME;
5611 stat->btime.tv_sec = lli->lli_btime;
5614 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5615 #ifdef HAVE_LUSTRE_CRYPTO
5616 stat->attributes_mask |= STATX_ATTR_ENCRYPTED;
5618 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5619 /* if Lustre specific LUSTRE_ENCRYPT_FL flag is set, also set
5620 * ext4 equivalent to please statx
5622 if (stat->attributes & LUSTRE_ENCRYPT_FL)
5623 stat->attributes |= STATX_ATTR_ENCRYPTED;
5624 stat->result_mask &= request_mask;
5627 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5628 ktime_us_delta(ktime_get(), kstart));
5633 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
5634 int ll_getattr(struct mnt_idmap *map, const struct path *path,
5635 struct kstat *stat, u32 request_mask, unsigned int flags)
5637 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5641 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5643 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5644 AT_STATX_SYNC_AS_STAT, false);
5648 static int cl_falloc(struct file *file, struct inode *inode, int mode,
5649 loff_t offset, loff_t len)
5651 loff_t size = i_size_read(inode);
5659 env = cl_env_get(&refcheck);
5661 RETURN(PTR_ERR(env));
5663 io = vvp_env_thread_io(env);
5664 io->ci_obj = ll_i2info(inode)->lli_clob;
5665 ll_io_set_mirror(io, file);
5667 io->ci_verify_layout = 1;
5668 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5669 io->u.ci_setattr.sa_falloc_mode = mode;
5670 io->u.ci_setattr.sa_falloc_offset = offset;
5671 io->u.ci_setattr.sa_falloc_end = offset + len;
5672 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5674 CDEBUG(D_INODE, "UID %u GID %u PRJID %u\n",
5675 from_kuid(&init_user_ns, inode->i_uid),
5676 from_kgid(&init_user_ns, inode->i_gid),
5677 ll_i2info(inode)->lli_projid);
5679 io->u.ci_setattr.sa_falloc_uid = from_kuid(&init_user_ns, inode->i_uid);
5680 io->u.ci_setattr.sa_falloc_gid = from_kgid(&init_user_ns, inode->i_gid);
5681 io->u.ci_setattr.sa_falloc_projid = ll_i2info(inode)->lli_projid;
5683 if (io->u.ci_setattr.sa_falloc_end > size) {
5684 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5686 /* Check new size against VFS/VM file size limit and rlimit */
5687 rc = inode_newsize_ok(inode, newsize);
5690 if (newsize > ll_file_maxbytes(inode)) {
5691 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5692 (unsigned long long)newsize,
5693 ll_file_maxbytes(inode));
5700 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5702 rc = cl_io_loop(env, io);
5705 cl_io_fini(env, io);
5706 } while (unlikely(io->ci_need_restart));
5709 cl_env_put(env, &refcheck);
5713 static long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5715 struct inode *inode = file_inode(filp);
5718 if (offset < 0 || len <= 0)
5721 * Encrypted inodes can't handle collapse range or zero range or insert
5722 * range since we would need to re-encrypt blocks with a different IV or
5723 * XTS tweak (which are based on the logical block number).
5724 * Similar to what ext4 does.
5726 if (IS_ENCRYPTED(inode) &&
5727 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5728 FALLOC_FL_ZERO_RANGE)))
5729 RETURN(-EOPNOTSUPP);
5732 * mode == 0 (which is standard prealloc) and PUNCH is supported
5733 * Rest of mode options are not supported yet.
5735 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5736 RETURN(-EOPNOTSUPP);
5738 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5740 rc = cl_falloc(filp, inode, mode, offset, len);
5742 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5743 * used by Lustre in several places. Retuning it here would
5744 * confuse applications that explicity test for EOPNOTSUPP
5745 * (95) and fall back to ftruncate().
5747 if (rc == -ENOTSUPP)
5753 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5754 __u64 start, __u64 len)
5758 struct fiemap *fiemap;
5759 unsigned int extent_count = fieinfo->fi_extents_max;
5761 num_bytes = sizeof(*fiemap) + (extent_count *
5762 sizeof(struct fiemap_extent));
5763 OBD_ALLOC_LARGE(fiemap, num_bytes);
5768 fiemap->fm_flags = fieinfo->fi_flags;
5769 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5770 fiemap->fm_start = start;
5771 fiemap->fm_length = len;
5772 if (extent_count > 0 &&
5773 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5774 sizeof(struct fiemap_extent)) != 0)
5775 GOTO(out, rc = -EFAULT);
5777 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5779 if (IS_ENCRYPTED(inode)) {
5782 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5783 fiemap->fm_extents[i].fe_flags |=
5784 FIEMAP_EXTENT_DATA_ENCRYPTED |
5785 FIEMAP_EXTENT_ENCODED;
5788 fieinfo->fi_flags = fiemap->fm_flags;
5789 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5790 if (extent_count > 0 &&
5791 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5792 fiemap->fm_mapped_extents *
5793 sizeof(struct fiemap_extent)) != 0)
5794 GOTO(out, rc = -EFAULT);
5796 OBD_FREE_LARGE(fiemap, num_bytes);
5800 int ll_inode_permission(struct mnt_idmap *idmap, struct inode *inode, int mask)
5803 struct ll_sb_info *sbi;
5804 struct root_squash_info *squash;
5805 struct cred *cred = NULL;
5806 const struct cred *old_cred = NULL;
5807 bool squash_id = false;
5808 ktime_t kstart = ktime_get();
5812 if (mask & MAY_NOT_BLOCK)
5816 * as root inode are NOT getting validated in lookup operation,
5817 * need to revalidate PERM before permission check.
5819 if (is_root_inode(inode)) {
5820 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_GETATTR);
5825 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5826 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5828 /* squash fsuid/fsgid if needed */
5829 sbi = ll_i2sbi(inode);
5830 squash = &sbi->ll_squash;
5831 if (unlikely(squash->rsi_uid != 0 &&
5832 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5833 !test_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags))) {
5837 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5838 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5839 squash->rsi_uid, squash->rsi_gid);
5841 /* update current process's credentials
5842 * and FS capability */
5843 cred = prepare_creds();
5847 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5848 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5849 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5850 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5852 old_cred = override_creds(cred);
5855 rc = generic_permission(idmap, inode, mask);
5856 /* restore current process's credentials and FS capability */
5858 revert_creds(old_cred);
5863 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5864 ktime_us_delta(ktime_get(), kstart));
5869 /* -o localflock - only provides locally consistent flock locks */
5870 static const struct file_operations ll_file_operations = {
5871 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5872 # ifdef HAVE_SYNC_READ_WRITE
5873 .read = new_sync_read,
5874 .write = new_sync_write,
5876 .read_iter = ll_file_read_iter,
5877 .write_iter = ll_file_write_iter,
5878 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5879 .read = ll_file_read,
5880 .aio_read = ll_file_aio_read,
5881 .write = ll_file_write,
5882 .aio_write = ll_file_aio_write,
5883 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5884 .unlocked_ioctl = ll_file_ioctl,
5885 .open = ll_file_open,
5886 .release = ll_file_release,
5887 .mmap = ll_file_mmap,
5888 .llseek = ll_file_seek,
5889 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5890 .splice_read = generic_file_splice_read,
5892 .splice_read = pcc_file_splice_read,
5894 #ifdef HAVE_ITER_FILE_SPLICE_WRITE
5895 .splice_write = iter_file_splice_write,
5899 .fallocate = ll_fallocate,
5902 static const struct file_operations ll_file_operations_flock = {
5903 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5904 # ifdef HAVE_SYNC_READ_WRITE
5905 .read = new_sync_read,
5906 .write = new_sync_write,
5907 # endif /* HAVE_SYNC_READ_WRITE */
5908 .read_iter = ll_file_read_iter,
5909 .write_iter = ll_file_write_iter,
5910 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5911 .read = ll_file_read,
5912 .aio_read = ll_file_aio_read,
5913 .write = ll_file_write,
5914 .aio_write = ll_file_aio_write,
5915 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5916 .unlocked_ioctl = ll_file_ioctl,
5917 .open = ll_file_open,
5918 .release = ll_file_release,
5919 .mmap = ll_file_mmap,
5920 .llseek = ll_file_seek,
5921 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5922 .splice_read = generic_file_splice_read,
5924 .splice_read = pcc_file_splice_read,
5926 #ifdef HAVE_ITER_FILE_SPLICE_WRITE
5927 .splice_write = iter_file_splice_write,
5931 .flock = ll_file_flock,
5932 .lock = ll_file_flock,
5933 .fallocate = ll_fallocate,
5936 /* These are for -o noflock - to return ENOSYS on flock calls */
5937 static const struct file_operations ll_file_operations_noflock = {
5938 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5939 # ifdef HAVE_SYNC_READ_WRITE
5940 .read = new_sync_read,
5941 .write = new_sync_write,
5942 # endif /* HAVE_SYNC_READ_WRITE */
5943 .read_iter = ll_file_read_iter,
5944 .write_iter = ll_file_write_iter,
5945 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5946 .read = ll_file_read,
5947 .aio_read = ll_file_aio_read,
5948 .write = ll_file_write,
5949 .aio_write = ll_file_aio_write,
5950 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5951 .unlocked_ioctl = ll_file_ioctl,
5952 .open = ll_file_open,
5953 .release = ll_file_release,
5954 .mmap = ll_file_mmap,
5955 .llseek = ll_file_seek,
5956 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5957 .splice_read = generic_file_splice_read,
5959 .splice_read = pcc_file_splice_read,
5961 #ifdef HAVE_ITER_FILE_SPLICE_WRITE
5962 .splice_write = iter_file_splice_write,
5966 .flock = ll_file_noflock,
5967 .lock = ll_file_noflock,
5968 .fallocate = ll_fallocate,
5971 const struct inode_operations ll_file_inode_operations = {
5972 .setattr = ll_setattr,
5973 .getattr = ll_getattr,
5974 .permission = ll_inode_permission,
5975 #ifdef HAVE_IOP_XATTR
5976 .setxattr = ll_setxattr,
5977 .getxattr = ll_getxattr,
5978 .removexattr = ll_removexattr,
5980 .listxattr = ll_listxattr,
5981 .fiemap = ll_fiemap,
5982 .get_acl = ll_get_acl,
5983 #ifdef HAVE_IOP_SET_ACL
5984 .set_acl = ll_set_acl,
5986 #ifdef HAVE_FILEATTR_GET
5987 .fileattr_get = ll_fileattr_get,
5988 .fileattr_set = ll_fileattr_set,
5992 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5994 const struct file_operations *fops = &ll_file_operations_noflock;
5996 if (test_bit(LL_SBI_FLOCK, sbi->ll_flags))
5997 fops = &ll_file_operations_flock;
5998 else if (test_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags))
5999 fops = &ll_file_operations;
6004 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
6006 struct ll_inode_info *lli = ll_i2info(inode);
6007 struct cl_object *obj = lli->lli_clob;
6016 env = cl_env_get(&refcheck);
6018 RETURN(PTR_ERR(env));
6020 rc = cl_conf_set(env, lli->lli_clob, conf);
6024 if (conf->coc_opc == OBJECT_CONF_SET) {
6025 struct ldlm_lock *lock = conf->coc_lock;
6026 struct cl_layout cl = {
6030 LASSERT(lock != NULL);
6031 LASSERT(ldlm_has_layout(lock));
6033 /* it can only be allowed to match after layout is
6034 * applied to inode otherwise false layout would be
6035 * seen. Applying layout shoud happen before dropping
6036 * the intent lock. */
6037 ldlm_lock_allow_match(lock);
6039 rc = cl_object_layout_get(env, obj, &cl);
6044 DFID": layout version change: %u -> %u\n",
6045 PFID(&lli->lli_fid), ll_layout_version_get(lli),
6047 ll_layout_version_set(lli, cl.cl_layout_gen);
6051 cl_env_put(env, &refcheck);
6053 RETURN(rc < 0 ? rc : 0);
6056 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
6057 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
6060 struct ll_sb_info *sbi = ll_i2sbi(inode);
6061 struct ptlrpc_request *req;
6068 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
6069 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
6070 lock->l_lvb_data, lock->l_lvb_len);
6072 if (lock->l_lvb_data != NULL)
6075 /* if layout lock was granted right away, the layout is returned
6076 * within DLM_LVB of dlm reply; otherwise if the lock was ever
6077 * blocked and then granted via completion ast, we have to fetch
6078 * layout here. Please note that we can't use the LVB buffer in
6079 * completion AST because it doesn't have a large enough buffer */
6080 rc = ll_get_default_mdsize(sbi, &lmmsize);
6084 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
6085 XATTR_NAME_LOV, lmmsize, &req);
6088 GOTO(out, rc = 0); /* empty layout */
6095 if (lmmsize == 0) /* empty layout */
6098 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
6100 GOTO(out, rc = -EFAULT);
6102 OBD_ALLOC_LARGE(lvbdata, lmmsize);
6103 if (lvbdata == NULL)
6104 GOTO(out, rc = -ENOMEM);
6106 memcpy(lvbdata, lmm, lmmsize);
6107 lock_res_and_lock(lock);
6108 if (unlikely(lock->l_lvb_data == NULL)) {
6109 lock->l_lvb_type = LVB_T_LAYOUT;
6110 lock->l_lvb_data = lvbdata;
6111 lock->l_lvb_len = lmmsize;
6114 unlock_res_and_lock(lock);
6117 OBD_FREE_LARGE(lvbdata, lmmsize);
6122 ptlrpc_req_finished(req);
6127 * Apply the layout to the inode. Layout lock is held and will be released
6130 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
6131 struct inode *inode, bool try)
6133 struct ll_inode_info *lli = ll_i2info(inode);
6134 struct ll_sb_info *sbi = ll_i2sbi(inode);
6135 struct ldlm_lock *lock;
6136 struct cl_object_conf conf;
6139 bool wait_layout = false;
6142 LASSERT(lustre_handle_is_used(lockh));
6144 lock = ldlm_handle2lock(lockh);
6145 LASSERT(lock != NULL);
6147 if (!ldlm_has_layout(lock))
6148 GOTO(out, rc = -EAGAIN);
6150 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
6151 PFID(&lli->lli_fid), inode);
6153 /* in case this is a caching lock and reinstate with new inode */
6154 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
6156 lock_res_and_lock(lock);
6157 lvb_ready = ldlm_is_lvb_ready(lock);
6158 unlock_res_and_lock(lock);
6160 /* checking lvb_ready is racy but this is okay. The worst case is
6161 * that multi processes may configure the file on the same time. */
6165 rc = ll_layout_fetch(inode, lock);
6169 /* for layout lock, lmm is stored in lock's lvb.
6170 * lvb_data is immutable if the lock is held so it's safe to access it
6173 * set layout to file. Unlikely this will fail as old layout was
6174 * surely eliminated */
6175 memset(&conf, 0, sizeof conf);
6176 conf.coc_opc = OBJECT_CONF_SET;
6177 conf.coc_inode = inode;
6178 conf.coc_lock = lock;
6180 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
6181 conf.u.coc_layout.lb_len = lock->l_lvb_len;
6182 rc = ll_layout_conf(inode, &conf);
6184 /* refresh layout failed, need to wait */
6185 wait_layout = rc == -EBUSY;
6188 LDLM_LOCK_PUT(lock);
6189 ldlm_lock_decref(lockh, mode);
6191 /* wait for IO to complete if it's still being used. */
6193 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
6194 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
6196 memset(&conf, 0, sizeof conf);
6197 conf.coc_opc = OBJECT_CONF_WAIT;
6198 conf.coc_inode = inode;
6199 rc = ll_layout_conf(inode, &conf);
6203 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
6204 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
6207 if (rc == -ERESTARTSYS) {
6210 struct cl_object * obj = lli->lli_clob;
6212 env = cl_env_get(&refcheck);
6214 RETURN(PTR_ERR(env));
6216 CDEBUG(D_INODE, "prune without lock "DFID"\n",
6217 PFID(lu_object_fid(&obj->co_lu)));
6219 trunc_sem_down_write(&lli->lli_trunc_sem);
6220 cl_object_prune(env, obj);
6221 trunc_sem_up_write(&lli->lli_trunc_sem);
6222 cl_env_put(env, &refcheck);
6231 * Issue layout intent RPC to MDS.
6232 * \param inode [in] file inode
6233 * \param intent [in] layout intent
6235 * \retval 0 on success
6236 * \retval < 0 error code
6238 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
6240 struct ll_inode_info *lli = ll_i2info(inode);
6241 struct ll_sb_info *sbi = ll_i2sbi(inode);
6242 struct md_op_data *op_data;
6243 struct lookup_intent it;
6244 struct ptlrpc_request *req;
6248 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
6249 0, 0, LUSTRE_OPC_ANY, NULL);
6250 if (IS_ERR(op_data))
6251 RETURN(PTR_ERR(op_data));
6253 op_data->op_data = intent;
6254 op_data->op_data_size = sizeof(*intent);
6256 memset(&it, 0, sizeof(it));
6257 it.it_op = IT_LAYOUT;
6258 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
6259 intent->li_opc == LAYOUT_INTENT_TRUNC)
6260 it.it_flags = FMODE_WRITE;
6262 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
6263 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
6265 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
6266 &ll_md_blocking_ast, 0);
6267 if (it.it_request != NULL)
6268 ptlrpc_req_finished(it.it_request);
6269 it.it_request = NULL;
6271 ll_finish_md_op_data(op_data);
6273 /* set lock data in case this is a new lock */
6275 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
6277 ll_intent_drop_lock(&it);
6283 * This function checks if there exists a LAYOUT lock on the client side,
6284 * or enqueues it if it doesn't have one in cache.
6286 * This function will not hold layout lock so it may be revoked any time after
6287 * this function returns. Any operations depend on layout should be redone
6290 * This function should be called before lov_io_init() to get an uptodate
6291 * layout version, the caller should save the version number and after IO
6292 * is finished, this function should be called again to verify that layout
6293 * is not changed during IO time.
6295 int ll_layout_refresh(struct inode *inode, __u32 *gen)
6297 struct ll_inode_info *lli = ll_i2info(inode);
6298 struct ll_sb_info *sbi = ll_i2sbi(inode);
6299 struct lustre_handle lockh;
6300 struct layout_intent intent = {
6301 .li_opc = LAYOUT_INTENT_ACCESS,
6303 enum ldlm_mode mode;
6308 *gen = ll_layout_version_get(lli);
6309 if (!test_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags) ||
6310 *gen != CL_LAYOUT_GEN_NONE)
6314 LASSERT(fid_is_sane(ll_inode2fid(inode)));
6315 LASSERT(S_ISREG(inode->i_mode));
6317 /* take layout lock mutex to enqueue layout lock exclusively. */
6318 mutex_lock(&lli->lli_layout_mutex);
6319 lli->lli_layout_lock_owner = current;
6322 /* mostly layout lock is caching on the local side, so try to
6323 * match it before grabbing layout lock mutex. */
6324 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
6325 LCK_CR | LCK_CW | LCK_PR |
6327 if (mode != 0) { /* hit cached lock */
6328 rc = ll_layout_lock_set(&lockh, mode, inode, try);
6335 rc = ll_layout_intent(inode, &intent);
6341 *gen = ll_layout_version_get(lli);
6342 lli->lli_layout_lock_owner = NULL;
6343 mutex_unlock(&lli->lli_layout_mutex);
6349 * Issue layout intent RPC indicating where in a file an IO is about to write.
6351 * \param[in] inode file inode.
6352 * \param[in] ext write range with start offset of fille in bytes where
6353 * an IO is about to write, and exclusive end offset in
6356 * \retval 0 on success
6357 * \retval < 0 error code
6359 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
6360 struct lu_extent *ext)
6362 struct layout_intent intent = {
6364 .li_extent.e_start = ext->e_start,
6365 .li_extent.e_end = ext->e_end,
6370 rc = ll_layout_intent(inode, &intent);
6376 * This function send a restore request to the MDT
6378 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
6380 struct hsm_user_request *hur;
6384 len = sizeof(struct hsm_user_request) +
6385 sizeof(struct hsm_user_item);
6386 OBD_ALLOC(hur, len);
6390 hur->hur_request.hr_action = HUA_RESTORE;
6391 hur->hur_request.hr_archive_id = 0;
6392 hur->hur_request.hr_flags = 0;
6393 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
6394 sizeof(hur->hur_user_item[0].hui_fid));
6395 hur->hur_user_item[0].hui_extent.offset = offset;
6396 hur->hur_user_item[0].hui_extent.length = length;
6397 hur->hur_request.hr_itemcount = 1;
6398 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,