4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Author: Peter Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Andreas Dilger <adilger@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
39 #include <lustre_dlm.h>
40 #include <linux/pagemap.h>
41 #include <linux/file.h>
42 #include <linux/sched.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
45 #include <linux/falloc.h>
46 #include <linux/ktime.h>
48 #include <uapi/linux/lustre/lustre_ioctl.h>
49 #include <uapi/linux/llcrypt.h>
50 #include <lustre_swab.h>
52 #include "cl_object.h"
53 #include "llite_internal.h"
54 #include "vvp_internal.h"
57 struct inode *sp_inode;
62 __u64 pa_data_version;
68 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
70 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
73 static struct ll_file_data *ll_file_data_get(void)
75 struct ll_file_data *fd;
77 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
81 fd->fd_write_failed = false;
82 pcc_file_init(&fd->fd_pcc_file);
87 static void ll_file_data_put(struct ll_file_data *fd)
90 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
94 * Packs all the attributes into @op_data for the CLOSE rpc.
96 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
97 struct obd_client_handle *och)
101 ll_prep_md_op_data(op_data, inode, NULL, NULL,
102 0, 0, LUSTRE_OPC_ANY, NULL);
104 op_data->op_attr.ia_mode = inode->i_mode;
105 op_data->op_attr.ia_atime = inode->i_atime;
106 op_data->op_attr.ia_mtime = inode->i_mtime;
107 op_data->op_attr.ia_ctime = inode->i_ctime;
108 op_data->op_attr.ia_size = i_size_read(inode);
109 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
110 ATTR_MTIME | ATTR_MTIME_SET |
112 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
113 op_data->op_attr_blocks = inode->i_blocks;
114 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
115 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
116 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
117 op_data->op_open_handle = och->och_open_handle;
119 if (och->och_flags & FMODE_WRITE &&
120 test_and_clear_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags))
121 /* For HSM: if inode data has been modified, pack it so that
122 * MDT can set data dirty flag in the archive. */
123 op_data->op_bias |= MDS_DATA_MODIFIED;
129 * Perform a close, possibly with a bias.
130 * The meaning of "data" depends on the value of "bias".
132 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
133 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
136 static int ll_close_inode_openhandle(struct inode *inode,
137 struct obd_client_handle *och,
138 enum mds_op_bias bias, void *data)
140 struct obd_export *md_exp = ll_i2mdexp(inode);
141 const struct ll_inode_info *lli = ll_i2info(inode);
142 struct md_op_data *op_data;
143 struct ptlrpc_request *req = NULL;
147 if (class_exp2obd(md_exp) == NULL) {
148 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
149 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
153 OBD_ALLOC_PTR(op_data);
154 /* We leak openhandle and request here on error, but not much to be
155 * done in OOM case since app won't retry close on error either. */
157 GOTO(out, rc = -ENOMEM);
159 ll_prepare_close(inode, op_data, och);
161 case MDS_CLOSE_LAYOUT_MERGE:
162 /* merge blocks from the victim inode */
163 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
164 op_data->op_attr.ia_valid |= ATTR_SIZE;
165 op_data->op_xvalid |= OP_XVALID_BLOCKS;
167 case MDS_CLOSE_LAYOUT_SPLIT:
168 case MDS_CLOSE_LAYOUT_SWAP: {
169 struct split_param *sp = data;
171 LASSERT(data != NULL);
172 op_data->op_bias |= bias;
173 op_data->op_data_version = 0;
174 op_data->op_lease_handle = och->och_lease_handle;
175 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
176 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
177 op_data->op_mirror_id = sp->sp_mirror_id;
179 op_data->op_fid2 = *ll_inode2fid(data);
184 case MDS_CLOSE_RESYNC_DONE: {
185 struct ll_ioc_lease *ioc = data;
187 LASSERT(data != NULL);
188 op_data->op_attr_blocks +=
189 ioc->lil_count * op_data->op_attr_blocks;
190 op_data->op_attr.ia_valid |= ATTR_SIZE;
191 op_data->op_xvalid |= OP_XVALID_BLOCKS;
192 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
194 op_data->op_lease_handle = och->och_lease_handle;
195 op_data->op_data = &ioc->lil_ids[0];
196 op_data->op_data_size =
197 ioc->lil_count * sizeof(ioc->lil_ids[0]);
201 case MDS_PCC_ATTACH: {
202 struct pcc_param *param = data;
204 LASSERT(data != NULL);
205 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
206 op_data->op_archive_id = param->pa_archive_id;
207 op_data->op_data_version = param->pa_data_version;
208 op_data->op_lease_handle = och->och_lease_handle;
212 case MDS_HSM_RELEASE:
213 LASSERT(data != NULL);
214 op_data->op_bias |= MDS_HSM_RELEASE;
215 op_data->op_data_version = *(__u64 *)data;
216 op_data->op_lease_handle = och->och_lease_handle;
217 op_data->op_attr.ia_valid |= ATTR_SIZE;
218 op_data->op_xvalid |= OP_XVALID_BLOCKS;
222 LASSERT(data == NULL);
226 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
227 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
228 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
229 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
231 rc = md_close(md_exp, op_data, och->och_mod, &req);
232 if (rc != 0 && rc != -EINTR)
233 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
234 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
236 if (rc == 0 && op_data->op_bias & bias) {
237 struct mdt_body *body;
239 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
240 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
243 if (bias & MDS_PCC_ATTACH) {
244 struct pcc_param *param = data;
246 param->pa_layout_gen = body->mbo_layout_gen;
250 ll_finish_md_op_data(op_data);
254 md_clear_open_replay_data(md_exp, och);
255 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
258 ptlrpc_req_finished(req); /* This is close request */
262 int ll_md_real_close(struct inode *inode, fmode_t fmode)
264 struct ll_inode_info *lli = ll_i2info(inode);
265 struct obd_client_handle **och_p;
266 struct obd_client_handle *och;
271 if (fmode & FMODE_WRITE) {
272 och_p = &lli->lli_mds_write_och;
273 och_usecount = &lli->lli_open_fd_write_count;
274 } else if (fmode & FMODE_EXEC) {
275 och_p = &lli->lli_mds_exec_och;
276 och_usecount = &lli->lli_open_fd_exec_count;
278 LASSERT(fmode & FMODE_READ);
279 och_p = &lli->lli_mds_read_och;
280 och_usecount = &lli->lli_open_fd_read_count;
283 mutex_lock(&lli->lli_och_mutex);
284 if (*och_usecount > 0) {
285 /* There are still users of this handle, so skip
287 mutex_unlock(&lli->lli_och_mutex);
293 mutex_unlock(&lli->lli_och_mutex);
296 /* There might be a race and this handle may already
298 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
304 static int ll_md_close(struct inode *inode, struct file *file)
306 union ldlm_policy_data policy = {
307 .l_inodebits = { MDS_INODELOCK_OPEN },
309 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
310 struct ll_file_data *fd = file->private_data;
311 struct ll_inode_info *lli = ll_i2info(inode);
312 struct lustre_handle lockh;
313 enum ldlm_mode lockmode;
317 /* clear group lock, if present */
318 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
319 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
321 mutex_lock(&lli->lli_och_mutex);
322 if (fd->fd_lease_och != NULL) {
324 struct obd_client_handle *lease_och;
326 lease_och = fd->fd_lease_och;
327 fd->fd_lease_och = NULL;
328 mutex_unlock(&lli->lli_och_mutex);
330 /* Usually the lease is not released when the
331 * application crashed, we need to release here. */
332 rc = ll_lease_close(lease_och, inode, &lease_broken);
334 mutex_lock(&lli->lli_och_mutex);
336 CDEBUG_LIMIT(rc ? D_ERROR : D_INODE,
337 "Clean up lease "DFID" %d/%d\n",
338 PFID(&lli->lli_fid), rc, lease_broken);
341 if (fd->fd_och != NULL) {
342 struct obd_client_handle *och;
346 mutex_unlock(&lli->lli_och_mutex);
348 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
352 /* Let's see if we have good enough OPEN lock on the file and if
353 we can skip talking to MDS */
354 if (fd->fd_omode & FMODE_WRITE) {
356 LASSERT(lli->lli_open_fd_write_count);
357 lli->lli_open_fd_write_count--;
358 } else if (fd->fd_omode & FMODE_EXEC) {
360 LASSERT(lli->lli_open_fd_exec_count);
361 lli->lli_open_fd_exec_count--;
364 LASSERT(lli->lli_open_fd_read_count);
365 lli->lli_open_fd_read_count--;
367 mutex_unlock(&lli->lli_och_mutex);
369 /* LU-4398: do not cache write open lock if the file has exec bit */
370 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
371 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
372 LDLM_IBITS, &policy, lockmode, &lockh))
373 rc = ll_md_real_close(inode, fd->fd_omode);
376 file->private_data = NULL;
377 ll_file_data_put(fd);
382 /* While this returns an error code, fput() the caller does not, so we need
383 * to make every effort to clean up all of our state here. Also, applications
384 * rarely check close errors and even if an error is returned they will not
385 * re-try the close call.
387 int ll_file_release(struct inode *inode, struct file *file)
389 struct ll_file_data *fd;
390 struct ll_sb_info *sbi = ll_i2sbi(inode);
391 struct ll_inode_info *lli = ll_i2info(inode);
392 ktime_t kstart = ktime_get();
397 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
398 PFID(ll_inode2fid(inode)), inode);
400 fd = file->private_data;
403 /* The last ref on @file, maybe not the the owner pid of statahead,
404 * because parent and child process can share the same file handle. */
405 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
406 ll_deauthorize_statahead(inode, fd);
408 if (is_root_inode(inode)) {
409 file->private_data = NULL;
410 ll_file_data_put(fd);
414 pcc_file_release(inode, file);
416 if (!S_ISDIR(inode->i_mode)) {
417 if (lli->lli_clob != NULL)
418 lov_read_and_clear_async_rc(lli->lli_clob);
419 lli->lli_async_rc = 0;
422 lli->lli_close_fd_time = ktime_get();
424 rc = ll_md_close(inode, file);
426 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
427 libcfs_debug_dumplog();
430 if (!rc && !is_root_inode(inode))
431 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
432 ktime_us_delta(ktime_get(), kstart));
436 static inline int ll_dom_readpage(void *data, struct page *page)
438 struct niobuf_local *lnb = data;
442 struct inode *inode = page2inode(page);
444 kaddr = kmap_atomic(page);
445 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
446 if (lnb->lnb_len < PAGE_SIZE)
447 memset(kaddr + lnb->lnb_len, 0,
448 PAGE_SIZE - lnb->lnb_len);
449 flush_dcache_page(page);
450 SetPageUptodate(page);
451 kunmap_atomic(kaddr);
453 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
454 if (!llcrypt_has_encryption_key(inode))
455 CDEBUG(D_SEC, "no enc key for "DFID"\n",
456 PFID(ll_inode2fid(inode)));
458 unsigned int offs = 0;
460 while (offs < PAGE_SIZE) {
461 /* decrypt only if page is not empty */
462 if (memcmp(page_address(page) + offs,
463 page_address(ZERO_PAGE(0)),
464 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
467 rc = llcrypt_decrypt_pagecache_blocks(page,
468 LUSTRE_ENCRYPTION_UNIT_SIZE,
473 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
482 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
486 struct ll_inode_info *lli = ll_i2info(inode);
487 struct cl_object *obj = lli->lli_clob;
488 struct address_space *mapping = inode->i_mapping;
490 struct niobuf_remote *rnb;
491 struct mdt_body *body;
493 unsigned long index, start;
494 struct niobuf_local lnb;
503 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
507 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
508 if (rnb == NULL || rnb->rnb_len == 0)
511 /* LU-11595: Server may return whole file and that is OK always or
512 * it may return just file tail and its offset must be aligned with
513 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
514 * smaller then offset may be not aligned and that data is just ignored.
516 if (rnb->rnb_offset & ~PAGE_MASK)
519 /* Server returns whole file or just file tail if it fills in reply
520 * buffer, in both cases total size should be equal to the file size.
522 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
523 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
524 !(inode && IS_ENCRYPTED(inode))) {
525 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
526 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
527 rnb->rnb_len, body->mbo_dom_size);
531 env = cl_env_get(&refcheck);
534 io = vvp_env_thread_io(env);
536 io->ci_ignore_layout = 1;
537 rc = cl_io_init(env, io, CIT_MISC, obj);
541 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
542 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
544 data = (char *)rnb + sizeof(*rnb);
546 lnb.lnb_file_offset = rnb->rnb_offset;
547 start = lnb.lnb_file_offset >> PAGE_SHIFT;
549 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
550 lnb.lnb_page_offset = 0;
552 struct cl_page *page;
554 lnb.lnb_data = data + (index << PAGE_SHIFT);
555 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
556 if (lnb.lnb_len > PAGE_SIZE)
557 lnb.lnb_len = PAGE_SIZE;
559 vmpage = read_cache_page(mapping, index + start,
560 ll_dom_readpage, &lnb);
561 if (IS_ERR(vmpage)) {
562 CWARN("%s: cannot fill page %lu for "DFID
563 " with data: rc = %li\n",
564 ll_i2sbi(inode)->ll_fsname, index + start,
565 PFID(lu_object_fid(&obj->co_lu)),
570 if (vmpage->mapping == NULL) {
573 /* page was truncated */
576 /* attach VM page to CL page cache */
577 page = cl_page_find(env, obj, vmpage->index, vmpage,
580 ClearPageUptodate(vmpage);
585 cl_page_export(env, page, 1);
586 cl_page_put(env, page);
590 } while (rnb->rnb_len > (index << PAGE_SHIFT));
594 cl_env_put(env, &refcheck);
599 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
600 struct lookup_intent *itp)
602 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
603 struct dentry *parent = de->d_parent;
606 struct md_op_data *op_data;
607 struct ptlrpc_request *req = NULL;
611 LASSERT(parent != NULL);
612 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
614 /* if server supports open-by-fid, or file name is invalid, don't pack
615 * name in open request */
616 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
617 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
619 len = de->d_name.len;
620 name = kmalloc(len + 1, GFP_NOFS);
625 spin_lock(&de->d_lock);
626 if (len != de->d_name.len) {
627 spin_unlock(&de->d_lock);
631 memcpy(name, de->d_name.name, len);
633 spin_unlock(&de->d_lock);
635 if (!lu_name_is_valid_2(name, len)) {
641 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
642 name, len, 0, LUSTRE_OPC_OPEN, NULL);
643 if (IS_ERR(op_data)) {
645 RETURN(PTR_ERR(op_data));
647 op_data->op_data = lmm;
648 op_data->op_data_size = lmmsize;
650 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_OPEN_DELAY, cfs_fail_val);
652 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
653 &ll_md_blocking_ast, 0);
655 ll_finish_md_op_data(op_data);
657 /* reason for keep own exit path - don`t flood log
658 * with messages with -ESTALE errors.
660 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
661 it_open_error(DISP_OPEN_OPEN, itp))
663 ll_release_openhandle(de, itp);
667 if (it_disposition(itp, DISP_LOOKUP_NEG))
668 GOTO(out, rc = -ENOENT);
670 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
671 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
672 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
676 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
678 if (!rc && itp->it_lock_mode) {
681 /* If we got a lock back and it has a LOOKUP bit set,
682 * make sure the dentry is marked as valid so we can find it.
683 * We don't need to care about actual hashing since other bits
684 * of kernel will deal with that later.
686 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
687 if (bits & MDS_INODELOCK_LOOKUP) {
688 d_lustre_revalidate(de);
689 ll_update_dir_depth(parent->d_inode, de->d_inode);
692 /* if DoM bit returned along with LAYOUT bit then there
693 * can be read-on-open data returned.
695 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
696 ll_dom_finish_open(de->d_inode, req);
700 ptlrpc_req_finished(req);
701 ll_intent_drop_lock(itp);
703 /* We did open by fid, but by the time we got to the server, the object
704 * disappeared. This is possible if the object was unlinked, but it's
705 * also possible if the object was unlinked by a rename. In the case
706 * of an object renamed over our existing one, we can't fail this open.
707 * O_CREAT also goes through this path if we had an existing dentry,
708 * and it's obviously wrong to return ENOENT for O_CREAT.
710 * Instead let's return -ESTALE, and the VFS will retry the open with
711 * LOOKUP_REVAL, which we catch in ll_revalidate_dentry and fail to
712 * revalidate, causing a lookup. This causes extra lookups in the case
713 * where we had a dentry in cache but the file is being unlinked and we
714 * lose the race with unlink, but this should be very rare.
722 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
723 struct obd_client_handle *och)
725 struct mdt_body *body;
727 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
728 och->och_open_handle = body->mbo_open_handle;
729 och->och_fid = body->mbo_fid1;
730 och->och_lease_handle.cookie = it->it_lock_handle;
731 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
732 och->och_flags = it->it_flags;
734 return md_set_open_replay_data(md_exp, och, it);
737 static int ll_local_open(struct file *file, struct lookup_intent *it,
738 struct ll_file_data *fd, struct obd_client_handle *och)
740 struct inode *inode = file_inode(file);
743 LASSERT(!file->private_data);
750 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
755 file->private_data = fd;
756 ll_readahead_init(inode, &fd->fd_ras);
757 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
758 /* turn off the kernel's read-ahead */
759 file->f_ra.ra_pages = 0;
764 void ll_track_file_opens(struct inode *inode)
766 struct ll_inode_info *lli = ll_i2info(inode);
767 struct ll_sb_info *sbi = ll_i2sbi(inode);
769 /* do not skew results with delays from never-opened inodes */
770 if (ktime_to_ns(lli->lli_close_fd_time))
771 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
772 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
774 if (ktime_after(ktime_get(),
775 ktime_add_ms(lli->lli_close_fd_time,
776 sbi->ll_oc_max_ms))) {
777 lli->lli_open_fd_count = 1;
778 lli->lli_close_fd_time = ns_to_ktime(0);
780 lli->lli_open_fd_count++;
783 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
784 lli->lli_open_fd_count);
787 /* Open a file, and (for the very first open) create objects on the OSTs at
788 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
789 * creation or open until ll_lov_setstripe() ioctl is called.
791 * If we already have the stripe MD locally then we don't request it in
792 * md_open(), by passing a lmm_size = 0.
794 * It is up to the application to ensure no other processes open this file
795 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
796 * used. We might be able to avoid races of that sort by getting lli_open_sem
797 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
798 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
800 int ll_file_open(struct inode *inode, struct file *file)
802 struct ll_inode_info *lli = ll_i2info(inode);
803 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
804 .it_flags = file->f_flags };
805 struct obd_client_handle **och_p = NULL;
806 __u64 *och_usecount = NULL;
807 struct ll_file_data *fd;
808 ktime_t kstart = ktime_get();
812 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
813 PFID(ll_inode2fid(inode)), inode, file->f_flags);
815 it = file->private_data; /* XXX: compat macro */
816 file->private_data = NULL; /* prevent ll_local_open assertion */
818 if (S_ISREG(inode->i_mode)) {
819 rc = llcrypt_file_open(inode, file);
821 GOTO(out_nofiledata, rc);
824 fd = ll_file_data_get();
826 GOTO(out_nofiledata, rc = -ENOMEM);
829 if (S_ISDIR(inode->i_mode))
830 ll_authorize_statahead(inode, fd);
832 ll_track_file_opens(inode);
833 if (is_root_inode(inode)) {
834 file->private_data = fd;
838 if (!it || !it->it_disposition) {
839 /* Convert f_flags into access mode. We cannot use file->f_mode,
840 * because everything but O_ACCMODE mask was stripped from
842 if ((oit.it_flags + 1) & O_ACCMODE)
844 if (file->f_flags & O_TRUNC)
845 oit.it_flags |= FMODE_WRITE;
847 /* kernel only call f_op->open in dentry_open. filp_open calls
848 * dentry_open after call to open_namei that checks permissions.
849 * Only nfsd_open call dentry_open directly without checking
850 * permissions and because of that this code below is safe.
852 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
853 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
855 /* We do not want O_EXCL here, presumably we opened the file
856 * already? XXX - NFS implications? */
857 oit.it_flags &= ~O_EXCL;
859 /* bug20584, if "it_flags" contains O_CREAT, the file will be
860 * created if necessary, then "IT_CREAT" should be set to keep
861 * consistent with it */
862 if (oit.it_flags & O_CREAT)
863 oit.it_op |= IT_CREAT;
869 /* Let's see if we have file open on MDS already. */
870 if (it->it_flags & FMODE_WRITE) {
871 och_p = &lli->lli_mds_write_och;
872 och_usecount = &lli->lli_open_fd_write_count;
873 } else if (it->it_flags & FMODE_EXEC) {
874 och_p = &lli->lli_mds_exec_och;
875 och_usecount = &lli->lli_open_fd_exec_count;
877 och_p = &lli->lli_mds_read_och;
878 och_usecount = &lli->lli_open_fd_read_count;
881 mutex_lock(&lli->lli_och_mutex);
882 if (*och_p) { /* Open handle is present */
883 if (it_disposition(it, DISP_OPEN_OPEN)) {
884 /* Well, there's extra open request that we do not need,
885 * let's close it somehow. This will decref request. */
886 rc = it_open_error(DISP_OPEN_OPEN, it);
888 mutex_unlock(&lli->lli_och_mutex);
889 GOTO(out_openerr, rc);
892 ll_release_openhandle(file_dentry(file), it);
896 rc = ll_local_open(file, it, fd, NULL);
899 mutex_unlock(&lli->lli_och_mutex);
900 GOTO(out_openerr, rc);
903 LASSERT(*och_usecount == 0);
904 if (!it->it_disposition) {
905 struct dentry *dentry = file_dentry(file);
906 struct ll_sb_info *sbi = ll_i2sbi(inode);
907 struct ll_dentry_data *ldd;
909 /* We cannot just request lock handle now, new ELC code
910 * means that one of other OPEN locks for this file
911 * could be cancelled, and since blocking ast handler
912 * would attempt to grab och_mutex as well, that would
913 * result in a deadlock
915 mutex_unlock(&lli->lli_och_mutex);
917 * Normally called under two situations:
919 * 2. A race/condition on MDS resulting in no open
920 * handle to be returned from LOOKUP|OPEN request,
921 * for example if the target entry was a symlink.
923 * In NFS path we know there's pathologic behavior
924 * so we always enable open lock caching when coming
925 * from there. It's detected by setting a flag in
928 * After reaching number of opens of this inode
929 * we always ask for an open lock on it to handle
930 * bad userspace actors that open and close files
931 * in a loop for absolutely no good reason
934 ldd = ll_d2d(dentry);
935 if (filename_is_volatile(dentry->d_name.name,
938 /* There really is nothing here, but this
939 * make this more readable I think.
940 * We do not want openlock for volatile
941 * files under any circumstances
943 } else if (ldd && ldd->lld_nfs_dentry) {
944 /* NFS path. This also happens to catch
945 * open by fh files I guess
947 it->it_flags |= MDS_OPEN_LOCK;
948 /* clear the flag for future lookups */
949 ldd->lld_nfs_dentry = 0;
950 } else if (sbi->ll_oc_thrsh_count > 0) {
951 /* Take MDS_OPEN_LOCK with many opens */
952 if (lli->lli_open_fd_count >=
953 sbi->ll_oc_thrsh_count)
954 it->it_flags |= MDS_OPEN_LOCK;
956 /* If this is open after we just closed */
957 else if (ktime_before(ktime_get(),
958 ktime_add_ms(lli->lli_close_fd_time,
959 sbi->ll_oc_thrsh_ms)))
960 it->it_flags |= MDS_OPEN_LOCK;
964 * Always specify MDS_OPEN_BY_FID because we don't want
965 * to get file with different fid.
967 it->it_flags |= MDS_OPEN_BY_FID;
968 rc = ll_intent_file_open(dentry, NULL, 0, it);
970 GOTO(out_openerr, rc);
974 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
976 GOTO(out_och_free, rc = -ENOMEM);
980 /* md_intent_lock() didn't get a request ref if there was an
981 * open error, so don't do cleanup on the request here
983 /* XXX (green): Should not we bail out on any error here, not
984 * just open error? */
985 rc = it_open_error(DISP_OPEN_OPEN, it);
987 GOTO(out_och_free, rc);
989 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
990 "inode %p: disposition %x, status %d\n", inode,
991 it_disposition(it, ~0), it->it_status);
993 rc = ll_local_open(file, it, fd, *och_p);
995 GOTO(out_och_free, rc);
998 rc = pcc_file_open(inode, file);
1000 GOTO(out_och_free, rc);
1002 mutex_unlock(&lli->lli_och_mutex);
1006 /* Must do this outside lli_och_mutex lock to prevent deadlock where
1007 different kind of OPEN lock for this same inode gets cancelled
1008 by ldlm_cancel_lru */
1009 if (!S_ISREG(inode->i_mode))
1010 GOTO(out_och_free, rc);
1011 cl_lov_delay_create_clear(&file->f_flags);
1012 GOTO(out_och_free, rc);
1016 if (och_p && *och_p) {
1017 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1018 *och_p = NULL; /* OBD_FREE writes some magic there */
1021 mutex_unlock(&lli->lli_och_mutex);
1024 if (lli->lli_opendir_key == fd)
1025 ll_deauthorize_statahead(inode, fd);
1028 ll_file_data_put(fd);
1030 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1031 ktime_us_delta(ktime_get(), kstart));
1035 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1036 ptlrpc_req_finished(it->it_request);
1037 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1043 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1044 struct ldlm_lock_desc *desc, void *data, int flag)
1047 struct lustre_handle lockh;
1051 case LDLM_CB_BLOCKING:
1052 ldlm_lock2handle(lock, &lockh);
1053 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1055 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1059 case LDLM_CB_CANCELING:
1067 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1068 * and save it as fd->fd_och so as to force client to reopen the file even
1069 * if it has an open lock in cache already.
1071 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1072 struct lustre_handle *old_open_handle)
1074 struct ll_inode_info *lli = ll_i2info(inode);
1075 struct ll_file_data *fd = file->private_data;
1076 struct obd_client_handle **och_p;
1077 __u64 *och_usecount;
1081 /* Get the openhandle of the file */
1082 mutex_lock(&lli->lli_och_mutex);
1083 if (fd->fd_lease_och != NULL)
1084 GOTO(out_unlock, rc = -EBUSY);
1086 if (fd->fd_och == NULL) {
1087 if (file->f_mode & FMODE_WRITE) {
1088 LASSERT(lli->lli_mds_write_och != NULL);
1089 och_p = &lli->lli_mds_write_och;
1090 och_usecount = &lli->lli_open_fd_write_count;
1092 LASSERT(lli->lli_mds_read_och != NULL);
1093 och_p = &lli->lli_mds_read_och;
1094 och_usecount = &lli->lli_open_fd_read_count;
1097 if (*och_usecount > 1)
1098 GOTO(out_unlock, rc = -EBUSY);
1100 fd->fd_och = *och_p;
1105 *old_open_handle = fd->fd_och->och_open_handle;
1109 mutex_unlock(&lli->lli_och_mutex);
1114 * Release ownership on lli_mds_*_och when putting back a file lease.
1116 static int ll_lease_och_release(struct inode *inode, struct file *file)
1118 struct ll_inode_info *lli = ll_i2info(inode);
1119 struct ll_file_data *fd = file->private_data;
1120 struct obd_client_handle **och_p;
1121 struct obd_client_handle *old_och = NULL;
1122 __u64 *och_usecount;
1126 mutex_lock(&lli->lli_och_mutex);
1127 if (file->f_mode & FMODE_WRITE) {
1128 och_p = &lli->lli_mds_write_och;
1129 och_usecount = &lli->lli_open_fd_write_count;
1131 och_p = &lli->lli_mds_read_och;
1132 och_usecount = &lli->lli_open_fd_read_count;
1135 /* The file may have been open by another process (broken lease) so
1136 * *och_p is not NULL. In this case we should simply increase usecount
1139 if (*och_p != NULL) {
1140 old_och = fd->fd_och;
1143 *och_p = fd->fd_och;
1147 mutex_unlock(&lli->lli_och_mutex);
1149 if (old_och != NULL)
1150 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1156 * Acquire a lease and open the file.
1158 static struct obd_client_handle *
1159 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1162 struct lookup_intent it = { .it_op = IT_OPEN };
1163 struct ll_sb_info *sbi = ll_i2sbi(inode);
1164 struct md_op_data *op_data;
1165 struct ptlrpc_request *req = NULL;
1166 struct lustre_handle old_open_handle = { 0 };
1167 struct obd_client_handle *och = NULL;
1172 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1173 RETURN(ERR_PTR(-EINVAL));
1176 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1177 RETURN(ERR_PTR(-EPERM));
1179 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1181 RETURN(ERR_PTR(rc));
1186 RETURN(ERR_PTR(-ENOMEM));
1188 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1189 LUSTRE_OPC_ANY, NULL);
1190 if (IS_ERR(op_data))
1191 GOTO(out, rc = PTR_ERR(op_data));
1193 /* To tell the MDT this openhandle is from the same owner */
1194 op_data->op_open_handle = old_open_handle;
1196 it.it_flags = fmode | open_flags;
1197 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1198 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1199 &ll_md_blocking_lease_ast,
1200 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1201 * it can be cancelled which may mislead applications that the lease is
1203 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1204 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1205 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1206 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1207 ll_finish_md_op_data(op_data);
1208 ptlrpc_req_finished(req);
1210 GOTO(out_release_it, rc);
1212 if (it_disposition(&it, DISP_LOOKUP_NEG))
1213 GOTO(out_release_it, rc = -ENOENT);
1215 rc = it_open_error(DISP_OPEN_OPEN, &it);
1217 GOTO(out_release_it, rc);
1219 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1220 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1222 GOTO(out_release_it, rc);
1224 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1225 GOTO(out_close, rc = -EOPNOTSUPP);
1227 /* already get lease, handle lease lock */
1228 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1229 if (!it.it_lock_mode ||
1230 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1231 /* open lock must return for lease */
1232 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1233 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1235 GOTO(out_close, rc = -EPROTO);
1238 ll_intent_release(&it);
1242 /* Cancel open lock */
1243 if (it.it_lock_mode != 0) {
1244 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1246 it.it_lock_mode = 0;
1247 och->och_lease_handle.cookie = 0ULL;
1249 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1251 CERROR("%s: error closing file "DFID": %d\n",
1252 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1253 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1255 ll_intent_release(&it);
1259 RETURN(ERR_PTR(rc));
1263 * Check whether a layout swap can be done between two inodes.
1265 * \param[in] inode1 First inode to check
1266 * \param[in] inode2 Second inode to check
1268 * \retval 0 on success, layout swap can be performed between both inodes
1269 * \retval negative error code if requirements are not met
1271 static int ll_check_swap_layouts_validity(struct inode *inode1,
1272 struct inode *inode2)
1274 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1277 if (inode_permission(inode1, MAY_WRITE) ||
1278 inode_permission(inode2, MAY_WRITE))
1281 if (inode1->i_sb != inode2->i_sb)
1287 static int ll_swap_layouts_close(struct obd_client_handle *och,
1288 struct inode *inode, struct inode *inode2)
1290 const struct lu_fid *fid1 = ll_inode2fid(inode);
1291 const struct lu_fid *fid2;
1295 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1296 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1298 rc = ll_check_swap_layouts_validity(inode, inode2);
1300 GOTO(out_free_och, rc);
1302 /* We now know that inode2 is a lustre inode */
1303 fid2 = ll_inode2fid(inode2);
1305 rc = lu_fid_cmp(fid1, fid2);
1307 GOTO(out_free_och, rc = -EINVAL);
1309 /* Close the file and {swap,merge} layouts between inode & inode2.
1310 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1311 * because we still need it to pack l_remote_handle to MDT. */
1312 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1315 och = NULL; /* freed in ll_close_inode_openhandle() */
1325 * Release lease and close the file.
1326 * It will check if the lease has ever broken.
1328 static int ll_lease_close_intent(struct obd_client_handle *och,
1329 struct inode *inode,
1330 bool *lease_broken, enum mds_op_bias bias,
1333 struct ldlm_lock *lock;
1334 bool cancelled = true;
1338 lock = ldlm_handle2lock(&och->och_lease_handle);
1340 lock_res_and_lock(lock);
1341 cancelled = ldlm_is_cancel(lock);
1342 unlock_res_and_lock(lock);
1343 LDLM_LOCK_PUT(lock);
1346 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1347 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1349 if (lease_broken != NULL)
1350 *lease_broken = cancelled;
1352 if (!cancelled && !bias)
1353 ldlm_cli_cancel(&och->och_lease_handle, 0);
1355 if (cancelled) { /* no need to excute intent */
1360 rc = ll_close_inode_openhandle(inode, och, bias, data);
1364 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1367 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1371 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1373 static int ll_lease_file_resync(struct obd_client_handle *och,
1374 struct inode *inode, unsigned long arg)
1376 struct ll_sb_info *sbi = ll_i2sbi(inode);
1377 struct md_op_data *op_data;
1378 struct ll_ioc_lease_id ioc;
1379 __u64 data_version_unused;
1383 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1384 LUSTRE_OPC_ANY, NULL);
1385 if (IS_ERR(op_data))
1386 RETURN(PTR_ERR(op_data));
1388 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1392 /* before starting file resync, it's necessary to clean up page cache
1393 * in client memory, otherwise once the layout version is increased,
1394 * writing back cached data will be denied the OSTs. */
1395 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1399 op_data->op_lease_handle = och->och_lease_handle;
1400 op_data->op_mirror_id = ioc.lil_mirror_id;
1401 rc = md_file_resync(sbi->ll_md_exp, op_data);
1407 ll_finish_md_op_data(op_data);
1411 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1413 struct ll_inode_info *lli = ll_i2info(inode);
1414 struct cl_object *obj = lli->lli_clob;
1415 struct cl_attr *attr = vvp_env_thread_attr(env);
1423 ll_inode_size_lock(inode);
1425 /* Merge timestamps the most recently obtained from MDS with
1426 * timestamps obtained from OSTs.
1428 * Do not overwrite atime of inode because it may be refreshed
1429 * by file_accessed() function. If the read was served by cache
1430 * data, there is no RPC to be sent so that atime may not be
1431 * transferred to OSTs at all. MDT only updates atime at close time
1432 * if it's at least 'mdd.*.atime_diff' older.
1433 * All in all, the atime in Lustre does not strictly comply with
1434 * POSIX. Solving this problem needs to send an RPC to MDT for each
1435 * read, this will hurt performance.
1437 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1438 inode->i_atime.tv_sec < lli->lli_atime)
1439 inode->i_atime.tv_sec = lli->lli_atime;
1441 inode->i_mtime.tv_sec = lli->lli_mtime;
1442 inode->i_ctime.tv_sec = lli->lli_ctime;
1444 mtime = inode->i_mtime.tv_sec;
1445 atime = inode->i_atime.tv_sec;
1446 ctime = inode->i_ctime.tv_sec;
1448 cl_object_attr_lock(obj);
1449 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1452 rc = cl_object_attr_get(env, obj, attr);
1453 cl_object_attr_unlock(obj);
1456 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1458 if (atime < attr->cat_atime)
1459 atime = attr->cat_atime;
1461 if (ctime < attr->cat_ctime)
1462 ctime = attr->cat_ctime;
1464 if (mtime < attr->cat_mtime)
1465 mtime = attr->cat_mtime;
1467 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1468 PFID(&lli->lli_fid), attr->cat_size);
1470 i_size_write(inode, attr->cat_size);
1471 inode->i_blocks = attr->cat_blocks;
1473 inode->i_mtime.tv_sec = mtime;
1474 inode->i_atime.tv_sec = atime;
1475 inode->i_ctime.tv_sec = ctime;
1478 ll_inode_size_unlock(inode);
1484 * Set designated mirror for I/O.
1486 * So far only read, write, and truncated can support to issue I/O to
1487 * designated mirror.
1489 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1491 struct ll_file_data *fd = file->private_data;
1493 /* clear layout version for generic(non-resync) I/O in case it carries
1494 * stale layout version due to I/O restart */
1495 io->ci_layout_version = 0;
1497 /* FLR: disable non-delay for designated mirror I/O because obviously
1498 * only one mirror is available */
1499 if (fd->fd_designated_mirror > 0) {
1501 io->ci_designated_mirror = fd->fd_designated_mirror;
1502 io->ci_layout_version = fd->fd_layout_version;
1505 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1506 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1509 static bool file_is_noatime(const struct file *file)
1511 const struct vfsmount *mnt = file->f_path.mnt;
1512 const struct inode *inode = file_inode((struct file *)file);
1514 /* Adapted from file_accessed() and touch_atime().*/
1515 if (file->f_flags & O_NOATIME)
1518 if (inode->i_flags & S_NOATIME)
1521 if (IS_NOATIME(inode))
1524 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1527 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1530 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1536 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1537 struct vvp_io_args *args)
1539 struct inode *inode = file_inode(file);
1540 struct ll_file_data *fd = file->private_data;
1542 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1543 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1545 if (iot == CIT_WRITE) {
1546 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1547 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1548 file->f_flags & O_DIRECT ||
1550 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1551 io->u.ci_wr.wr_sync |= !!(args &&
1552 (args->u.normal.via_iocb->ki_flags &
1557 io->ci_obj = ll_i2info(inode)->lli_clob;
1558 io->ci_lockreq = CILR_MAYBE;
1559 if (ll_file_nolock(file)) {
1560 io->ci_lockreq = CILR_NEVER;
1561 io->ci_no_srvlock = 1;
1562 } else if (file->f_flags & O_APPEND) {
1563 io->ci_lockreq = CILR_MANDATORY;
1565 io->ci_noatime = file_is_noatime(file);
1566 io->ci_async_readahead = false;
1568 /* FLR: only use non-delay I/O for read as there is only one
1569 * avaliable mirror for write. */
1570 io->ci_ndelay = !(iot == CIT_WRITE);
1572 ll_io_set_mirror(io, file);
1575 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1578 struct ll_inode_info *lli = ll_i2info(inode);
1579 struct ll_sb_info *sbi = ll_i2sbi(inode);
1580 enum obd_heat_type sample_type;
1581 enum obd_heat_type iobyte_type;
1582 __u64 now = ktime_get_real_seconds();
1584 if (!ll_sbi_has_file_heat(sbi) ||
1585 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1588 if (iot == CIT_READ) {
1589 sample_type = OBD_HEAT_READSAMPLE;
1590 iobyte_type = OBD_HEAT_READBYTE;
1591 } else if (iot == CIT_WRITE) {
1592 sample_type = OBD_HEAT_WRITESAMPLE;
1593 iobyte_type = OBD_HEAT_WRITEBYTE;
1598 spin_lock(&lli->lli_heat_lock);
1599 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1600 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1601 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1602 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1603 spin_unlock(&lli->lli_heat_lock);
1607 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1608 struct file *file, enum cl_io_type iot,
1609 loff_t *ppos, size_t count)
1611 struct vvp_io *vio = vvp_env_io(env);
1612 struct inode *inode = file_inode(file);
1613 struct ll_inode_info *lli = ll_i2info(inode);
1614 struct ll_sb_info *sbi = ll_i2sbi(inode);
1615 struct ll_file_data *fd = file->private_data;
1616 struct range_lock range;
1617 bool range_locked = false;
1622 unsigned int retried = 0, dio_lock = 0;
1623 bool is_aio = false;
1624 bool is_parallel_dio = false;
1625 struct cl_dio_aio *ci_aio = NULL;
1627 bool partial_io = false;
1628 size_t max_io_pages, max_cached_pages;
1632 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1633 file_dentry(file)->d_name.name,
1634 iot == CIT_READ ? "read" : "write", *ppos, count);
1636 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1637 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1638 if (max_io_pages > (max_cached_pages >> 2))
1639 max_io_pages = max_cached_pages >> 2;
1641 io = vvp_env_thread_io(env);
1642 if (file->f_flags & O_DIRECT) {
1643 if (!is_sync_kiocb(args->u.normal.via_iocb))
1646 /* the kernel does not support AIO on pipes, and parallel DIO
1647 * uses part of the AIO path, so we must not do parallel dio
1650 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1653 if (!ll_sbi_has_parallel_dio(sbi))
1654 is_parallel_dio = false;
1656 ci_aio = cl_aio_alloc(args->u.normal.via_iocb,
1657 ll_i2info(inode)->lli_clob);
1659 GOTO(out, rc = -ENOMEM);
1664 * IO block size need be aware of cached page limit, otherwise
1665 * if we have small max_cached_mb but large block IO issued, io
1666 * could not be finished and blocked whole client.
1668 if (file->f_flags & O_DIRECT)
1671 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1672 partial_io = per_bytes < count;
1673 io = vvp_env_thread_io(env);
1674 ll_io_init(io, file, iot, args);
1675 io->ci_aio = ci_aio;
1676 io->ci_dio_lock = dio_lock;
1677 io->ci_ndelay_tried = retried;
1678 io->ci_parallel_dio = is_parallel_dio;
1680 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1681 if (file->f_flags & O_APPEND)
1682 range_lock_init(&range, 0, LUSTRE_EOF);
1684 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1686 vio->vui_fd = file->private_data;
1687 vio->vui_iter = args->u.normal.via_iter;
1688 vio->vui_iocb = args->u.normal.via_iocb;
1689 /* Direct IO reads must also take range lock,
1690 * or multiple reads will try to work on the same pages
1691 * See LU-6227 for details.
1693 if (((iot == CIT_WRITE) ||
1694 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1695 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1696 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1698 rc = range_lock(&lli->lli_write_tree, &range);
1702 range_locked = true;
1705 ll_cl_add(inode, env, io, LCC_RW);
1706 rc = cl_io_loop(env, io);
1707 ll_cl_remove(inode, env);
1709 if (range_locked && !is_parallel_dio) {
1710 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1712 range_unlock(&lli->lli_write_tree, &range);
1713 range_locked = false;
1716 /* cl_io_rw_init() handled IO */
1720 /* N/B: parallel DIO may be disabled during i/o submission;
1721 * if that occurs, async RPCs are resolved before we get here, and this
1722 * wait call completes immediately.
1724 if (is_parallel_dio) {
1725 struct cl_sync_io *anchor = &io->ci_aio->cda_sync;
1727 /* for dio, EIOCBQUEUED is an implementation detail,
1728 * and we don't return it to userspace
1730 if (rc == -EIOCBQUEUED)
1733 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1738 range_unlock(&lli->lli_write_tree, &range);
1739 range_locked = false;
1744 * In order to move forward AIO, ci_nob was increased,
1745 * but that doesn't mean io have been finished, it just
1746 * means io have been submited, we will always return
1747 * EIOCBQUEUED to the caller, So we could only return
1748 * number of bytes in non-AIO case.
1750 if (io->ci_nob > 0) {
1753 result += io->ci_nob;
1754 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1759 count -= io->ci_nob;
1761 /* prepare IO restart */
1763 args->u.normal.via_iter = vio->vui_iter;
1767 * Reexpand iov count because it was zero
1770 iov_iter_reexpand(vio->vui_iter, count);
1771 if (per_bytes == io->ci_nob)
1772 io->ci_need_restart = 1;
1776 cl_io_fini(env, io);
1779 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1780 file->f_path.dentry->d_name.name,
1781 iot, rc, result, io->ci_need_restart);
1783 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1784 count > 0 && io->ci_need_restart) {
1786 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1787 file_dentry(file)->d_name.name,
1788 iot == CIT_READ ? "read" : "write",
1789 *ppos, count, result, rc);
1790 /* preserve the tried count for FLR */
1791 retried = io->ci_ndelay_tried;
1792 dio_lock = io->ci_dio_lock;
1798 * VFS will call aio_complete() if no -EIOCBQUEUED
1799 * is returned for AIO, so we can not call aio_complete()
1802 if (rc != -EIOCBQUEUED)
1803 io->ci_aio->cda_no_aio_complete = 1;
1805 * Drop one extra reference so that end_io() could be
1806 * called for this IO context, we could call it after
1807 * we make sure all AIO requests have been proceed.
1809 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1810 rc == -EIOCBQUEUED ? 0 : rc);
1812 cl_aio_free(env, io->ci_aio);
1817 if (iot == CIT_READ) {
1819 ll_stats_ops_tally(ll_i2sbi(inode),
1820 LPROC_LL_READ_BYTES, result);
1821 } else if (iot == CIT_WRITE) {
1823 ll_stats_ops_tally(ll_i2sbi(inode),
1824 LPROC_LL_WRITE_BYTES, result);
1825 fd->fd_write_failed = false;
1826 } else if (result == 0 && rc == 0) {
1829 fd->fd_write_failed = true;
1831 fd->fd_write_failed = false;
1832 } else if (rc != -ERESTARTSYS) {
1833 fd->fd_write_failed = true;
1837 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1839 ll_heat_add(inode, iot, result);
1841 RETURN(result > 0 ? result : rc);
1845 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1846 * especially for small I/O.
1848 * To serve a read request, CLIO has to create and initialize a cl_io and
1849 * then request DLM lock. This has turned out to have siginificant overhead
1850 * and affects the performance of small I/O dramatically.
1852 * It's not necessary to create a cl_io for each I/O. Under the help of read
1853 * ahead, most of the pages being read are already in memory cache and we can
1854 * read those pages directly because if the pages exist, the corresponding DLM
1855 * lock must exist so that page content must be valid.
1857 * In fast read implementation, the llite speculatively finds and reads pages
1858 * in memory cache. There are three scenarios for fast read:
1859 * - If the page exists and is uptodate, kernel VM will provide the data and
1860 * CLIO won't be intervened;
1861 * - If the page was brought into memory by read ahead, it will be exported
1862 * and read ahead parameters will be updated;
1863 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1864 * it will go back and invoke normal read, i.e., a cl_io will be created
1865 * and DLM lock will be requested.
1867 * POSIX compliance: posix standard states that read is intended to be atomic.
1868 * Lustre read implementation is in line with Linux kernel read implementation
1869 * and neither of them complies with POSIX standard in this matter. Fast read
1870 * doesn't make the situation worse on single node but it may interleave write
1871 * results from multiple nodes due to short read handling in ll_file_aio_read().
1873 * \param env - lu_env
1874 * \param iocb - kiocb from kernel
1875 * \param iter - user space buffers where the data will be copied
1877 * \retval - number of bytes have been read, or error code if error occurred.
1880 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1884 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1887 /* NB: we can't do direct IO for fast read because it will need a lock
1888 * to make IO engine happy. */
1889 if (iocb->ki_filp->f_flags & O_DIRECT)
1892 result = generic_file_read_iter(iocb, iter);
1894 /* If the first page is not in cache, generic_file_aio_read() will be
1895 * returned with -ENODATA.
1896 * See corresponding code in ll_readpage(). */
1897 if (result == -ENODATA)
1901 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1902 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1903 LPROC_LL_READ_BYTES, result);
1910 * Read from a file (through the page cache).
1912 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1915 struct vvp_io_args *args;
1916 struct file *file = iocb->ki_filp;
1920 ktime_t kstart = ktime_get();
1923 if (!iov_iter_count(to))
1927 * Currently when PCC read failed, we do not fall back to the
1928 * normal read path, just return the error.
1929 * The resaon is that: for RW-PCC, the file data may be modified
1930 * in the PCC and inconsistent with the data on OSTs (or file
1931 * data has been removed from the Lustre file system), at this
1932 * time, fallback to the normal read path may read the wrong
1934 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1935 * path: read data from data copy on OSTs.
1937 result = pcc_file_read_iter(iocb, to, &cached);
1941 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1943 result = ll_do_fast_read(iocb, to);
1944 if (result < 0 || iov_iter_count(to) == 0)
1947 env = cl_env_get(&refcheck);
1949 return PTR_ERR(env);
1951 args = ll_env_args(env);
1952 args->u.normal.via_iter = to;
1953 args->u.normal.via_iocb = iocb;
1955 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1956 &iocb->ki_pos, iov_iter_count(to));
1959 else if (result == 0)
1962 cl_env_put(env, &refcheck);
1965 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1966 file->private_data, iocb->ki_pos, result,
1968 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1969 ktime_us_delta(ktime_get(), kstart));
1976 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1977 * If a page is already in the page cache and dirty (and some other things -
1978 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1979 * write to it without doing a full I/O, because Lustre already knows about it
1980 * and will write it out. This saves a lot of processing time.
1982 * All writes here are within one page, so exclusion is handled by the page
1983 * lock on the vm page. We do not do tiny writes for writes which touch
1984 * multiple pages because it's very unlikely multiple sequential pages are
1985 * are already dirty.
1987 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1988 * and are unlikely to be to already dirty pages.
1990 * Attribute updates are important here, we do them in ll_tiny_write_end.
1992 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1994 ssize_t count = iov_iter_count(iter);
1995 struct file *file = iocb->ki_filp;
1996 struct inode *inode = file_inode(file);
1997 bool lock_inode = !IS_NOSEC(inode);
2002 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
2003 * of function for why.
2005 if (count >= PAGE_SIZE ||
2006 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
2009 if (unlikely(lock_inode))
2011 result = __generic_file_write_iter(iocb, iter);
2013 if (unlikely(lock_inode))
2014 inode_unlock(inode);
2016 /* If the page is not already dirty, ll_tiny_write_begin returns
2017 * -ENODATA. We continue on to normal write.
2019 if (result == -ENODATA)
2023 ll_heat_add(inode, CIT_WRITE, result);
2024 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2027 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2033 * Write to a file (through the page cache).
2035 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2037 struct vvp_io_args *args;
2039 ssize_t rc_tiny = 0, rc_normal;
2040 struct file *file = iocb->ki_filp;
2043 ktime_t kstart = ktime_get();
2048 if (!iov_iter_count(from))
2049 GOTO(out, rc_normal = 0);
2052 * When PCC write failed, we usually do not fall back to the normal
2053 * write path, just return the error. But there is a special case when
2054 * returned error code is -ENOSPC due to running out of space on PCC HSM
2055 * bakcend. At this time, it will fall back to normal I/O path and
2056 * retry the I/O. As the file is in HSM released state, it will restore
2057 * the file data to OSTs first and redo the write again. And the
2058 * restore process will revoke the layout lock and detach the file
2059 * from PCC cache automatically.
2061 result = pcc_file_write_iter(iocb, from, &cached);
2062 if (cached && result != -ENOSPC && result != -EDQUOT)
2063 GOTO(out, rc_normal = result);
2065 /* NB: we can't do direct IO for tiny writes because they use the page
2066 * cache, we can't do sync writes because tiny writes can't flush
2067 * pages, and we can't do append writes because we can't guarantee the
2068 * required DLM locks are held to protect file size.
2070 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2071 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2072 rc_tiny = ll_do_tiny_write(iocb, from);
2074 /* In case of error, go on and try normal write - Only stop if tiny
2075 * write completed I/O.
2077 if (iov_iter_count(from) == 0)
2078 GOTO(out, rc_normal = rc_tiny);
2080 env = cl_env_get(&refcheck);
2082 return PTR_ERR(env);
2084 args = ll_env_args(env);
2085 args->u.normal.via_iter = from;
2086 args->u.normal.via_iocb = iocb;
2088 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2089 &iocb->ki_pos, iov_iter_count(from));
2091 /* On success, combine bytes written. */
2092 if (rc_tiny >= 0 && rc_normal > 0)
2093 rc_normal += rc_tiny;
2094 /* On error, only return error from normal write if tiny write did not
2095 * write any bytes. Otherwise return bytes written by tiny write.
2097 else if (rc_tiny > 0)
2098 rc_normal = rc_tiny;
2100 cl_env_put(env, &refcheck);
2102 if (rc_normal > 0) {
2103 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2104 file->private_data, iocb->ki_pos,
2106 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2107 ktime_us_delta(ktime_get(), kstart));
2113 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2115 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2117 static int ll_file_get_iov_count(const struct iovec *iov,
2118 unsigned long *nr_segs, size_t *count,
2124 for (seg = 0; seg < *nr_segs; seg++) {
2125 const struct iovec *iv = &iov[seg];
2128 * If any segment has a negative length, or the cumulative
2129 * length ever wraps negative then return -EINVAL.
2132 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2134 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2139 cnt -= iv->iov_len; /* This segment is no good */
2146 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2147 unsigned long nr_segs, loff_t pos)
2154 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2161 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2162 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2163 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2164 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2165 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2167 result = ll_file_read_iter(iocb, &to);
2172 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2175 struct iovec iov = { .iov_base = buf, .iov_len = count };
2184 init_sync_kiocb(&kiocb, file);
2185 kiocb.ki_pos = *ppos;
2186 #ifdef HAVE_KIOCB_KI_LEFT
2187 kiocb.ki_left = count;
2188 #elif defined(HAVE_KI_NBYTES)
2189 kiocb.i_nbytes = count;
2192 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2193 *ppos = kiocb.ki_pos;
2199 * Write to a file (through the page cache).
2202 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2203 unsigned long nr_segs, loff_t pos)
2205 struct iov_iter from;
2210 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2217 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2218 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2219 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2220 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2221 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2223 result = ll_file_write_iter(iocb, &from);
2228 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2229 size_t count, loff_t *ppos)
2231 struct iovec iov = { .iov_base = (void __user *)buf,
2241 init_sync_kiocb(&kiocb, file);
2242 kiocb.ki_pos = *ppos;
2243 #ifdef HAVE_KIOCB_KI_LEFT
2244 kiocb.ki_left = count;
2245 #elif defined(HAVE_KI_NBYTES)
2246 kiocb.ki_nbytes = count;
2249 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2250 *ppos = kiocb.ki_pos;
2254 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2256 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2257 __u64 flags, struct lov_user_md *lum, int lum_size)
2259 struct lookup_intent oit = {
2261 .it_flags = flags | MDS_OPEN_BY_FID,
2266 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2267 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2268 /* this code will only exist for big-endian systems */
2269 lustre_swab_lov_user_md(lum, 0);
2272 ll_inode_size_lock(inode);
2273 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2275 GOTO(out_unlock, rc);
2277 ll_release_openhandle(dentry, &oit);
2280 ll_inode_size_unlock(inode);
2281 ll_intent_release(&oit);
2286 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2287 struct lov_mds_md **lmmp, int *lmm_size,
2288 struct ptlrpc_request **request)
2290 struct ll_sb_info *sbi = ll_i2sbi(inode);
2291 struct mdt_body *body;
2292 struct lov_mds_md *lmm = NULL;
2293 struct ptlrpc_request *req = NULL;
2294 struct md_op_data *op_data;
2299 rc = ll_get_default_mdsize(sbi, &lmmsize);
2303 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2304 strlen(filename), lmmsize,
2305 LUSTRE_OPC_ANY, NULL);
2306 if (IS_ERR(op_data))
2307 RETURN(PTR_ERR(op_data));
2309 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2310 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2311 ll_finish_md_op_data(op_data);
2313 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
2318 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2319 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2321 lmmsize = body->mbo_eadatasize;
2323 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2325 GOTO(out, rc = -ENODATA);
2327 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2328 LASSERT(lmm != NULL);
2330 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2331 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2332 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2333 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2334 GOTO(out, rc = -EPROTO);
2337 * This is coming from the MDS, so is probably in
2338 * little endian. We convert it to host endian before
2339 * passing it to userspace.
2341 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2342 int stripe_count = 0;
2344 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2345 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2346 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2347 if (le32_to_cpu(lmm->lmm_pattern) &
2348 LOV_PATTERN_F_RELEASED)
2350 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2352 /* if function called for directory - we should
2353 * avoid swab not existent lsm objects
2355 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2356 S_ISREG(body->mbo_mode))
2357 lustre_swab_lov_user_md_objects(
2358 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2360 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2361 S_ISREG(body->mbo_mode))
2362 lustre_swab_lov_user_md_objects(
2363 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2365 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2366 lustre_swab_lov_comp_md_v1(
2367 (struct lov_comp_md_v1 *)lmm);
2371 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2372 struct lov_comp_md_v1 *comp_v1 = NULL;
2373 struct lov_comp_md_entry_v1 *ent;
2374 struct lov_user_md_v1 *v1;
2378 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2379 /* Dump the striping information */
2380 for (; i < comp_v1->lcm_entry_count; i++) {
2381 ent = &comp_v1->lcm_entries[i];
2382 off = ent->lcme_offset;
2383 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2385 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2386 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2390 * Return valid stripe_count and stripe_size instead of 0 for
2391 * DoM files to avoid divide-by-zero for older userspace that
2392 * calls this ioctl, e.g. lustre ADIO driver.
2394 if (lmm->lmm_stripe_count == 0)
2395 lmm->lmm_stripe_count = 1;
2396 if (lmm->lmm_stripe_size == 0) {
2397 /* Since the first component of the file data is placed
2398 * on the MDT for faster access, the stripe_size of the
2399 * second one is always that applications which are
2402 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2403 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2405 i = comp_v1->lcm_entry_count > 1 ?
2406 comp_v1->lcm_entry_count - 1 : 0;
2407 ent = &comp_v1->lcm_entries[i];
2408 off = ent->lcme_offset;
2409 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2410 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2415 *lmm_size = lmmsize;
2420 static int ll_lov_setea(struct inode *inode, struct file *file,
2423 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2424 struct lov_user_md *lump;
2425 int lum_size = sizeof(struct lov_user_md) +
2426 sizeof(struct lov_user_ost_data);
2430 if (!capable(CAP_SYS_ADMIN))
2433 OBD_ALLOC_LARGE(lump, lum_size);
2437 if (copy_from_user(lump, arg, lum_size))
2438 GOTO(out_lump, rc = -EFAULT);
2440 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2442 cl_lov_delay_create_clear(&file->f_flags);
2445 OBD_FREE_LARGE(lump, lum_size);
2449 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2456 env = cl_env_get(&refcheck);
2458 RETURN(PTR_ERR(env));
2460 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2461 cl_env_put(env, &refcheck);
2465 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2468 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2469 struct lov_user_md *klum;
2471 __u64 flags = FMODE_WRITE;
2474 rc = ll_copy_user_md(lum, &klum);
2479 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2484 rc = put_user(0, &lum->lmm_stripe_count);
2488 rc = ll_layout_refresh(inode, &gen);
2492 rc = ll_file_getstripe(inode, arg, lum_size);
2493 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2494 ll_i2info(inode)->lli_clob) {
2495 struct iattr attr = { 0 };
2497 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2498 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2501 cl_lov_delay_create_clear(&file->f_flags);
2504 OBD_FREE_LARGE(klum, lum_size);
2510 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2512 struct ll_inode_info *lli = ll_i2info(inode);
2513 struct cl_object *obj = lli->lli_clob;
2514 struct ll_file_data *fd = file->private_data;
2515 struct ll_grouplock grouplock;
2520 CWARN("group id for group lock must not be 0\n");
2524 if (ll_file_nolock(file))
2525 RETURN(-EOPNOTSUPP);
2527 if (file->f_flags & O_NONBLOCK) {
2528 if (!mutex_trylock(&lli->lli_group_mutex))
2531 mutex_lock(&lli->lli_group_mutex);
2533 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2534 CWARN("group lock already existed with gid %lu\n",
2535 fd->fd_grouplock.lg_gid);
2536 GOTO(out, rc = -EINVAL);
2538 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2539 if (file->f_flags & O_NONBLOCK)
2540 GOTO(out, rc = -EAGAIN);
2541 mutex_unlock(&lli->lli_group_mutex);
2542 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2543 GOTO(retry, rc = 0);
2545 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2548 * XXX: group lock needs to protect all OST objects while PFL
2549 * can add new OST objects during the IO, so we'd instantiate
2550 * all OST objects before getting its group lock.
2555 struct cl_layout cl = {
2556 .cl_is_composite = false,
2558 struct lu_extent ext = {
2560 .e_end = OBD_OBJECT_EOF,
2563 env = cl_env_get(&refcheck);
2565 GOTO(out, rc = PTR_ERR(env));
2567 rc = cl_object_layout_get(env, obj, &cl);
2568 if (rc >= 0 && cl.cl_is_composite)
2569 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2572 cl_env_put(env, &refcheck);
2577 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2578 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2583 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2584 fd->fd_grouplock = grouplock;
2585 if (lli->lli_group_users == 0)
2586 lli->lli_group_gid = grouplock.lg_gid;
2587 lli->lli_group_users++;
2589 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2591 mutex_unlock(&lli->lli_group_mutex);
2596 static int ll_put_grouplock(struct inode *inode, struct file *file,
2599 struct ll_inode_info *lli = ll_i2info(inode);
2600 struct ll_file_data *fd = file->private_data;
2601 struct ll_grouplock grouplock;
2605 mutex_lock(&lli->lli_group_mutex);
2606 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2607 CWARN("no group lock held\n");
2608 GOTO(out, rc = -EINVAL);
2611 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2613 if (fd->fd_grouplock.lg_gid != arg) {
2614 CWARN("group lock %lu doesn't match current id %lu\n",
2615 arg, fd->fd_grouplock.lg_gid);
2616 GOTO(out, rc = -EINVAL);
2619 grouplock = fd->fd_grouplock;
2620 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2621 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2623 cl_put_grouplock(&grouplock);
2625 lli->lli_group_users--;
2626 if (lli->lli_group_users == 0) {
2627 lli->lli_group_gid = 0;
2628 wake_up_var(&lli->lli_group_users);
2630 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2633 mutex_unlock(&lli->lli_group_mutex);
2639 * Close inode open handle
2641 * \param dentry [in] dentry which contains the inode
2642 * \param it [in,out] intent which contains open info and result
2645 * \retval <0 failure
2647 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2649 struct inode *inode = dentry->d_inode;
2650 struct obd_client_handle *och;
2656 /* Root ? Do nothing. */
2657 if (is_root_inode(inode))
2660 /* No open handle to close? Move away */
2661 if (!it_disposition(it, DISP_OPEN_OPEN))
2664 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2666 OBD_ALLOC(och, sizeof(*och));
2668 GOTO(out, rc = -ENOMEM);
2670 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2674 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2676 /* this one is in place of ll_file_open */
2677 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2678 ptlrpc_req_finished(it->it_request);
2679 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2685 * Get size for inode for which FIEMAP mapping is requested.
2686 * Make the FIEMAP get_info call and returns the result.
2687 * \param fiemap kernel buffer to hold extens
2688 * \param num_bytes kernel buffer size
2690 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2696 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2699 /* Checks for fiemap flags */
2700 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2701 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2705 /* Check for FIEMAP_FLAG_SYNC */
2706 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2707 rc = filemap_fdatawrite(inode->i_mapping);
2712 env = cl_env_get(&refcheck);
2714 RETURN(PTR_ERR(env));
2716 if (i_size_read(inode) == 0) {
2717 rc = ll_glimpse_size(inode);
2722 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2723 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2724 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2726 /* If filesize is 0, then there would be no objects for mapping */
2727 if (fmkey.lfik_oa.o_size == 0) {
2728 fiemap->fm_mapped_extents = 0;
2732 fmkey.lfik_fiemap = *fiemap;
2734 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2735 &fmkey, fiemap, &num_bytes);
2737 cl_env_put(env, &refcheck);
2741 int ll_fid2path(struct inode *inode, void __user *arg)
2743 struct obd_export *exp = ll_i2mdexp(inode);
2744 const struct getinfo_fid2path __user *gfin = arg;
2746 struct getinfo_fid2path *gfout;
2752 if (!capable(CAP_DAC_READ_SEARCH) &&
2753 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
2756 /* Only need to get the buflen */
2757 if (get_user(pathlen, &gfin->gf_pathlen))
2760 if (pathlen > PATH_MAX)
2763 outsize = sizeof(*gfout) + pathlen;
2764 OBD_ALLOC(gfout, outsize);
2768 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2769 GOTO(gf_free, rc = -EFAULT);
2770 /* append root FID after gfout to let MDT know the root FID so that it
2771 * can lookup the correct path, this is mainly for fileset.
2772 * old server without fileset mount support will ignore this. */
2773 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2775 /* Call mdc_iocontrol */
2776 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2780 if (copy_to_user(arg, gfout, outsize))
2784 OBD_FREE(gfout, outsize);
2789 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2791 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2799 ioc->idv_version = 0;
2800 ioc->idv_layout_version = UINT_MAX;
2802 /* If no file object initialized, we consider its version is 0. */
2806 env = cl_env_get(&refcheck);
2808 RETURN(PTR_ERR(env));
2810 io = vvp_env_thread_io(env);
2812 io->u.ci_data_version.dv_data_version = 0;
2813 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2814 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2817 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2818 result = cl_io_loop(env, io);
2820 result = io->ci_result;
2822 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2823 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2825 cl_io_fini(env, io);
2827 if (unlikely(io->ci_need_restart))
2830 cl_env_put(env, &refcheck);
2836 * Read the data_version for inode.
2838 * This value is computed using stripe object version on OST.
2839 * Version is computed using server side locking.
2841 * @param flags if do sync on the OST side;
2843 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2844 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2846 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2848 struct ioc_data_version ioc = { .idv_flags = flags };
2851 rc = ll_ioc_data_version(inode, &ioc);
2853 *data_version = ioc.idv_version;
2859 * Trigger a HSM release request for the provided inode.
2861 int ll_hsm_release(struct inode *inode)
2864 struct obd_client_handle *och = NULL;
2865 __u64 data_version = 0;
2870 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2871 ll_i2sbi(inode)->ll_fsname,
2872 PFID(&ll_i2info(inode)->lli_fid));
2874 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2876 GOTO(out, rc = PTR_ERR(och));
2878 /* Grab latest data_version and [am]time values */
2879 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2883 env = cl_env_get(&refcheck);
2885 GOTO(out, rc = PTR_ERR(env));
2887 rc = ll_merge_attr(env, inode);
2888 cl_env_put(env, &refcheck);
2890 /* If error happen, we have the wrong size for a file.
2896 /* Release the file.
2897 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2898 * we still need it to pack l_remote_handle to MDT. */
2899 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2905 if (och != NULL && !IS_ERR(och)) /* close the file */
2906 ll_lease_close(och, inode, NULL);
2911 struct ll_swap_stack {
2914 struct inode *inode1;
2915 struct inode *inode2;
2920 static int ll_swap_layouts(struct file *file1, struct file *file2,
2921 struct lustre_swap_layouts *lsl)
2923 struct mdc_swap_layouts msl;
2924 struct md_op_data *op_data;
2927 struct ll_swap_stack *llss = NULL;
2930 OBD_ALLOC_PTR(llss);
2934 llss->inode1 = file_inode(file1);
2935 llss->inode2 = file_inode(file2);
2937 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2941 /* we use 2 bool because it is easier to swap than 2 bits */
2942 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2943 llss->check_dv1 = true;
2945 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2946 llss->check_dv2 = true;
2948 /* we cannot use lsl->sl_dvX directly because we may swap them */
2949 llss->dv1 = lsl->sl_dv1;
2950 llss->dv2 = lsl->sl_dv2;
2952 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2953 if (rc == 0) /* same file, done! */
2956 if (rc < 0) { /* sequentialize it */
2957 swap(llss->inode1, llss->inode2);
2959 swap(llss->dv1, llss->dv2);
2960 swap(llss->check_dv1, llss->check_dv2);
2964 if (gid != 0) { /* application asks to flush dirty cache */
2965 rc = ll_get_grouplock(llss->inode1, file1, gid);
2969 rc = ll_get_grouplock(llss->inode2, file2, gid);
2971 ll_put_grouplock(llss->inode1, file1, gid);
2976 /* ultimate check, before swaping the layouts we check if
2977 * dataversion has changed (if requested) */
2978 if (llss->check_dv1) {
2979 rc = ll_data_version(llss->inode1, &dv, 0);
2982 if (dv != llss->dv1)
2983 GOTO(putgl, rc = -EAGAIN);
2986 if (llss->check_dv2) {
2987 rc = ll_data_version(llss->inode2, &dv, 0);
2990 if (dv != llss->dv2)
2991 GOTO(putgl, rc = -EAGAIN);
2994 /* struct md_op_data is used to send the swap args to the mdt
2995 * only flags is missing, so we use struct mdc_swap_layouts
2996 * through the md_op_data->op_data */
2997 /* flags from user space have to be converted before they are send to
2998 * server, no flag is sent today, they are only used on the client */
3001 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
3002 0, LUSTRE_OPC_ANY, &msl);
3003 if (IS_ERR(op_data))
3004 GOTO(free, rc = PTR_ERR(op_data));
3006 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
3007 sizeof(*op_data), op_data, NULL);
3008 ll_finish_md_op_data(op_data);
3015 ll_put_grouplock(llss->inode2, file2, gid);
3016 ll_put_grouplock(llss->inode1, file1, gid);
3026 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3028 struct obd_export *exp = ll_i2mdexp(inode);
3029 struct md_op_data *op_data;
3033 /* Detect out-of range masks */
3034 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3037 /* Non-root users are forbidden to set or clear flags which are
3038 * NOT defined in HSM_USER_MASK. */
3039 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3040 !capable(CAP_SYS_ADMIN))
3043 if (!exp_connect_archive_id_array(exp)) {
3044 /* Detect out-of range archive id */
3045 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3046 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3050 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3051 LUSTRE_OPC_ANY, hss);
3052 if (IS_ERR(op_data))
3053 RETURN(PTR_ERR(op_data));
3055 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3058 ll_finish_md_op_data(op_data);
3063 static int ll_hsm_import(struct inode *inode, struct file *file,
3064 struct hsm_user_import *hui)
3066 struct hsm_state_set *hss = NULL;
3067 struct iattr *attr = NULL;
3071 if (!S_ISREG(inode->i_mode))
3077 GOTO(out, rc = -ENOMEM);
3079 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3080 hss->hss_archive_id = hui->hui_archive_id;
3081 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3082 rc = ll_hsm_state_set(inode, hss);
3086 OBD_ALLOC_PTR(attr);
3088 GOTO(out, rc = -ENOMEM);
3090 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3091 attr->ia_mode |= S_IFREG;
3092 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3093 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3094 attr->ia_size = hui->hui_size;
3095 attr->ia_mtime.tv_sec = hui->hui_mtime;
3096 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3097 attr->ia_atime.tv_sec = hui->hui_atime;
3098 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3100 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3101 ATTR_UID | ATTR_GID |
3102 ATTR_MTIME | ATTR_MTIME_SET |
3103 ATTR_ATIME | ATTR_ATIME_SET;
3107 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3111 inode_unlock(inode);
3123 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3125 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3126 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3129 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3131 struct inode *inode = file_inode(file);
3133 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3134 ATTR_MTIME | ATTR_MTIME_SET |
3137 .tv_sec = lfu->lfu_atime_sec,
3138 .tv_nsec = lfu->lfu_atime_nsec,
3141 .tv_sec = lfu->lfu_mtime_sec,
3142 .tv_nsec = lfu->lfu_mtime_nsec,
3145 .tv_sec = lfu->lfu_ctime_sec,
3146 .tv_nsec = lfu->lfu_ctime_nsec,
3152 if (!capable(CAP_SYS_ADMIN))
3155 if (!S_ISREG(inode->i_mode))
3159 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3161 inode_unlock(inode);
3166 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3169 case MODE_READ_USER:
3171 case MODE_WRITE_USER:
3178 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3180 /* Used to allow the upper layers of the client to request an LDLM lock
3181 * without doing an actual read or write.
3183 * Used for ladvise lockahead to manually request specific locks.
3185 * \param[in] file file this ladvise lock request is on
3186 * \param[in] ladvise ladvise struct describing this lock request
3188 * \retval 0 success, no detailed result available (sync requests
3189 * and requests sent to the server [not handled locally]
3190 * cannot return detailed results)
3191 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3192 * see definitions for details.
3193 * \retval negative negative errno on error
3195 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3197 struct lu_env *env = NULL;
3198 struct cl_io *io = NULL;
3199 struct cl_lock *lock = NULL;
3200 struct cl_lock_descr *descr = NULL;
3201 struct dentry *dentry = file->f_path.dentry;
3202 struct inode *inode = dentry->d_inode;
3203 enum cl_lock_mode cl_mode;
3204 off_t start = ladvise->lla_start;
3205 off_t end = ladvise->lla_end;
3212 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3213 dentry, dentry->d_inode,
3214 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3217 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3219 GOTO(out, result = cl_mode);
3221 /* Get IO environment */
3222 result = cl_io_get(inode, &env, &io, &refcheck);
3226 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3229 * nothing to do for this io. This currently happens when
3230 * stripe sub-object's are not yet created.
3232 result = io->ci_result;
3233 } else if (result == 0) {
3234 lock = vvp_env_lock(env);
3235 descr = &lock->cll_descr;
3237 descr->cld_obj = io->ci_obj;
3238 /* Convert byte offsets to pages */
3239 descr->cld_start = cl_index(io->ci_obj, start);
3240 descr->cld_end = cl_index(io->ci_obj, end);
3241 descr->cld_mode = cl_mode;
3242 /* CEF_MUST is used because we do not want to convert a
3243 * lockahead request to a lockless lock */
3244 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3246 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3247 descr->cld_enq_flags |= CEF_SPECULATIVE;
3249 result = cl_lock_request(env, io, lock);
3251 /* On success, we need to release the lock */
3253 cl_lock_release(env, lock);
3255 cl_io_fini(env, io);
3256 cl_env_put(env, &refcheck);
3258 /* -ECANCELED indicates a matching lock with a different extent
3259 * was already present, and -EEXIST indicates a matching lock
3260 * on exactly the same extent was already present.
3261 * We convert them to positive values for userspace to make
3262 * recognizing true errors easier.
3263 * Note we can only return these detailed results on async requests,
3264 * as sync requests look the same as i/o requests for locking. */
3265 if (result == -ECANCELED)
3266 result = LLA_RESULT_DIFFERENT;
3267 else if (result == -EEXIST)
3268 result = LLA_RESULT_SAME;
3273 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3275 static int ll_ladvise_sanity(struct inode *inode,
3276 struct llapi_lu_ladvise *ladvise)
3278 struct ll_sb_info *sbi = ll_i2sbi(inode);
3279 enum lu_ladvise_type advice = ladvise->lla_advice;
3280 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3281 * be in the first 32 bits of enum ladvise_flags */
3282 __u32 flags = ladvise->lla_peradvice_flags;
3283 /* 3 lines at 80 characters per line, should be plenty */
3286 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3289 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3290 sbi->ll_fsname, advice,
3291 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3295 /* Per-advice checks */
3297 case LU_LADVISE_LOCKNOEXPAND:
3298 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3300 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3301 "rc = %d\n", sbi->ll_fsname, flags,
3302 ladvise_names[advice], rc);
3306 case LU_LADVISE_LOCKAHEAD:
3307 /* Currently only READ and WRITE modes can be requested */
3308 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3309 ladvise->lla_lockahead_mode == 0) {
3311 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3312 "rc = %d\n", sbi->ll_fsname,
3313 ladvise->lla_lockahead_mode,
3314 ladvise_names[advice], rc);
3318 case LU_LADVISE_WILLREAD:
3319 case LU_LADVISE_DONTNEED:
3321 /* Note fall through above - These checks apply to all advices
3322 * except LOCKNOEXPAND */
3323 if (flags & ~LF_DEFAULT_MASK) {
3325 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3326 "rc = %d\n", sbi->ll_fsname, flags,
3327 ladvise_names[advice], rc);
3330 if (ladvise->lla_start >= ladvise->lla_end) {
3332 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3333 "for %s: rc = %d\n", sbi->ll_fsname,
3334 ladvise->lla_start, ladvise->lla_end,
3335 ladvise_names[advice], rc);
3347 * Give file access advices
3349 * The ladvise interface is similar to Linux fadvise() system call, except it
3350 * forwards the advices directly from Lustre client to server. The server side
3351 * codes will apply appropriate read-ahead and caching techniques for the
3352 * corresponding files.
3354 * A typical workload for ladvise is e.g. a bunch of different clients are
3355 * doing small random reads of a file, so prefetching pages into OSS cache
3356 * with big linear reads before the random IO is a net benefit. Fetching
3357 * all that data into each client cache with fadvise() may not be, due to
3358 * much more data being sent to the client.
3360 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3361 struct llapi_lu_ladvise *ladvise)
3365 struct cl_ladvise_io *lio;
3370 env = cl_env_get(&refcheck);
3372 RETURN(PTR_ERR(env));
3374 io = vvp_env_thread_io(env);
3375 io->ci_obj = ll_i2info(inode)->lli_clob;
3377 /* initialize parameters for ladvise */
3378 lio = &io->u.ci_ladvise;
3379 lio->li_start = ladvise->lla_start;
3380 lio->li_end = ladvise->lla_end;
3381 lio->li_fid = ll_inode2fid(inode);
3382 lio->li_advice = ladvise->lla_advice;
3383 lio->li_flags = flags;
3385 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3386 rc = cl_io_loop(env, io);
3390 cl_io_fini(env, io);
3391 cl_env_put(env, &refcheck);
3395 static int ll_lock_noexpand(struct file *file, int flags)
3397 struct ll_file_data *fd = file->private_data;
3399 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3404 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3407 struct fsxattr fsxattr;
3409 if (copy_from_user(&fsxattr,
3410 (const struct fsxattr __user *)arg,
3414 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3415 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3416 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3417 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3418 if (copy_to_user((struct fsxattr __user *)arg,
3419 &fsxattr, sizeof(fsxattr)))
3425 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3429 * Project Quota ID state is only allowed to change from within the init
3430 * namespace. Enforce that restriction only if we are trying to change
3431 * the quota ID state. Everything else is allowed in user namespaces.
3433 if (current_user_ns() == &init_user_ns) {
3435 * Caller is allowed to change the project ID. if it is being
3436 * changed, make sure that the new value is valid.
3438 if (ll_i2info(inode)->lli_projid != projid &&
3439 !projid_valid(make_kprojid(&init_user_ns, projid)))
3445 if (ll_i2info(inode)->lli_projid != projid)
3448 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3449 if (!(xflags & FS_XFLAG_PROJINHERIT))
3452 if (xflags & FS_XFLAG_PROJINHERIT)
3459 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3461 struct md_op_data *op_data;
3462 struct ptlrpc_request *req = NULL;
3463 struct cl_object *obj;
3464 unsigned int inode_flags;
3467 rc = ll_ioctl_check_project(inode, xflags, projid);
3471 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3472 LUSTRE_OPC_ANY, NULL);
3473 if (IS_ERR(op_data))
3474 RETURN(PTR_ERR(op_data));
3476 inode_flags = ll_xflags_to_inode_flags(xflags);
3477 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3478 if (xflags & FS_XFLAG_PROJINHERIT)
3479 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3480 op_data->op_projid = projid;
3481 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3482 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3483 ptlrpc_req_finished(req);
3485 GOTO(out_fsxattr, rc);
3486 ll_update_inode_flags(inode, op_data->op_attr_flags);
3488 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3489 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3490 GOTO(out_fsxattr, rc);
3492 obj = ll_i2info(inode)->lli_clob;
3494 struct iattr attr = { 0 };
3496 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3500 ll_finish_md_op_data(op_data);
3504 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3507 struct fsxattr fsxattr;
3511 if (copy_from_user(&fsxattr,
3512 (const struct fsxattr __user *)arg,
3516 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3517 fsxattr.fsx_projid));
3520 int ll_ioctl_project(struct file *file, unsigned int cmd,
3523 struct lu_project lu_project;
3524 struct dentry *dentry = file_dentry(file);
3525 struct inode *inode = file_inode(file);
3526 struct dentry *child_dentry = NULL;
3527 int rc = 0, name_len;
3529 if (copy_from_user(&lu_project,
3530 (const struct lu_project __user *)arg,
3531 sizeof(lu_project)))
3534 /* apply child dentry if name is valid */
3535 name_len = strnlen(lu_project.project_name, NAME_MAX);
3536 if (name_len > 0 && name_len <= NAME_MAX) {
3538 child_dentry = lookup_one_len(lu_project.project_name,
3540 inode_unlock(inode);
3541 if (IS_ERR(child_dentry)) {
3542 rc = PTR_ERR(child_dentry);
3545 inode = child_dentry->d_inode;
3550 } else if (name_len > NAME_MAX) {
3555 switch (lu_project.project_type) {
3556 case LU_PROJECT_SET:
3557 rc = ll_set_project(inode, lu_project.project_xflags,
3558 lu_project.project_id);
3560 case LU_PROJECT_GET:
3561 lu_project.project_xflags =
3562 ll_inode_flags_to_xflags(inode->i_flags);
3563 if (test_bit(LLIF_PROJECT_INHERIT,
3564 &ll_i2info(inode)->lli_flags))
3565 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3566 lu_project.project_id = ll_i2info(inode)->lli_projid;
3567 if (copy_to_user((struct lu_project __user *)arg,
3568 &lu_project, sizeof(lu_project))) {
3578 if (!IS_ERR_OR_NULL(child_dentry))
3583 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3586 struct inode *inode = file_inode(file);
3587 struct ll_file_data *fd = file->private_data;
3588 struct ll_inode_info *lli = ll_i2info(inode);
3589 struct obd_client_handle *och = NULL;
3590 struct split_param sp;
3591 struct pcc_param param;
3592 bool lease_broken = false;
3594 enum mds_op_bias bias = 0;
3595 struct file *layout_file = NULL;
3597 size_t data_size = 0;
3598 bool attached = false;
3603 mutex_lock(&lli->lli_och_mutex);
3604 if (fd->fd_lease_och != NULL) {
3605 och = fd->fd_lease_och;
3606 fd->fd_lease_och = NULL;
3608 mutex_unlock(&lli->lli_och_mutex);
3613 fmode = och->och_flags;
3615 switch (ioc->lil_flags) {
3616 case LL_LEASE_RESYNC_DONE:
3617 if (ioc->lil_count > IOC_IDS_MAX)
3618 GOTO(out_lease_close, rc = -EINVAL);
3620 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3621 OBD_ALLOC(data, data_size);
3623 GOTO(out_lease_close, rc = -ENOMEM);
3625 if (copy_from_user(data, (void __user *)arg, data_size))
3626 GOTO(out_lease_close, rc = -EFAULT);
3628 bias = MDS_CLOSE_RESYNC_DONE;
3630 case LL_LEASE_LAYOUT_MERGE: {
3633 if (ioc->lil_count != 1)
3634 GOTO(out_lease_close, rc = -EINVAL);
3636 arg += sizeof(*ioc);
3637 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3638 GOTO(out_lease_close, rc = -EFAULT);
3640 layout_file = fget(fd);
3642 GOTO(out_lease_close, rc = -EBADF);
3644 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3645 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3646 GOTO(out_lease_close, rc = -EPERM);
3648 data = file_inode(layout_file);
3649 bias = MDS_CLOSE_LAYOUT_MERGE;
3652 case LL_LEASE_LAYOUT_SPLIT: {
3656 if (ioc->lil_count != 2)
3657 GOTO(out_lease_close, rc = -EINVAL);
3659 arg += sizeof(*ioc);
3660 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3661 GOTO(out_lease_close, rc = -EFAULT);
3663 arg += sizeof(__u32);
3664 if (copy_from_user(&mirror_id, (void __user *)arg,
3666 GOTO(out_lease_close, rc = -EFAULT);
3668 layout_file = fget(fdv);
3670 GOTO(out_lease_close, rc = -EBADF);
3672 /* if layout_file == file, it means to destroy the mirror */
3673 sp.sp_inode = file_inode(layout_file);
3674 sp.sp_mirror_id = (__u16)mirror_id;
3676 bias = MDS_CLOSE_LAYOUT_SPLIT;
3679 case LL_LEASE_PCC_ATTACH:
3680 if (ioc->lil_count != 1)
3683 if (IS_ENCRYPTED(inode))
3684 RETURN(-EOPNOTSUPP);
3686 arg += sizeof(*ioc);
3687 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3689 GOTO(out_lease_close, rc2 = -EFAULT);
3691 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3693 GOTO(out_lease_close, rc2);
3696 /* Grab latest data version */
3697 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3700 GOTO(out_lease_close, rc2);
3703 bias = MDS_PCC_ATTACH;
3706 /* without close intent */
3711 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3715 rc = ll_lease_och_release(inode, file);
3724 switch (ioc->lil_flags) {
3725 case LL_LEASE_RESYNC_DONE:
3727 OBD_FREE(data, data_size);
3729 case LL_LEASE_LAYOUT_MERGE:
3730 case LL_LEASE_LAYOUT_SPLIT:
3734 ll_layout_refresh(inode, &fd->fd_layout_version);
3736 case LL_LEASE_PCC_ATTACH:
3739 rc = pcc_readwrite_attach_fini(file, inode,
3740 param.pa_layout_gen,
3747 rc = ll_lease_type_from_fmode(fmode);
3751 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3754 struct inode *inode = file_inode(file);
3755 struct ll_inode_info *lli = ll_i2info(inode);
3756 struct ll_file_data *fd = file->private_data;
3757 struct obd_client_handle *och = NULL;
3758 __u64 open_flags = 0;
3764 switch (ioc->lil_mode) {
3765 case LL_LEASE_WRLCK:
3766 if (!(file->f_mode & FMODE_WRITE))
3768 fmode = FMODE_WRITE;
3770 case LL_LEASE_RDLCK:
3771 if (!(file->f_mode & FMODE_READ))
3775 case LL_LEASE_UNLCK:
3776 RETURN(ll_file_unlock_lease(file, ioc, arg));
3781 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3783 /* apply for lease */
3784 if (ioc->lil_flags & LL_LEASE_RESYNC)
3785 open_flags = MDS_OPEN_RESYNC;
3786 och = ll_lease_open(inode, file, fmode, open_flags);
3788 RETURN(PTR_ERR(och));
3790 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3791 rc = ll_lease_file_resync(och, inode, arg);
3793 ll_lease_close(och, inode, NULL);
3796 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3798 ll_lease_close(och, inode, NULL);
3804 mutex_lock(&lli->lli_och_mutex);
3805 if (fd->fd_lease_och == NULL) {
3806 fd->fd_lease_och = och;
3809 mutex_unlock(&lli->lli_och_mutex);
3811 /* impossible now that only excl is supported for now */
3812 ll_lease_close(och, inode, &lease_broken);
3818 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3820 struct ll_inode_info *lli = ll_i2info(inode);
3821 struct ll_sb_info *sbi = ll_i2sbi(inode);
3822 __u64 now = ktime_get_real_seconds();
3825 spin_lock(&lli->lli_heat_lock);
3826 heat->lh_flags = lli->lli_heat_flags;
3827 for (i = 0; i < heat->lh_count; i++)
3828 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3829 now, sbi->ll_heat_decay_weight,
3830 sbi->ll_heat_period_second);
3831 spin_unlock(&lli->lli_heat_lock);
3834 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3836 struct ll_inode_info *lli = ll_i2info(inode);
3839 spin_lock(&lli->lli_heat_lock);
3840 if (flags & LU_HEAT_FLAG_CLEAR)
3841 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3843 if (flags & LU_HEAT_FLAG_OFF)
3844 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3846 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3848 spin_unlock(&lli->lli_heat_lock);
3854 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3856 struct inode *inode = file_inode(file);
3857 struct ll_file_data *fd = file->private_data;
3861 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3862 PFID(ll_inode2fid(inode)), inode, cmd);
3863 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3865 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3866 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3870 case LL_IOC_GETFLAGS:
3871 /* Get the current value of the file flags */
3872 return put_user(fd->fd_flags, (int __user *)arg);
3873 case LL_IOC_SETFLAGS:
3874 case LL_IOC_CLRFLAGS:
3875 /* Set or clear specific file flags */
3876 /* XXX This probably needs checks to ensure the flags are
3877 * not abused, and to handle any flag side effects.
3879 if (get_user(flags, (int __user *) arg))
3882 if (cmd == LL_IOC_SETFLAGS) {
3883 if ((flags & LL_FILE_IGNORE_LOCK) &&
3884 !(file->f_flags & O_DIRECT)) {
3885 CERROR("%s: unable to disable locking on "
3886 "non-O_DIRECT file\n", current->comm);
3890 fd->fd_flags |= flags;
3892 fd->fd_flags &= ~flags;
3895 case LL_IOC_LOV_SETSTRIPE:
3896 case LL_IOC_LOV_SETSTRIPE_NEW:
3897 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3898 case LL_IOC_LOV_SETEA:
3899 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3900 case LL_IOC_LOV_SWAP_LAYOUTS: {
3902 struct lustre_swap_layouts lsl;
3904 if (copy_from_user(&lsl, (char __user *)arg,
3905 sizeof(struct lustre_swap_layouts)))
3908 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3911 file2 = fget(lsl.sl_fd);
3915 /* O_WRONLY or O_RDWR */
3916 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3917 GOTO(out, rc = -EPERM);
3919 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3920 struct inode *inode2;
3921 struct ll_inode_info *lli;
3922 struct obd_client_handle *och = NULL;
3924 lli = ll_i2info(inode);
3925 mutex_lock(&lli->lli_och_mutex);
3926 if (fd->fd_lease_och != NULL) {
3927 och = fd->fd_lease_och;
3928 fd->fd_lease_och = NULL;
3930 mutex_unlock(&lli->lli_och_mutex);
3932 GOTO(out, rc = -ENOLCK);
3933 inode2 = file_inode(file2);
3934 rc = ll_swap_layouts_close(och, inode, inode2);
3936 rc = ll_swap_layouts(file, file2, &lsl);
3942 case LL_IOC_LOV_GETSTRIPE:
3943 case LL_IOC_LOV_GETSTRIPE_NEW:
3944 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3945 case FS_IOC_GETFLAGS:
3946 case FS_IOC_SETFLAGS:
3947 RETURN(ll_iocontrol(inode, file, cmd, arg));
3948 case FSFILT_IOC_GETVERSION:
3949 case FS_IOC_GETVERSION:
3950 RETURN(put_user(inode->i_generation, (int __user *)arg));
3951 /* We need to special case any other ioctls we want to handle,
3952 * to send them to the MDS/OST as appropriate and to properly
3953 * network encode the arg field. */
3954 case FS_IOC_SETVERSION:
3957 case LL_IOC_GROUP_LOCK:
3958 RETURN(ll_get_grouplock(inode, file, arg));
3959 case LL_IOC_GROUP_UNLOCK:
3960 RETURN(ll_put_grouplock(inode, file, arg));
3961 case IOC_OBD_STATFS:
3962 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3964 case LL_IOC_FLUSHCTX:
3965 RETURN(ll_flush_ctx(inode));
3966 case LL_IOC_PATH2FID: {
3967 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3968 sizeof(struct lu_fid)))
3973 case LL_IOC_GETPARENT:
3974 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3976 case OBD_IOC_FID2PATH:
3977 RETURN(ll_fid2path(inode, (void __user *)arg));
3978 case LL_IOC_DATA_VERSION: {
3979 struct ioc_data_version idv;
3982 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3985 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3986 rc = ll_ioc_data_version(inode, &idv);
3989 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3995 case LL_IOC_GET_MDTIDX: {
3998 mdtidx = ll_get_mdt_idx(inode);
4002 if (put_user((int)mdtidx, (int __user *)arg))
4007 case OBD_IOC_GETNAME_OLD:
4009 case OBD_IOC_GETDTNAME:
4011 case OBD_IOC_GETMDNAME:
4012 RETURN(ll_get_obd_name(inode, cmd, arg));
4013 case LL_IOC_HSM_STATE_GET: {
4014 struct md_op_data *op_data;
4015 struct hsm_user_state *hus;
4022 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4023 LUSTRE_OPC_ANY, hus);
4024 if (IS_ERR(op_data)) {
4026 RETURN(PTR_ERR(op_data));
4029 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4032 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4035 ll_finish_md_op_data(op_data);
4039 case LL_IOC_HSM_STATE_SET: {
4040 struct hsm_state_set *hss;
4047 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4052 rc = ll_hsm_state_set(inode, hss);
4057 case LL_IOC_HSM_ACTION: {
4058 struct md_op_data *op_data;
4059 struct hsm_current_action *hca;
4067 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4068 LUSTRE_OPC_ANY, hca);
4069 if (IS_ERR(op_data)) {
4071 RETURN(PTR_ERR(op_data));
4074 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4077 GOTO(skip_copy, rc);
4079 /* The hsm_current_action retreived from the server could
4080 * contain corrupt information. If it is incorrect data collect
4081 * debug information. We still send the data even if incorrect
4082 * to user land to handle.
4084 action = hsm_user_action2name(hca->hca_action);
4085 if (strcmp(action, "UNKNOWN") == 0 ||
4086 hca->hca_state > HPS_DONE) {
4088 "HSM current state %s action %s, offset = %llu, length %llu\n",
4089 hsm_progress_state2name(hca->hca_state), action,
4090 hca->hca_location.offset, hca->hca_location.length);
4093 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4096 ll_finish_md_op_data(op_data);
4100 case LL_IOC_SET_LEASE_OLD: {
4101 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4103 RETURN(ll_file_set_lease(file, &ioc, 0));
4105 case LL_IOC_SET_LEASE: {
4106 struct ll_ioc_lease ioc;
4108 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4111 RETURN(ll_file_set_lease(file, &ioc, arg));
4113 case LL_IOC_GET_LEASE: {
4114 struct ll_inode_info *lli = ll_i2info(inode);
4115 struct ldlm_lock *lock = NULL;
4118 mutex_lock(&lli->lli_och_mutex);
4119 if (fd->fd_lease_och != NULL) {
4120 struct obd_client_handle *och = fd->fd_lease_och;
4122 lock = ldlm_handle2lock(&och->och_lease_handle);
4124 lock_res_and_lock(lock);
4125 if (!ldlm_is_cancel(lock))
4126 fmode = och->och_flags;
4128 unlock_res_and_lock(lock);
4129 LDLM_LOCK_PUT(lock);
4132 mutex_unlock(&lli->lli_och_mutex);
4134 RETURN(ll_lease_type_from_fmode(fmode));
4136 case LL_IOC_HSM_IMPORT: {
4137 struct hsm_user_import *hui;
4143 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4148 rc = ll_hsm_import(inode, file, hui);
4153 case LL_IOC_FUTIMES_3: {
4154 struct ll_futimes_3 lfu;
4156 if (copy_from_user(&lfu,
4157 (const struct ll_futimes_3 __user *)arg,
4161 RETURN(ll_file_futimes_3(file, &lfu));
4163 case LL_IOC_LADVISE: {
4164 struct llapi_ladvise_hdr *k_ladvise_hdr;
4165 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4168 int alloc_size = sizeof(*k_ladvise_hdr);
4171 u_ladvise_hdr = (void __user *)arg;
4172 OBD_ALLOC_PTR(k_ladvise_hdr);
4173 if (k_ladvise_hdr == NULL)
4176 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4177 GOTO(out_ladvise, rc = -EFAULT);
4179 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4180 k_ladvise_hdr->lah_count < 1)
4181 GOTO(out_ladvise, rc = -EINVAL);
4183 num_advise = k_ladvise_hdr->lah_count;
4184 if (num_advise >= LAH_COUNT_MAX)
4185 GOTO(out_ladvise, rc = -EFBIG);
4187 OBD_FREE_PTR(k_ladvise_hdr);
4188 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4189 lah_advise[num_advise]);
4190 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4191 if (k_ladvise_hdr == NULL)
4195 * TODO: submit multiple advices to one server in a single RPC
4197 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4198 GOTO(out_ladvise, rc = -EFAULT);
4200 for (i = 0; i < num_advise; i++) {
4201 struct llapi_lu_ladvise *k_ladvise =
4202 &k_ladvise_hdr->lah_advise[i];
4203 struct llapi_lu_ladvise __user *u_ladvise =
4204 &u_ladvise_hdr->lah_advise[i];
4206 rc = ll_ladvise_sanity(inode, k_ladvise);
4208 GOTO(out_ladvise, rc);
4210 switch (k_ladvise->lla_advice) {
4211 case LU_LADVISE_LOCKNOEXPAND:
4212 rc = ll_lock_noexpand(file,
4213 k_ladvise->lla_peradvice_flags);
4214 GOTO(out_ladvise, rc);
4215 case LU_LADVISE_LOCKAHEAD:
4217 rc = ll_file_lock_ahead(file, k_ladvise);
4220 GOTO(out_ladvise, rc);
4223 &u_ladvise->lla_lockahead_result))
4224 GOTO(out_ladvise, rc = -EFAULT);
4227 rc = ll_ladvise(inode, file,
4228 k_ladvise_hdr->lah_flags,
4231 GOTO(out_ladvise, rc);
4238 OBD_FREE(k_ladvise_hdr, alloc_size);
4241 case LL_IOC_FLR_SET_MIRROR: {
4242 /* mirror I/O must be direct to avoid polluting page cache
4244 if (!(file->f_flags & O_DIRECT))
4247 fd->fd_designated_mirror = (__u32)arg;
4250 case FS_IOC_FSGETXATTR:
4251 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4252 case FS_IOC_FSSETXATTR:
4253 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4254 case LL_IOC_PROJECT:
4255 RETURN(ll_ioctl_project(file, cmd, arg));
4257 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4258 case LL_IOC_HEAT_GET: {
4259 struct lu_heat uheat;
4260 struct lu_heat *heat;
4263 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4266 if (uheat.lh_count > OBD_HEAT_COUNT)
4267 uheat.lh_count = OBD_HEAT_COUNT;
4269 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4270 OBD_ALLOC(heat, size);
4274 heat->lh_count = uheat.lh_count;
4275 ll_heat_get(inode, heat);
4276 rc = copy_to_user((char __user *)arg, heat, size);
4277 OBD_FREE(heat, size);
4278 RETURN(rc ? -EFAULT : 0);
4280 case LL_IOC_HEAT_SET: {
4283 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4286 rc = ll_heat_set(inode, flags);
4289 case LL_IOC_PCC_DETACH: {
4290 struct lu_pcc_detach *detach;
4292 OBD_ALLOC_PTR(detach);
4296 if (copy_from_user(detach,
4297 (const struct lu_pcc_detach __user *)arg,
4299 GOTO(out_detach_free, rc = -EFAULT);
4301 if (!S_ISREG(inode->i_mode))
4302 GOTO(out_detach_free, rc = -EINVAL);
4304 if (!inode_owner_or_capable(inode))
4305 GOTO(out_detach_free, rc = -EPERM);
4307 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4309 OBD_FREE_PTR(detach);
4312 case LL_IOC_PCC_STATE: {
4313 struct lu_pcc_state __user *ustate =
4314 (struct lu_pcc_state __user *)arg;
4315 struct lu_pcc_state *state;
4317 OBD_ALLOC_PTR(state);
4321 if (copy_from_user(state, ustate, sizeof(*state)))
4322 GOTO(out_state, rc = -EFAULT);
4324 rc = pcc_ioctl_state(file, inode, state);
4326 GOTO(out_state, rc);
4328 if (copy_to_user(ustate, state, sizeof(*state)))
4329 GOTO(out_state, rc = -EFAULT);
4332 OBD_FREE_PTR(state);
4335 #ifdef HAVE_LUSTRE_CRYPTO
4336 case LL_IOC_SET_ENCRYPTION_POLICY:
4337 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4339 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4340 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4341 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4343 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4344 case LL_IOC_ADD_ENCRYPTION_KEY:
4345 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4347 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4348 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4349 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4351 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4352 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4353 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4355 return llcrypt_ioctl_remove_key_all_users(file,
4356 (void __user *)arg);
4357 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4358 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4360 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4363 case LL_IOC_UNLOCK_FOREIGN: {
4364 struct dentry *dentry = file_dentry(file);
4366 /* if not a foreign symlink do nothing */
4367 if (ll_foreign_is_removable(dentry, true)) {
4369 "prevent unlink of non-foreign file ("DFID")\n",
4370 PFID(ll_inode2fid(inode)));
4371 RETURN(-EOPNOTSUPP);
4377 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4378 (void __user *)arg));
4382 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4384 struct inode *inode = file_inode(file);
4387 struct cl_lseek_io *lsio;
4394 env = cl_env_get(&refcheck);
4396 RETURN(PTR_ERR(env));
4398 io = vvp_env_thread_io(env);
4399 io->ci_obj = ll_i2info(inode)->lli_clob;
4400 ll_io_set_mirror(io, file);
4402 lsio = &io->u.ci_lseek;
4403 lsio->ls_start = offset;
4404 lsio->ls_whence = whence;
4405 lsio->ls_result = -ENXIO;
4408 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4410 struct vvp_io *vio = vvp_env_io(env);
4412 vio->vui_fd = file->private_data;
4413 rc = cl_io_loop(env, io);
4417 retval = rc ? : lsio->ls_result;
4418 cl_io_fini(env, io);
4419 } while (unlikely(io->ci_need_restart));
4421 cl_env_put(env, &refcheck);
4426 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4428 struct inode *inode = file_inode(file);
4429 loff_t retval = offset, eof = 0;
4430 ktime_t kstart = ktime_get();
4434 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4435 PFID(ll_inode2fid(inode)), inode, retval, retval,
4438 if (origin == SEEK_END) {
4439 retval = ll_glimpse_size(inode);
4442 eof = i_size_read(inode);
4445 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4449 /* flush local cache first if any */
4450 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4453 retval = ll_lseek(file, offset, origin);
4456 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4458 retval = generic_file_llseek_size(file, offset, origin,
4459 ll_file_maxbytes(inode), eof);
4462 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4463 ktime_us_delta(ktime_get(), kstart));
4467 static int ll_flush(struct file *file, fl_owner_t id)
4469 struct inode *inode = file_inode(file);
4470 struct ll_inode_info *lli = ll_i2info(inode);
4471 struct ll_file_data *fd = file->private_data;
4474 LASSERT(!S_ISDIR(inode->i_mode));
4476 /* catch async errors that were recorded back when async writeback
4477 * failed for pages in this mapping. */
4478 rc = lli->lli_async_rc;
4479 lli->lli_async_rc = 0;
4480 if (lli->lli_clob != NULL) {
4481 err = lov_read_and_clear_async_rc(lli->lli_clob);
4486 /* The application has been told write failure already.
4487 * Do not report failure again. */
4488 if (fd->fd_write_failed)
4490 return rc ? -EIO : 0;
4494 * Called to make sure a portion of file has been written out.
4495 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4497 * Return how many pages have been written.
4499 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4500 enum cl_fsync_mode mode, int ignore_layout)
4504 struct cl_fsync_io *fio;
4509 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4510 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4513 env = cl_env_get(&refcheck);
4515 RETURN(PTR_ERR(env));
4517 io = vvp_env_thread_io(env);
4518 io->ci_obj = ll_i2info(inode)->lli_clob;
4519 io->ci_ignore_layout = ignore_layout;
4521 /* initialize parameters for sync */
4522 fio = &io->u.ci_fsync;
4523 fio->fi_start = start;
4525 fio->fi_fid = ll_inode2fid(inode);
4526 fio->fi_mode = mode;
4527 fio->fi_nr_written = 0;
4529 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4530 result = cl_io_loop(env, io);
4532 result = io->ci_result;
4534 result = fio->fi_nr_written;
4535 cl_io_fini(env, io);
4536 cl_env_put(env, &refcheck);
4542 * When dentry is provided (the 'else' case), file_dentry() may be
4543 * null and dentry must be used directly rather than pulled from
4544 * file_dentry() as is done otherwise.
4547 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4549 struct dentry *dentry = file_dentry(file);
4550 struct inode *inode = dentry->d_inode;
4551 struct ll_inode_info *lli = ll_i2info(inode);
4552 struct ptlrpc_request *req;
4553 ktime_t kstart = ktime_get();
4559 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4560 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4562 /* fsync's caller has already called _fdata{sync,write}, we want
4563 * that IO to finish before calling the osc and mdc sync methods */
4564 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4566 /* catch async errors that were recorded back when async writeback
4567 * failed for pages in this mapping. */
4568 if (!S_ISDIR(inode->i_mode)) {
4569 err = lli->lli_async_rc;
4570 lli->lli_async_rc = 0;
4573 if (lli->lli_clob != NULL) {
4574 err = lov_read_and_clear_async_rc(lli->lli_clob);
4580 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4584 ptlrpc_req_finished(req);
4586 if (S_ISREG(inode->i_mode)) {
4587 struct ll_file_data *fd = file->private_data;
4590 /* Sync metadata on MDT first, and then sync the cached data
4593 err = pcc_fsync(file, start, end, datasync, &cached);
4595 err = cl_sync_file_range(inode, start, end,
4597 if (rc == 0 && err < 0)
4600 fd->fd_write_failed = true;
4602 fd->fd_write_failed = false;
4606 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4607 ktime_us_delta(ktime_get(), kstart));
4612 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4614 struct inode *inode = file_inode(file);
4615 struct ll_sb_info *sbi = ll_i2sbi(inode);
4616 struct ldlm_enqueue_info einfo = {
4617 .ei_type = LDLM_FLOCK,
4618 .ei_cb_cp = ldlm_flock_completion_ast,
4619 .ei_cbdata = file_lock,
4621 struct md_op_data *op_data;
4622 struct lustre_handle lockh = { 0 };
4623 union ldlm_policy_data flock = { { 0 } };
4624 int fl_type = file_lock->fl_type;
4625 ktime_t kstart = ktime_get();
4631 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4632 PFID(ll_inode2fid(inode)), file_lock);
4634 if (file_lock->fl_flags & FL_FLOCK) {
4635 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4636 /* flocks are whole-file locks */
4637 flock.l_flock.end = OFFSET_MAX;
4638 /* For flocks owner is determined by the local file desctiptor*/
4639 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4640 } else if (file_lock->fl_flags & FL_POSIX) {
4641 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4642 flock.l_flock.start = file_lock->fl_start;
4643 flock.l_flock.end = file_lock->fl_end;
4647 flock.l_flock.pid = file_lock->fl_pid;
4649 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4650 /* Somewhat ugly workaround for svc lockd.
4651 * lockd installs custom fl_lmops->lm_compare_owner that checks
4652 * for the fl_owner to be the same (which it always is on local node
4653 * I guess between lockd processes) and then compares pid.
4654 * As such we assign pid to the owner field to make it all work,
4655 * conflict with normal locks is unlikely since pid space and
4656 * pointer space for current->files are not intersecting */
4657 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4658 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4663 einfo.ei_mode = LCK_PR;
4666 /* An unlock request may or may not have any relation to
4667 * existing locks so we may not be able to pass a lock handle
4668 * via a normal ldlm_lock_cancel() request. The request may even
4669 * unlock a byte range in the middle of an existing lock. In
4670 * order to process an unlock request we need all of the same
4671 * information that is given with a normal read or write record
4672 * lock request. To avoid creating another ldlm unlock (cancel)
4673 * message we'll treat a LCK_NL flock request as an unlock. */
4674 einfo.ei_mode = LCK_NL;
4677 einfo.ei_mode = LCK_PW;
4680 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4695 flags = LDLM_FL_BLOCK_NOWAIT;
4701 flags = LDLM_FL_TEST_LOCK;
4704 CERROR("unknown fcntl lock command: %d\n", cmd);
4708 /* Save the old mode so that if the mode in the lock changes we
4709 * can decrement the appropriate reader or writer refcount. */
4710 file_lock->fl_type = einfo.ei_mode;
4712 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4713 LUSTRE_OPC_ANY, NULL);
4714 if (IS_ERR(op_data))
4715 RETURN(PTR_ERR(op_data));
4717 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4718 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4719 flock.l_flock.pid, flags, einfo.ei_mode,
4720 flock.l_flock.start, flock.l_flock.end);
4722 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4725 /* Restore the file lock type if not TEST lock. */
4726 if (!(flags & LDLM_FL_TEST_LOCK))
4727 file_lock->fl_type = fl_type;
4729 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4730 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4731 !(flags & LDLM_FL_TEST_LOCK))
4732 rc2 = locks_lock_file_wait(file, file_lock);
4734 if ((file_lock->fl_flags & FL_FLOCK) &&
4735 (rc == 0 || file_lock->fl_type == F_UNLCK))
4736 rc2 = flock_lock_file_wait(file, file_lock);
4737 if ((file_lock->fl_flags & FL_POSIX) &&
4738 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4739 !(flags & LDLM_FL_TEST_LOCK))
4740 rc2 = posix_lock_file_wait(file, file_lock);
4741 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4743 if (rc2 && file_lock->fl_type != F_UNLCK) {
4744 einfo.ei_mode = LCK_NL;
4745 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4750 ll_finish_md_op_data(op_data);
4753 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4754 ktime_us_delta(ktime_get(), kstart));
4758 int ll_get_fid_by_name(struct inode *parent, const char *name,
4759 int namelen, struct lu_fid *fid,
4760 struct inode **inode)
4762 struct md_op_data *op_data = NULL;
4763 struct mdt_body *body;
4764 struct ptlrpc_request *req;
4768 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4769 LUSTRE_OPC_ANY, NULL);
4770 if (IS_ERR(op_data))
4771 RETURN(PTR_ERR(op_data));
4773 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4774 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4775 ll_finish_md_op_data(op_data);
4779 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4781 GOTO(out_req, rc = -EFAULT);
4783 *fid = body->mbo_fid1;
4786 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4788 ptlrpc_req_finished(req);
4792 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4795 struct dentry *dchild = NULL;
4796 struct inode *child_inode = NULL;
4797 struct md_op_data *op_data;
4798 struct ptlrpc_request *request = NULL;
4799 struct obd_client_handle *och = NULL;
4801 struct mdt_body *body;
4802 __u64 data_version = 0;
4803 size_t namelen = strlen(name);
4804 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4808 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4809 PFID(ll_inode2fid(parent)), name,
4810 lum->lum_stripe_offset, lum->lum_stripe_count);
4812 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4813 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4814 lustre_swab_lmv_user_md(lum);
4816 /* Get child FID first */
4817 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4820 dchild = d_lookup(file_dentry(file), &qstr);
4822 if (dchild->d_inode)
4823 child_inode = igrab(dchild->d_inode);
4828 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4837 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4838 OBD_CONNECT2_DIR_MIGRATE)) {
4839 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4840 ll_dir_striped(child_inode)) {
4841 CERROR("%s: MDT doesn't support stripe directory "
4842 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4843 GOTO(out_iput, rc = -EOPNOTSUPP);
4848 * lfs migrate command needs to be blocked on the client
4849 * by checking the migrate FID against the FID of the
4852 if (is_root_inode(child_inode))
4853 GOTO(out_iput, rc = -EINVAL);
4855 if (IS_ENCRYPTED(child_inode)) {
4856 rc = llcrypt_get_encryption_info(child_inode);
4859 if (!llcrypt_has_encryption_key(child_inode)) {
4860 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4861 PFID(ll_inode2fid(child_inode)));
4862 GOTO(out_iput, rc = -ENOKEY);
4864 if (unlikely(!llcrypt_policy_has_filename_enc(child_inode))) {
4866 "cannot migrate old format encrypted "DFID", please move to new enc dir first\n",
4867 PFID(ll_inode2fid(child_inode)));
4868 GOTO(out_iput, rc = -EUCLEAN);
4872 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4873 child_inode->i_mode, LUSTRE_OPC_MIGR,
4875 if (IS_ERR(op_data))
4876 GOTO(out_iput, rc = PTR_ERR(op_data));
4878 inode_lock(child_inode);
4879 op_data->op_fid3 = *ll_inode2fid(child_inode);
4880 if (!fid_is_sane(&op_data->op_fid3)) {
4881 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4882 ll_i2sbi(parent)->ll_fsname, name,
4883 PFID(&op_data->op_fid3));
4884 GOTO(out_unlock, rc = -EINVAL);
4887 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4888 op_data->op_data = lum;
4889 op_data->op_data_size = lumlen;
4892 if (S_ISREG(child_inode->i_mode)) {
4893 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4897 GOTO(out_unlock, rc);
4900 rc = ll_data_version(child_inode, &data_version,
4903 GOTO(out_close, rc);
4905 op_data->op_open_handle = och->och_open_handle;
4906 op_data->op_data_version = data_version;
4907 op_data->op_lease_handle = och->och_lease_handle;
4908 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4910 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4911 och->och_mod->mod_open_req->rq_replay = 0;
4912 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4915 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data,
4916 op_data->op_name, op_data->op_namelen,
4917 op_data->op_name, op_data->op_namelen, &request);
4919 LASSERT(request != NULL);
4920 ll_update_times(request, parent);
4923 if (rc == 0 || rc == -EAGAIN) {
4924 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4925 LASSERT(body != NULL);
4927 /* If the server does release layout lock, then we cleanup
4928 * the client och here, otherwise release it in out_close: */
4929 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4930 obd_mod_put(och->och_mod);
4931 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4933 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4939 if (request != NULL) {
4940 ptlrpc_req_finished(request);
4944 /* Try again if the lease has cancelled. */
4945 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4950 ll_lease_close(och, child_inode, NULL);
4952 clear_nlink(child_inode);
4954 inode_unlock(child_inode);
4955 ll_finish_md_op_data(op_data);
4962 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4964 struct ll_file_data *fd = file->private_data;
4968 * In order to avoid flood of warning messages, only print one message
4969 * for one file. And the entire message rate on the client is limited
4970 * by CDEBUG_LIMIT too.
4972 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4973 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4974 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4975 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4981 * test if some locks matching bits and l_req_mode are acquired
4982 * - bits can be in different locks
4983 * - if found clear the common lock bits in *bits
4984 * - the bits not found, are kept in *bits
4986 * \param bits [IN] searched lock bits [IN]
4987 * \param l_req_mode [IN] searched lock mode
4988 * \retval boolean, true iff all bits are found
4990 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4992 struct lustre_handle lockh;
4993 union ldlm_policy_data policy;
4994 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4995 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
5004 fid = &ll_i2info(inode)->lli_fid;
5005 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
5006 ldlm_lockname[mode]);
5008 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
5009 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
5010 policy.l_inodebits.bits = *bits & BIT(i);
5011 if (policy.l_inodebits.bits == 0)
5014 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
5015 &policy, mode, &lockh)) {
5016 struct ldlm_lock *lock;
5018 lock = ldlm_handle2lock(&lockh);
5021 ~(lock->l_policy_data.l_inodebits.bits);
5022 LDLM_LOCK_PUT(lock);
5024 *bits &= ~policy.l_inodebits.bits;
5031 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5032 struct lustre_handle *lockh, __u64 flags,
5033 enum ldlm_mode mode)
5035 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5040 fid = &ll_i2info(inode)->lli_fid;
5041 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5043 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5044 fid, LDLM_IBITS, &policy, mode, lockh);
5049 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5051 /* Already unlinked. Just update nlink and return success */
5052 if (rc == -ENOENT) {
5054 /* If it is striped directory, and there is bad stripe
5055 * Let's revalidate the dentry again, instead of returning
5057 if (ll_dir_striped(inode))
5060 /* This path cannot be hit for regular files unless in
5061 * case of obscure races, so no need to to validate
5063 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5065 } else if (rc != 0) {
5066 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5067 "%s: revalidate FID "DFID" error: rc = %d\n",
5068 ll_i2sbi(inode)->ll_fsname,
5069 PFID(ll_inode2fid(inode)), rc);
5075 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5077 struct inode *parent;
5078 struct inode *inode = dentry->d_inode;
5079 struct obd_export *exp = ll_i2mdexp(inode);
5080 struct lookup_intent oit = {
5083 struct ptlrpc_request *req = NULL;
5084 struct md_op_data *op_data;
5085 const char *name = NULL;
5090 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5091 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5093 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5094 parent = dentry->d_parent->d_inode;
5095 name = dentry->d_name.name;
5096 namelen = dentry->d_name.len;
5101 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5102 LUSTRE_OPC_ANY, NULL);
5103 if (IS_ERR(op_data))
5104 RETURN(PTR_ERR(op_data));
5106 /* Call getattr by fid */
5107 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5108 op_data->op_flags = MF_GETATTR_BY_FID;
5109 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5110 ll_finish_md_op_data(op_data);
5112 rc = ll_inode_revalidate_fini(inode, rc);
5116 rc = ll_revalidate_it_finish(req, &oit, dentry);
5118 ll_intent_release(&oit);
5122 /* Unlinked? Unhash dentry, so it is not picked up later by
5123 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5124 * here to preserve get_cwd functionality on 2.6.
5126 if (!dentry->d_inode->i_nlink)
5127 d_lustre_invalidate(dentry);
5129 ll_lookup_finish_locks(&oit, dentry);
5131 ptlrpc_req_finished(req);
5136 static int ll_merge_md_attr(struct inode *inode)
5138 struct ll_inode_info *lli = ll_i2info(inode);
5139 struct cl_attr attr = { 0 };
5142 LASSERT(lli->lli_lsm_md != NULL);
5144 if (!lmv_dir_striped(lli->lli_lsm_md))
5147 down_read(&lli->lli_lsm_sem);
5148 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5149 &attr, ll_md_blocking_ast);
5150 up_read(&lli->lli_lsm_sem);
5154 spin_lock(&inode->i_lock);
5155 set_nlink(inode, attr.cat_nlink);
5156 spin_unlock(&inode->i_lock);
5158 inode->i_blocks = attr.cat_blocks;
5159 i_size_write(inode, attr.cat_size);
5161 ll_i2info(inode)->lli_atime = attr.cat_atime;
5162 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5163 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5168 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5169 unsigned int flags, bool foreign)
5171 struct inode *inode = de->d_inode;
5172 struct ll_sb_info *sbi = ll_i2sbi(inode);
5173 struct ll_inode_info *lli = ll_i2info(inode);
5174 struct inode *dir = de->d_parent->d_inode;
5175 bool need_glimpse = true;
5176 ktime_t kstart = ktime_get();
5179 /* The OST object(s) determine the file size, blocks and mtime. */
5180 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5181 request_mask & STATX_MTIME))
5182 need_glimpse = false;
5184 if (dentry_may_statahead(dir, de))
5185 ll_start_statahead(dir, de, need_glimpse &&
5186 !(flags & AT_STATX_DONT_SYNC));
5188 if (flags & AT_STATX_DONT_SYNC)
5189 GOTO(fill_attr, rc = 0);
5191 rc = ll_inode_revalidate(de, IT_GETATTR);
5195 /* foreign file/dir are always of zero length, so don't
5196 * need to validate size.
5198 if (S_ISREG(inode->i_mode) && !foreign) {
5202 GOTO(fill_attr, rc);
5204 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5205 if (cached && rc < 0)
5209 GOTO(fill_attr, rc);
5212 * If the returned attr is masked with OBD_MD_FLSIZE &
5213 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5214 * or blocks obtained from MDT is strictly correct, and the file
5215 * is usually not being modified by clients, and the [a|m|c]time
5216 * got from MDT is also strictly correct.
5217 * Under this circumstance, it does not need to send glimpse
5218 * RPCs to OSTs for file attributes such as the size and blocks.
5220 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5221 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5222 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5223 inode->i_mtime.tv_sec = lli->lli_mtime;
5224 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5225 inode->i_atime.tv_sec = lli->lli_atime;
5226 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5227 inode->i_ctime.tv_sec = lli->lli_ctime;
5228 GOTO(fill_attr, rc);
5231 /* In case of restore, the MDT has the right size and has
5232 * already send it back without granting the layout lock,
5233 * inode is up-to-date so glimpse is useless.
5234 * Also to glimpse we need the layout, in case of a running
5235 * restore the MDT holds the layout lock so the glimpse will
5236 * block up to the end of restore (getattr will block)
5238 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5239 rc = ll_glimpse_size(inode);
5244 /* If object isn't regular a file then don't validate size. */
5245 /* foreign dir is not striped dir */
5246 if (ll_dir_striped(inode) && !foreign) {
5247 rc = ll_merge_md_attr(inode);
5252 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5253 inode->i_atime.tv_sec = lli->lli_atime;
5254 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5255 inode->i_mtime.tv_sec = lli->lli_mtime;
5256 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5257 inode->i_ctime.tv_sec = lli->lli_ctime;
5261 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5263 if (ll_need_32bit_api(sbi)) {
5264 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5265 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5266 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5268 stat->ino = inode->i_ino;
5269 stat->dev = inode->i_sb->s_dev;
5270 stat->rdev = inode->i_rdev;
5273 /* foreign symlink to be exposed as a real symlink */
5275 stat->mode = inode->i_mode;
5277 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5279 stat->uid = inode->i_uid;
5280 stat->gid = inode->i_gid;
5281 stat->atime = inode->i_atime;
5282 stat->mtime = inode->i_mtime;
5283 stat->ctime = inode->i_ctime;
5284 /* stat->blksize is used to tell about preferred IO size */
5285 if (sbi->ll_stat_blksize)
5286 stat->blksize = sbi->ll_stat_blksize;
5287 else if (S_ISREG(inode->i_mode))
5288 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5289 LL_MAX_BLKSIZE_BITS);
5291 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5293 stat->nlink = inode->i_nlink;
5294 stat->size = i_size_read(inode);
5295 stat->blocks = inode->i_blocks;
5297 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5298 if (flags & AT_STATX_DONT_SYNC) {
5299 if (stat->size == 0 &&
5300 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5301 stat->size = lli->lli_lazysize;
5302 if (stat->blocks == 0 &&
5303 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5304 stat->blocks = lli->lli_lazyblocks;
5307 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5308 stat->result_mask |= STATX_BTIME;
5309 stat->btime.tv_sec = lli->lli_btime;
5312 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5313 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5314 stat->result_mask &= request_mask;
5317 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5318 ktime_us_delta(ktime_get(), kstart));
5323 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5324 int ll_getattr(const struct path *path, struct kstat *stat,
5325 u32 request_mask, unsigned int flags)
5327 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5331 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5333 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5334 AT_STATX_SYNC_AS_STAT, false);
5338 int cl_falloc(struct file *file, struct inode *inode, int mode, loff_t offset,
5345 loff_t size = i_size_read(inode);
5349 env = cl_env_get(&refcheck);
5351 RETURN(PTR_ERR(env));
5353 io = vvp_env_thread_io(env);
5354 io->ci_obj = ll_i2info(inode)->lli_clob;
5355 ll_io_set_mirror(io, file);
5357 io->ci_verify_layout = 1;
5358 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5359 io->u.ci_setattr.sa_falloc_mode = mode;
5360 io->u.ci_setattr.sa_falloc_offset = offset;
5361 io->u.ci_setattr.sa_falloc_end = offset + len;
5362 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5364 CDEBUG(D_INODE, "UID %u GID %u\n",
5365 from_kuid(&init_user_ns, inode->i_uid),
5366 from_kgid(&init_user_ns, inode->i_gid));
5368 io->u.ci_setattr.sa_falloc_uid = from_kuid(&init_user_ns, inode->i_uid);
5369 io->u.ci_setattr.sa_falloc_gid = from_kgid(&init_user_ns, inode->i_gid);
5371 if (io->u.ci_setattr.sa_falloc_end > size) {
5372 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5374 /* Check new size against VFS/VM file size limit and rlimit */
5375 rc = inode_newsize_ok(inode, newsize);
5378 if (newsize > ll_file_maxbytes(inode)) {
5379 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5380 (unsigned long long)newsize,
5381 ll_file_maxbytes(inode));
5388 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5390 rc = cl_io_loop(env, io);
5393 cl_io_fini(env, io);
5394 } while (unlikely(io->ci_need_restart));
5397 cl_env_put(env, &refcheck);
5401 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5403 struct inode *inode = file_inode(filp);
5406 if (offset < 0 || len <= 0)
5409 * Encrypted inodes can't handle collapse range or zero range or insert
5410 * range since we would need to re-encrypt blocks with a different IV or
5411 * XTS tweak (which are based on the logical block number).
5412 * Similar to what ext4 does.
5414 if (IS_ENCRYPTED(inode) &&
5415 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5416 FALLOC_FL_ZERO_RANGE)))
5417 RETURN(-EOPNOTSUPP);
5420 * mode == 0 (which is standard prealloc) and PUNCH is supported
5421 * Rest of mode options are not supported yet.
5423 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5424 RETURN(-EOPNOTSUPP);
5426 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5428 rc = cl_falloc(filp, inode, mode, offset, len);
5430 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5431 * used by Lustre in several places. Retuning it here would
5432 * confuse applications that explicity test for EOPNOTSUPP
5433 * (95) and fall back to ftruncate().
5435 if (rc == -ENOTSUPP)
5441 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5442 __u64 start, __u64 len)
5446 struct fiemap *fiemap;
5447 unsigned int extent_count = fieinfo->fi_extents_max;
5449 num_bytes = sizeof(*fiemap) + (extent_count *
5450 sizeof(struct fiemap_extent));
5451 OBD_ALLOC_LARGE(fiemap, num_bytes);
5456 fiemap->fm_flags = fieinfo->fi_flags;
5457 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5458 fiemap->fm_start = start;
5459 fiemap->fm_length = len;
5460 if (extent_count > 0 &&
5461 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5462 sizeof(struct fiemap_extent)) != 0)
5463 GOTO(out, rc = -EFAULT);
5465 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5467 if (IS_ENCRYPTED(inode)) {
5470 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5471 fiemap->fm_extents[i].fe_flags |=
5472 FIEMAP_EXTENT_DATA_ENCRYPTED |
5473 FIEMAP_EXTENT_ENCODED;
5476 fieinfo->fi_flags = fiemap->fm_flags;
5477 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5478 if (extent_count > 0 &&
5479 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5480 fiemap->fm_mapped_extents *
5481 sizeof(struct fiemap_extent)) != 0)
5482 GOTO(out, rc = -EFAULT);
5484 OBD_FREE_LARGE(fiemap, num_bytes);
5488 int ll_inode_permission(struct inode *inode, int mask)
5491 struct ll_sb_info *sbi;
5492 struct root_squash_info *squash;
5493 struct cred *cred = NULL;
5494 const struct cred *old_cred = NULL;
5495 bool squash_id = false;
5496 ktime_t kstart = ktime_get();
5500 if (mask & MAY_NOT_BLOCK)
5504 * as root inode are NOT getting validated in lookup operation,
5505 * need to do it before permission check.
5508 if (is_root_inode(inode)) {
5509 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5514 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5515 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5517 /* squash fsuid/fsgid if needed */
5518 sbi = ll_i2sbi(inode);
5519 squash = &sbi->ll_squash;
5520 if (unlikely(squash->rsi_uid != 0 &&
5521 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5522 !test_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags))) {
5526 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5527 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5528 squash->rsi_uid, squash->rsi_gid);
5530 /* update current process's credentials
5531 * and FS capability */
5532 cred = prepare_creds();
5536 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5537 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5538 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5539 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5541 old_cred = override_creds(cred);
5544 rc = generic_permission(inode, mask);
5545 /* restore current process's credentials and FS capability */
5547 revert_creds(old_cred);
5552 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5553 ktime_us_delta(ktime_get(), kstart));
5558 /* -o localflock - only provides locally consistent flock locks */
5559 static const struct file_operations ll_file_operations = {
5560 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5561 # ifdef HAVE_SYNC_READ_WRITE
5562 .read = new_sync_read,
5563 .write = new_sync_write,
5565 .read_iter = ll_file_read_iter,
5566 .write_iter = ll_file_write_iter,
5567 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5568 .read = ll_file_read,
5569 .aio_read = ll_file_aio_read,
5570 .write = ll_file_write,
5571 .aio_write = ll_file_aio_write,
5572 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5573 .unlocked_ioctl = ll_file_ioctl,
5574 .open = ll_file_open,
5575 .release = ll_file_release,
5576 .mmap = ll_file_mmap,
5577 .llseek = ll_file_seek,
5578 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5579 .splice_read = generic_file_splice_read,
5581 .splice_read = pcc_file_splice_read,
5585 .fallocate = ll_fallocate,
5588 static const struct file_operations ll_file_operations_flock = {
5589 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5590 # ifdef HAVE_SYNC_READ_WRITE
5591 .read = new_sync_read,
5592 .write = new_sync_write,
5593 # endif /* HAVE_SYNC_READ_WRITE */
5594 .read_iter = ll_file_read_iter,
5595 .write_iter = ll_file_write_iter,
5596 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5597 .read = ll_file_read,
5598 .aio_read = ll_file_aio_read,
5599 .write = ll_file_write,
5600 .aio_write = ll_file_aio_write,
5601 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5602 .unlocked_ioctl = ll_file_ioctl,
5603 .open = ll_file_open,
5604 .release = ll_file_release,
5605 .mmap = ll_file_mmap,
5606 .llseek = ll_file_seek,
5607 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5608 .splice_read = generic_file_splice_read,
5610 .splice_read = pcc_file_splice_read,
5614 .flock = ll_file_flock,
5615 .lock = ll_file_flock,
5616 .fallocate = ll_fallocate,
5619 /* These are for -o noflock - to return ENOSYS on flock calls */
5620 static const struct file_operations ll_file_operations_noflock = {
5621 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5622 # ifdef HAVE_SYNC_READ_WRITE
5623 .read = new_sync_read,
5624 .write = new_sync_write,
5625 # endif /* HAVE_SYNC_READ_WRITE */
5626 .read_iter = ll_file_read_iter,
5627 .write_iter = ll_file_write_iter,
5628 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5629 .read = ll_file_read,
5630 .aio_read = ll_file_aio_read,
5631 .write = ll_file_write,
5632 .aio_write = ll_file_aio_write,
5633 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5634 .unlocked_ioctl = ll_file_ioctl,
5635 .open = ll_file_open,
5636 .release = ll_file_release,
5637 .mmap = ll_file_mmap,
5638 .llseek = ll_file_seek,
5639 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5640 .splice_read = generic_file_splice_read,
5642 .splice_read = pcc_file_splice_read,
5646 .flock = ll_file_noflock,
5647 .lock = ll_file_noflock,
5648 .fallocate = ll_fallocate,
5651 const struct inode_operations ll_file_inode_operations = {
5652 .setattr = ll_setattr,
5653 .getattr = ll_getattr,
5654 .permission = ll_inode_permission,
5655 #ifdef HAVE_IOP_XATTR
5656 .setxattr = ll_setxattr,
5657 .getxattr = ll_getxattr,
5658 .removexattr = ll_removexattr,
5660 .listxattr = ll_listxattr,
5661 .fiemap = ll_fiemap,
5662 .get_acl = ll_get_acl,
5663 #ifdef HAVE_IOP_SET_ACL
5664 .set_acl = ll_set_acl,
5668 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5670 const struct file_operations *fops = &ll_file_operations_noflock;
5672 if (test_bit(LL_SBI_FLOCK, sbi->ll_flags))
5673 fops = &ll_file_operations_flock;
5674 else if (test_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags))
5675 fops = &ll_file_operations;
5680 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5682 struct ll_inode_info *lli = ll_i2info(inode);
5683 struct cl_object *obj = lli->lli_clob;
5692 env = cl_env_get(&refcheck);
5694 RETURN(PTR_ERR(env));
5696 rc = cl_conf_set(env, lli->lli_clob, conf);
5700 if (conf->coc_opc == OBJECT_CONF_SET) {
5701 struct ldlm_lock *lock = conf->coc_lock;
5702 struct cl_layout cl = {
5706 LASSERT(lock != NULL);
5707 LASSERT(ldlm_has_layout(lock));
5709 /* it can only be allowed to match after layout is
5710 * applied to inode otherwise false layout would be
5711 * seen. Applying layout shoud happen before dropping
5712 * the intent lock. */
5713 ldlm_lock_allow_match(lock);
5715 rc = cl_object_layout_get(env, obj, &cl);
5720 DFID": layout version change: %u -> %u\n",
5721 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5723 ll_layout_version_set(lli, cl.cl_layout_gen);
5727 cl_env_put(env, &refcheck);
5729 RETURN(rc < 0 ? rc : 0);
5732 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5733 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5736 struct ll_sb_info *sbi = ll_i2sbi(inode);
5737 struct ptlrpc_request *req;
5744 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5745 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5746 lock->l_lvb_data, lock->l_lvb_len);
5748 if (lock->l_lvb_data != NULL)
5751 /* if layout lock was granted right away, the layout is returned
5752 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5753 * blocked and then granted via completion ast, we have to fetch
5754 * layout here. Please note that we can't use the LVB buffer in
5755 * completion AST because it doesn't have a large enough buffer */
5756 rc = ll_get_default_mdsize(sbi, &lmmsize);
5760 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5761 XATTR_NAME_LOV, lmmsize, &req);
5764 GOTO(out, rc = 0); /* empty layout */
5771 if (lmmsize == 0) /* empty layout */
5774 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5776 GOTO(out, rc = -EFAULT);
5778 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5779 if (lvbdata == NULL)
5780 GOTO(out, rc = -ENOMEM);
5782 memcpy(lvbdata, lmm, lmmsize);
5783 lock_res_and_lock(lock);
5784 if (unlikely(lock->l_lvb_data == NULL)) {
5785 lock->l_lvb_type = LVB_T_LAYOUT;
5786 lock->l_lvb_data = lvbdata;
5787 lock->l_lvb_len = lmmsize;
5790 unlock_res_and_lock(lock);
5793 OBD_FREE_LARGE(lvbdata, lmmsize);
5798 ptlrpc_req_finished(req);
5803 * Apply the layout to the inode. Layout lock is held and will be released
5806 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5807 struct inode *inode)
5809 struct ll_inode_info *lli = ll_i2info(inode);
5810 struct ll_sb_info *sbi = ll_i2sbi(inode);
5811 struct ldlm_lock *lock;
5812 struct cl_object_conf conf;
5815 bool wait_layout = false;
5818 LASSERT(lustre_handle_is_used(lockh));
5820 lock = ldlm_handle2lock(lockh);
5821 LASSERT(lock != NULL);
5823 if (!ldlm_has_layout(lock))
5824 GOTO(out, rc = -EAGAIN);
5826 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5827 PFID(&lli->lli_fid), inode);
5829 /* in case this is a caching lock and reinstate with new inode */
5830 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5832 lock_res_and_lock(lock);
5833 lvb_ready = ldlm_is_lvb_ready(lock);
5834 unlock_res_and_lock(lock);
5836 /* checking lvb_ready is racy but this is okay. The worst case is
5837 * that multi processes may configure the file on the same time. */
5841 rc = ll_layout_fetch(inode, lock);
5845 /* for layout lock, lmm is stored in lock's lvb.
5846 * lvb_data is immutable if the lock is held so it's safe to access it
5849 * set layout to file. Unlikely this will fail as old layout was
5850 * surely eliminated */
5851 memset(&conf, 0, sizeof conf);
5852 conf.coc_opc = OBJECT_CONF_SET;
5853 conf.coc_inode = inode;
5854 conf.coc_lock = lock;
5855 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5856 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5857 rc = ll_layout_conf(inode, &conf);
5859 /* refresh layout failed, need to wait */
5860 wait_layout = rc == -EBUSY;
5863 LDLM_LOCK_PUT(lock);
5864 ldlm_lock_decref(lockh, mode);
5866 /* wait for IO to complete if it's still being used. */
5868 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5869 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5871 memset(&conf, 0, sizeof conf);
5872 conf.coc_opc = OBJECT_CONF_WAIT;
5873 conf.coc_inode = inode;
5874 rc = ll_layout_conf(inode, &conf);
5878 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5879 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5885 * Issue layout intent RPC to MDS.
5886 * \param inode [in] file inode
5887 * \param intent [in] layout intent
5889 * \retval 0 on success
5890 * \retval < 0 error code
5892 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5894 struct ll_inode_info *lli = ll_i2info(inode);
5895 struct ll_sb_info *sbi = ll_i2sbi(inode);
5896 struct md_op_data *op_data;
5897 struct lookup_intent it;
5898 struct ptlrpc_request *req;
5902 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5903 0, 0, LUSTRE_OPC_ANY, NULL);
5904 if (IS_ERR(op_data))
5905 RETURN(PTR_ERR(op_data));
5907 op_data->op_data = intent;
5908 op_data->op_data_size = sizeof(*intent);
5910 memset(&it, 0, sizeof(it));
5911 it.it_op = IT_LAYOUT;
5912 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5913 intent->li_opc == LAYOUT_INTENT_TRUNC)
5914 it.it_flags = FMODE_WRITE;
5916 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5917 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5919 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5920 &ll_md_blocking_ast, 0);
5921 if (it.it_request != NULL)
5922 ptlrpc_req_finished(it.it_request);
5923 it.it_request = NULL;
5925 ll_finish_md_op_data(op_data);
5927 /* set lock data in case this is a new lock */
5929 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5931 ll_intent_drop_lock(&it);
5937 * This function checks if there exists a LAYOUT lock on the client side,
5938 * or enqueues it if it doesn't have one in cache.
5940 * This function will not hold layout lock so it may be revoked any time after
5941 * this function returns. Any operations depend on layout should be redone
5944 * This function should be called before lov_io_init() to get an uptodate
5945 * layout version, the caller should save the version number and after IO
5946 * is finished, this function should be called again to verify that layout
5947 * is not changed during IO time.
5949 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5951 struct ll_inode_info *lli = ll_i2info(inode);
5952 struct ll_sb_info *sbi = ll_i2sbi(inode);
5953 struct lustre_handle lockh;
5954 struct layout_intent intent = {
5955 .li_opc = LAYOUT_INTENT_ACCESS,
5957 enum ldlm_mode mode;
5961 *gen = ll_layout_version_get(lli);
5962 if (!test_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags) ||
5963 *gen != CL_LAYOUT_GEN_NONE)
5967 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5968 LASSERT(S_ISREG(inode->i_mode));
5970 /* take layout lock mutex to enqueue layout lock exclusively. */
5971 mutex_lock(&lli->lli_layout_mutex);
5974 /* mostly layout lock is caching on the local side, so try to
5975 * match it before grabbing layout lock mutex. */
5976 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5977 LCK_CR | LCK_CW | LCK_PR |
5979 if (mode != 0) { /* hit cached lock */
5980 rc = ll_layout_lock_set(&lockh, mode, inode);
5986 rc = ll_layout_intent(inode, &intent);
5992 *gen = ll_layout_version_get(lli);
5993 mutex_unlock(&lli->lli_layout_mutex);
5999 * Issue layout intent RPC indicating where in a file an IO is about to write.
6001 * \param[in] inode file inode.
6002 * \param[in] ext write range with start offset of fille in bytes where
6003 * an IO is about to write, and exclusive end offset in
6006 * \retval 0 on success
6007 * \retval < 0 error code
6009 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
6010 struct lu_extent *ext)
6012 struct layout_intent intent = {
6014 .li_extent.e_start = ext->e_start,
6015 .li_extent.e_end = ext->e_end,
6020 rc = ll_layout_intent(inode, &intent);
6026 * This function send a restore request to the MDT
6028 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
6030 struct hsm_user_request *hur;
6034 len = sizeof(struct hsm_user_request) +
6035 sizeof(struct hsm_user_item);
6036 OBD_ALLOC(hur, len);
6040 hur->hur_request.hr_action = HUA_RESTORE;
6041 hur->hur_request.hr_archive_id = 0;
6042 hur->hur_request.hr_flags = 0;
6043 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
6044 sizeof(hur->hur_user_item[0].hui_fid));
6045 hur->hur_user_item[0].hui_extent.offset = offset;
6046 hur->hur_user_item[0].hui_extent.length = length;
6047 hur->hur_request.hr_itemcount = 1;
6048 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,