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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "../include/lustre_dlm.h"
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/mount.h>
45 #include "../include/lustre/ll_fiemap.h"
46 #include "../include/lustre/lustre_ioctl.h"
48 #include "../include/cl_object.h"
49 #include "llite_internal.h"
52 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
54 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
57 static enum llioc_iter
58 ll_iocontrol_call(struct inode *inode, struct file *file,
59 unsigned int cmd, unsigned long arg, int *rcp);
61 static struct ll_file_data *ll_file_data_get(void)
63 struct ll_file_data *fd;
65 fd = kmem_cache_zalloc(ll_file_data_slab, GFP_NOFS);
68 fd->fd_write_failed = false;
72 static void ll_file_data_put(struct ll_file_data *fd)
75 kmem_cache_free(ll_file_data_slab, fd);
78 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
79 struct lustre_handle *fh)
81 op_data->op_fid1 = ll_i2info(inode)->lli_fid;
82 op_data->op_attr.ia_mode = inode->i_mode;
83 op_data->op_attr.ia_atime = inode->i_atime;
84 op_data->op_attr.ia_mtime = inode->i_mtime;
85 op_data->op_attr.ia_ctime = inode->i_ctime;
86 op_data->op_attr.ia_size = i_size_read(inode);
87 op_data->op_attr_blocks = inode->i_blocks;
88 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
89 op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
91 op_data->op_handle = *fh;
93 if (ll_i2info(inode)->lli_flags & LLIF_DATA_MODIFIED)
94 op_data->op_bias |= MDS_DATA_MODIFIED;
98 * Closes the IO epoch and packs all the attributes into @op_data for
101 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
102 struct obd_client_handle *och)
104 op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
105 ATTR_MTIME | ATTR_MTIME_SET |
106 ATTR_CTIME | ATTR_CTIME_SET;
108 if (!(och->och_flags & FMODE_WRITE))
111 if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
112 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
114 ll_ioepoch_close(inode, op_data, &och, 0);
117 ll_pack_inode2opdata(inode, op_data, &och->och_fh);
118 ll_prep_md_op_data(op_data, inode, NULL, NULL,
119 0, 0, LUSTRE_OPC_ANY, NULL);
122 static int ll_close_inode_openhandle(struct obd_export *md_exp,
124 struct obd_client_handle *och,
125 const __u64 *data_version)
127 struct obd_export *exp = ll_i2mdexp(inode);
128 struct md_op_data *op_data;
129 struct ptlrpc_request *req = NULL;
130 struct obd_device *obd = class_exp2obd(exp);
136 * XXX: in case of LMV, is this correct to access
139 CERROR("Invalid MDC connection handle %#llx\n",
140 ll_i2mdexp(inode)->exp_handle.h_cookie);
145 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
147 /* XXX We leak openhandle and request here. */
152 ll_prepare_close(inode, op_data, och);
154 /* Pass in data_version implies release. */
155 op_data->op_bias |= MDS_HSM_RELEASE;
156 op_data->op_data_version = *data_version;
157 op_data->op_lease_handle = och->och_lease_handle;
158 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
160 epoch_close = op_data->op_flags & MF_EPOCH_CLOSE;
161 rc = md_close(md_exp, op_data, och->och_mod, &req);
163 /* This close must have the epoch closed. */
164 LASSERT(epoch_close);
165 /* MDS has instructed us to obtain Size-on-MDS attribute from
166 * OSTs and send setattr to back to MDS.
168 rc = ll_som_update(inode, op_data);
170 CERROR("%s: inode "DFID" mdc Size-on-MDS update failed: rc = %d\n",
171 ll_i2mdexp(inode)->exp_obd->obd_name,
172 PFID(ll_inode2fid(inode)), rc);
176 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
177 ll_i2mdexp(inode)->exp_obd->obd_name,
178 PFID(ll_inode2fid(inode)), rc);
181 /* DATA_MODIFIED flag was successfully sent on close, cancel data
184 if (rc == 0 && (op_data->op_bias & MDS_DATA_MODIFIED)) {
185 struct ll_inode_info *lli = ll_i2info(inode);
187 spin_lock(&lli->lli_lock);
188 lli->lli_flags &= ~LLIF_DATA_MODIFIED;
189 spin_unlock(&lli->lli_lock);
192 if (rc == 0 && op_data->op_bias & MDS_HSM_RELEASE) {
193 struct mdt_body *body;
195 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
196 if (!(body->mbo_valid & OBD_MD_FLRELEASED))
200 ll_finish_md_op_data(op_data);
203 if (exp_connect_som(exp) && !epoch_close &&
204 S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
205 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
207 md_clear_open_replay_data(md_exp, och);
208 /* Free @och if it is not waiting for DONE_WRITING. */
209 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
212 if (req) /* This is close request */
213 ptlrpc_req_finished(req);
217 int ll_md_real_close(struct inode *inode, fmode_t fmode)
219 struct ll_inode_info *lli = ll_i2info(inode);
220 struct obd_client_handle **och_p;
221 struct obd_client_handle *och;
225 if (fmode & FMODE_WRITE) {
226 och_p = &lli->lli_mds_write_och;
227 och_usecount = &lli->lli_open_fd_write_count;
228 } else if (fmode & FMODE_EXEC) {
229 och_p = &lli->lli_mds_exec_och;
230 och_usecount = &lli->lli_open_fd_exec_count;
232 LASSERT(fmode & FMODE_READ);
233 och_p = &lli->lli_mds_read_och;
234 och_usecount = &lli->lli_open_fd_read_count;
237 mutex_lock(&lli->lli_och_mutex);
238 if (*och_usecount > 0) {
239 /* There are still users of this handle, so skip
242 mutex_unlock(&lli->lli_och_mutex);
248 mutex_unlock(&lli->lli_och_mutex);
251 /* There might be a race and this handle may already
254 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
261 static int ll_md_close(struct obd_export *md_exp, struct inode *inode,
264 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
265 struct ll_inode_info *lli = ll_i2info(inode);
267 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
268 struct lustre_handle lockh;
269 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_OPEN} };
272 /* clear group lock, if present */
273 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
274 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
276 if (fd->fd_lease_och) {
279 /* Usually the lease is not released when the
280 * application crashed, we need to release here.
282 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
283 CDEBUG(rc ? D_ERROR : D_INODE,
284 "Clean up lease " DFID " %d/%d\n",
285 PFID(&lli->lli_fid), rc, lease_broken);
287 fd->fd_lease_och = NULL;
291 rc = ll_close_inode_openhandle(md_exp, inode, fd->fd_och, NULL);
296 /* Let's see if we have good enough OPEN lock on the file and if
297 * we can skip talking to MDS
300 mutex_lock(&lli->lli_och_mutex);
301 if (fd->fd_omode & FMODE_WRITE) {
303 LASSERT(lli->lli_open_fd_write_count);
304 lli->lli_open_fd_write_count--;
305 } else if (fd->fd_omode & FMODE_EXEC) {
307 LASSERT(lli->lli_open_fd_exec_count);
308 lli->lli_open_fd_exec_count--;
311 LASSERT(lli->lli_open_fd_read_count);
312 lli->lli_open_fd_read_count--;
314 mutex_unlock(&lli->lli_och_mutex);
316 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
317 LDLM_IBITS, &policy, lockmode, &lockh))
318 rc = ll_md_real_close(inode, fd->fd_omode);
321 LUSTRE_FPRIVATE(file) = NULL;
322 ll_file_data_put(fd);
327 /* While this returns an error code, fput() the caller does not, so we need
328 * to make every effort to clean up all of our state here. Also, applications
329 * rarely check close errors and even if an error is returned they will not
330 * re-try the close call.
332 int ll_file_release(struct inode *inode, struct file *file)
334 struct ll_file_data *fd;
335 struct ll_sb_info *sbi = ll_i2sbi(inode);
336 struct ll_inode_info *lli = ll_i2info(inode);
339 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
340 PFID(ll_inode2fid(inode)), inode);
342 if (!is_root_inode(inode))
343 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
344 fd = LUSTRE_FPRIVATE(file);
347 /* The last ref on @file, maybe not be the owner pid of statahead,
348 * because parent and child process can share the same file handle.
350 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
351 ll_deauthorize_statahead(inode, fd);
353 if (is_root_inode(inode)) {
354 LUSTRE_FPRIVATE(file) = NULL;
355 ll_file_data_put(fd);
359 if (!S_ISDIR(inode->i_mode)) {
361 lov_read_and_clear_async_rc(lli->lli_clob);
362 lli->lli_async_rc = 0;
365 rc = ll_md_close(sbi->ll_md_exp, inode, file);
367 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
368 libcfs_debug_dumplog();
373 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
374 struct lookup_intent *itp)
376 struct inode *inode = d_inode(de);
377 struct ll_sb_info *sbi = ll_i2sbi(inode);
378 struct dentry *parent = de->d_parent;
379 const char *name = NULL;
380 struct md_op_data *op_data;
381 struct ptlrpc_request *req = NULL;
385 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
388 * if server supports open-by-fid, or file name is invalid, don't pack
389 * name in open request
391 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
392 lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
393 name = de->d_name.name;
394 len = de->d_name.len;
397 op_data = ll_prep_md_op_data(NULL, d_inode(parent), inode, name, len,
398 O_RDWR, LUSTRE_OPC_ANY, NULL);
400 return PTR_ERR(op_data);
401 op_data->op_data = lmm;
402 op_data->op_data_size = lmmsize;
404 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
405 &ll_md_blocking_ast, 0);
406 ll_finish_md_op_data(op_data);
408 /* reason for keep own exit path - don`t flood log
409 * with messages with -ESTALE errors.
411 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
412 it_open_error(DISP_OPEN_OPEN, itp))
414 ll_release_openhandle(inode, itp);
418 if (it_disposition(itp, DISP_LOOKUP_NEG)) {
423 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
424 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
425 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
429 rc = ll_prep_inode(&inode, req, NULL, itp);
430 if (!rc && itp->it_lock_mode)
431 ll_set_lock_data(sbi->ll_md_exp, inode, itp, NULL);
434 ptlrpc_req_finished(req);
435 ll_intent_drop_lock(itp);
441 * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
442 * not believe attributes if a few ioepoch holders exist. Attributes for
443 * previous ioepoch if new one is opened are also skipped by MDS.
445 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
447 if (ioepoch && lli->lli_ioepoch != ioepoch) {
448 lli->lli_ioepoch = ioepoch;
449 CDEBUG(D_INODE, "Epoch %llu opened on "DFID"\n",
450 ioepoch, PFID(&lli->lli_fid));
454 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
455 struct obd_client_handle *och)
457 struct mdt_body *body;
459 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
460 och->och_fh = body->mbo_handle;
461 och->och_fid = body->mbo_fid1;
462 och->och_lease_handle.cookie = it->it_lock_handle;
463 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
464 och->och_flags = it->it_flags;
466 return md_set_open_replay_data(md_exp, och, it);
469 static int ll_local_open(struct file *file, struct lookup_intent *it,
470 struct ll_file_data *fd, struct obd_client_handle *och)
472 struct inode *inode = file_inode(file);
473 struct ll_inode_info *lli = ll_i2info(inode);
475 LASSERT(!LUSTRE_FPRIVATE(file));
480 struct mdt_body *body;
483 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
487 body = req_capsule_server_get(&it->it_request->rq_pill,
489 ll_ioepoch_open(lli, body->mbo_ioepoch);
492 LUSTRE_FPRIVATE(file) = fd;
493 ll_readahead_init(inode, &fd->fd_ras);
494 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
496 /* ll_cl_context initialize */
497 rwlock_init(&fd->fd_lock);
498 INIT_LIST_HEAD(&fd->fd_lccs);
503 /* Open a file, and (for the very first open) create objects on the OSTs at
504 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
505 * creation or open until ll_lov_setstripe() ioctl is called.
507 * If we already have the stripe MD locally then we don't request it in
508 * md_open(), by passing a lmm_size = 0.
510 * It is up to the application to ensure no other processes open this file
511 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
512 * used. We might be able to avoid races of that sort by getting lli_open_sem
513 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
514 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
516 int ll_file_open(struct inode *inode, struct file *file)
518 struct ll_inode_info *lli = ll_i2info(inode);
519 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
520 .it_flags = file->f_flags };
521 struct obd_client_handle **och_p = NULL;
522 __u64 *och_usecount = NULL;
523 struct ll_file_data *fd;
526 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
527 PFID(ll_inode2fid(inode)), inode, file->f_flags);
529 it = file->private_data; /* XXX: compat macro */
530 file->private_data = NULL; /* prevent ll_local_open assertion */
532 fd = ll_file_data_get();
539 if (S_ISDIR(inode->i_mode))
540 ll_authorize_statahead(inode, fd);
542 if (is_root_inode(inode)) {
543 LUSTRE_FPRIVATE(file) = fd;
547 if (!it || !it->it_disposition) {
548 /* Convert f_flags into access mode. We cannot use file->f_mode,
549 * because everything but O_ACCMODE mask was stripped from
552 if ((oit.it_flags + 1) & O_ACCMODE)
554 if (file->f_flags & O_TRUNC)
555 oit.it_flags |= FMODE_WRITE;
557 /* kernel only call f_op->open in dentry_open. filp_open calls
558 * dentry_open after call to open_namei that checks permissions.
559 * Only nfsd_open call dentry_open directly without checking
560 * permissions and because of that this code below is safe.
562 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
563 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
565 /* We do not want O_EXCL here, presumably we opened the file
566 * already? XXX - NFS implications?
568 oit.it_flags &= ~O_EXCL;
570 /* bug20584, if "it_flags" contains O_CREAT, the file will be
571 * created if necessary, then "IT_CREAT" should be set to keep
574 if (oit.it_flags & O_CREAT)
575 oit.it_op |= IT_CREAT;
581 /* Let's see if we have file open on MDS already. */
582 if (it->it_flags & FMODE_WRITE) {
583 och_p = &lli->lli_mds_write_och;
584 och_usecount = &lli->lli_open_fd_write_count;
585 } else if (it->it_flags & FMODE_EXEC) {
586 och_p = &lli->lli_mds_exec_och;
587 och_usecount = &lli->lli_open_fd_exec_count;
589 och_p = &lli->lli_mds_read_och;
590 och_usecount = &lli->lli_open_fd_read_count;
593 mutex_lock(&lli->lli_och_mutex);
594 if (*och_p) { /* Open handle is present */
595 if (it_disposition(it, DISP_OPEN_OPEN)) {
596 /* Well, there's extra open request that we do not need,
597 * let's close it somehow. This will decref request.
599 rc = it_open_error(DISP_OPEN_OPEN, it);
601 mutex_unlock(&lli->lli_och_mutex);
605 ll_release_openhandle(inode, it);
609 rc = ll_local_open(file, it, fd, NULL);
612 mutex_unlock(&lli->lli_och_mutex);
616 LASSERT(*och_usecount == 0);
617 if (!it->it_disposition) {
618 /* We cannot just request lock handle now, new ELC code
619 * means that one of other OPEN locks for this file
620 * could be cancelled, and since blocking ast handler
621 * would attempt to grab och_mutex as well, that would
622 * result in a deadlock
624 mutex_unlock(&lli->lli_och_mutex);
626 * Normally called under two situations:
628 * 2. revalidate with IT_OPEN (revalidate doesn't
629 * execute this intent any more).
631 * Always fetch MDS_OPEN_LOCK if this is not setstripe.
633 * Always specify MDS_OPEN_BY_FID because we don't want
634 * to get file with different fid.
636 it->it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID;
637 rc = ll_intent_file_open(file->f_path.dentry, NULL, 0, it);
643 *och_p = kzalloc(sizeof(struct obd_client_handle), GFP_NOFS);
651 /* md_intent_lock() didn't get a request ref if there was an
652 * open error, so don't do cleanup on the request here
655 /* XXX (green): Should not we bail out on any error here, not
658 rc = it_open_error(DISP_OPEN_OPEN, it);
662 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
663 "inode %p: disposition %x, status %d\n", inode,
664 it_disposition(it, ~0), it->it_status);
666 rc = ll_local_open(file, it, fd, *och_p);
670 mutex_unlock(&lli->lli_och_mutex);
673 /* Must do this outside lli_och_mutex lock to prevent deadlock where
674 * different kind of OPEN lock for this same inode gets cancelled
677 if (!S_ISREG(inode->i_mode))
680 if (!lli->lli_has_smd &&
681 (cl_is_lov_delay_create(file->f_flags) ||
682 (file->f_mode & FMODE_WRITE) == 0)) {
683 CDEBUG(D_INODE, "object creation was delayed\n");
686 cl_lov_delay_create_clear(&file->f_flags);
691 if (och_p && *och_p) {
696 mutex_unlock(&lli->lli_och_mutex);
699 if (lli->lli_opendir_key == fd)
700 ll_deauthorize_statahead(inode, fd);
702 ll_file_data_put(fd);
704 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
707 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
708 ptlrpc_req_finished(it->it_request);
709 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
715 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
716 struct ldlm_lock_desc *desc,
717 void *data, int flag)
720 struct lustre_handle lockh;
723 case LDLM_CB_BLOCKING:
724 ldlm_lock2handle(lock, &lockh);
725 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
727 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
731 case LDLM_CB_CANCELING:
739 * Acquire a lease and open the file.
741 static struct obd_client_handle *
742 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
745 struct lookup_intent it = { .it_op = IT_OPEN };
746 struct ll_sb_info *sbi = ll_i2sbi(inode);
747 struct md_op_data *op_data;
748 struct ptlrpc_request *req = NULL;
749 struct lustre_handle old_handle = { 0 };
750 struct obd_client_handle *och = NULL;
754 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
755 return ERR_PTR(-EINVAL);
758 struct ll_inode_info *lli = ll_i2info(inode);
759 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
760 struct obd_client_handle **och_p;
763 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
764 return ERR_PTR(-EPERM);
766 /* Get the openhandle of the file */
768 mutex_lock(&lli->lli_och_mutex);
769 if (fd->fd_lease_och) {
770 mutex_unlock(&lli->lli_och_mutex);
775 if (file->f_mode & FMODE_WRITE) {
776 LASSERT(lli->lli_mds_write_och);
777 och_p = &lli->lli_mds_write_och;
778 och_usecount = &lli->lli_open_fd_write_count;
780 LASSERT(lli->lli_mds_read_och);
781 och_p = &lli->lli_mds_read_och;
782 och_usecount = &lli->lli_open_fd_read_count;
784 if (*och_usecount == 1) {
791 mutex_unlock(&lli->lli_och_mutex);
792 if (rc < 0) /* more than 1 opener */
796 old_handle = fd->fd_och->och_fh;
799 och = kzalloc(sizeof(*och), GFP_NOFS);
801 return ERR_PTR(-ENOMEM);
803 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
804 LUSTRE_OPC_ANY, NULL);
805 if (IS_ERR(op_data)) {
806 rc = PTR_ERR(op_data);
810 /* To tell the MDT this openhandle is from the same owner */
811 op_data->op_handle = old_handle;
813 it.it_flags = fmode | open_flags;
814 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
815 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
816 &ll_md_blocking_lease_ast,
817 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
818 * it can be cancelled which may mislead applications that the lease is
820 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
821 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
822 * doesn't deal with openhandle, so normal openhandle will be leaked.
824 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
825 ll_finish_md_op_data(op_data);
826 ptlrpc_req_finished(req);
830 if (it_disposition(&it, DISP_LOOKUP_NEG)) {
835 rc = it_open_error(DISP_OPEN_OPEN, &it);
839 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
840 ll_och_fill(sbi->ll_md_exp, &it, och);
842 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */ {
847 /* already get lease, handle lease lock */
848 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
849 if (it.it_lock_mode == 0 ||
850 it.it_lock_bits != MDS_INODELOCK_OPEN) {
851 /* open lock must return for lease */
852 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
853 PFID(ll_inode2fid(inode)), it.it_lock_mode,
859 ll_intent_release(&it);
863 /* Cancel open lock */
864 if (it.it_lock_mode != 0) {
865 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
868 och->och_lease_handle.cookie = 0ULL;
870 rc2 = ll_close_inode_openhandle(sbi->ll_md_exp, inode, och, NULL);
872 CERROR("%s: error closing file "DFID": %d\n",
873 ll_get_fsname(inode->i_sb, NULL, 0),
874 PFID(&ll_i2info(inode)->lli_fid), rc2);
875 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
877 ll_intent_release(&it);
884 * Release lease and close the file.
885 * It will check if the lease has ever broken.
887 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
890 struct ldlm_lock *lock;
891 bool cancelled = true;
893 lock = ldlm_handle2lock(&och->och_lease_handle);
895 lock_res_and_lock(lock);
896 cancelled = ldlm_is_cancel(lock);
897 unlock_res_and_lock(lock);
901 CDEBUG(D_INODE, "lease for " DFID " broken? %d\n",
902 PFID(&ll_i2info(inode)->lli_fid), cancelled);
905 ldlm_cli_cancel(&och->och_lease_handle, 0);
907 *lease_broken = cancelled;
909 return ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
913 /* Fills the obdo with the attributes for the lsm */
914 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
915 struct obdo *obdo, __u64 ioepoch, int dv_flags)
917 struct ptlrpc_request_set *set;
918 struct obd_info oinfo = { };
925 oinfo.oi_oa->o_oi = lsm->lsm_oi;
926 oinfo.oi_oa->o_mode = S_IFREG;
927 oinfo.oi_oa->o_ioepoch = ioepoch;
928 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
929 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
930 OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
931 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
932 OBD_MD_FLGROUP | OBD_MD_FLEPOCH |
933 OBD_MD_FLDATAVERSION;
934 if (dv_flags & (LL_DV_WR_FLUSH | LL_DV_RD_FLUSH)) {
935 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
936 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
937 if (dv_flags & LL_DV_WR_FLUSH)
938 oinfo.oi_oa->o_flags |= OBD_FL_FLUSH;
941 set = ptlrpc_prep_set();
943 CERROR("cannot allocate ptlrpc set: rc = %d\n", -ENOMEM);
946 rc = obd_getattr_async(exp, &oinfo, set);
948 rc = ptlrpc_set_wait(set);
949 ptlrpc_set_destroy(set);
952 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
953 OBD_MD_FLATIME | OBD_MD_FLMTIME |
954 OBD_MD_FLCTIME | OBD_MD_FLSIZE |
955 OBD_MD_FLDATAVERSION | OBD_MD_FLFLAGS);
956 if (dv_flags & LL_DV_WR_FLUSH &&
957 !(oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
958 oinfo.oi_oa->o_flags & OBD_FL_FLUSH))
965 * Performs the getattr on the inode and updates its fields.
966 * If @sync != 0, perform the getattr under the server-side lock.
968 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
969 __u64 ioepoch, int sync)
971 struct lov_stripe_md *lsm;
974 lsm = ccc_inode_lsm_get(inode);
975 rc = ll_lsm_getattr(lsm, ll_i2dtexp(inode),
976 obdo, ioepoch, sync ? LL_DV_RD_FLUSH : 0);
978 struct ost_id *oi = lsm ? &lsm->lsm_oi : &obdo->o_oi;
980 obdo_refresh_inode(inode, obdo, obdo->o_valid);
981 CDEBUG(D_INODE, "objid " DOSTID " size %llu, blocks %llu, blksize %lu\n",
982 POSTID(oi), i_size_read(inode),
983 (unsigned long long)inode->i_blocks,
984 1UL << inode->i_blkbits);
986 ccc_inode_lsm_put(inode, lsm);
990 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
992 struct ll_inode_info *lli = ll_i2info(inode);
993 struct cl_object *obj = lli->lli_clob;
994 struct cl_attr *attr = vvp_env_thread_attr(env);
1000 ll_inode_size_lock(inode);
1002 /* merge timestamps the most recently obtained from mds with
1003 * timestamps obtained from osts
1005 LTIME_S(inode->i_atime) = lli->lli_atime;
1006 LTIME_S(inode->i_mtime) = lli->lli_mtime;
1007 LTIME_S(inode->i_ctime) = lli->lli_ctime;
1009 mtime = LTIME_S(inode->i_mtime);
1010 atime = LTIME_S(inode->i_atime);
1011 ctime = LTIME_S(inode->i_ctime);
1013 cl_object_attr_lock(obj);
1014 rc = cl_object_attr_get(env, obj, attr);
1015 cl_object_attr_unlock(obj);
1018 goto out_size_unlock;
1020 if (atime < attr->cat_atime)
1021 atime = attr->cat_atime;
1023 if (ctime < attr->cat_ctime)
1024 ctime = attr->cat_ctime;
1026 if (mtime < attr->cat_mtime)
1027 mtime = attr->cat_mtime;
1029 CDEBUG(D_VFSTRACE, DFID " updating i_size %llu\n",
1030 PFID(&lli->lli_fid), attr->cat_size);
1032 i_size_write(inode, attr->cat_size);
1034 inode->i_blocks = attr->cat_blocks;
1036 LTIME_S(inode->i_mtime) = mtime;
1037 LTIME_S(inode->i_atime) = atime;
1038 LTIME_S(inode->i_ctime) = ctime;
1041 ll_inode_size_unlock(inode);
1046 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
1049 struct obdo obdo = { 0 };
1052 rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, &obdo, 0, 0);
1054 st->st_size = obdo.o_size;
1055 st->st_blocks = obdo.o_blocks;
1056 st->st_mtime = obdo.o_mtime;
1057 st->st_atime = obdo.o_atime;
1058 st->st_ctime = obdo.o_ctime;
1063 static bool file_is_noatime(const struct file *file)
1065 const struct vfsmount *mnt = file->f_path.mnt;
1066 const struct inode *inode = file_inode(file);
1068 /* Adapted from file_accessed() and touch_atime().*/
1069 if (file->f_flags & O_NOATIME)
1072 if (inode->i_flags & S_NOATIME)
1075 if (IS_NOATIME(inode))
1078 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1081 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1084 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1090 void ll_io_init(struct cl_io *io, const struct file *file, int write)
1092 struct inode *inode = file_inode(file);
1094 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1096 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1097 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC ||
1098 file->f_flags & O_DIRECT ||
1101 io->ci_obj = ll_i2info(inode)->lli_clob;
1102 io->ci_lockreq = CILR_MAYBE;
1103 if (ll_file_nolock(file)) {
1104 io->ci_lockreq = CILR_NEVER;
1105 io->ci_no_srvlock = 1;
1106 } else if (file->f_flags & O_APPEND) {
1107 io->ci_lockreq = CILR_MANDATORY;
1110 io->ci_noatime = file_is_noatime(file);
1114 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1115 struct file *file, enum cl_io_type iot,
1116 loff_t *ppos, size_t count)
1118 struct ll_inode_info *lli = ll_i2info(file_inode(file));
1119 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1120 struct range_lock range;
1124 CDEBUG(D_VFSTRACE, "file: %pD, type: %d ppos: %llu, count: %zu\n",
1125 file, iot, *ppos, count);
1128 io = vvp_env_thread_io(env);
1129 ll_io_init(io, file, iot == CIT_WRITE);
1131 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1132 struct vvp_io *vio = vvp_env_io(env);
1133 bool range_locked = false;
1135 if (file->f_flags & O_APPEND)
1136 range_lock_init(&range, 0, LUSTRE_EOF);
1138 range_lock_init(&range, *ppos, *ppos + count - 1);
1140 vio->vui_fd = LUSTRE_FPRIVATE(file);
1141 vio->vui_iter = args->u.normal.via_iter;
1142 vio->vui_iocb = args->u.normal.via_iocb;
1144 * Direct IO reads must also take range lock,
1145 * or multiple reads will try to work on the same pages
1146 * See LU-6227 for details.
1148 if (((iot == CIT_WRITE) ||
1149 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1150 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1151 CDEBUG(D_VFSTRACE, "Range lock [%llu, %llu]\n",
1152 range.rl_node.in_extent.start,
1153 range.rl_node.in_extent.end);
1154 result = range_lock(&lli->lli_write_tree,
1159 range_locked = true;
1161 down_read(&lli->lli_trunc_sem);
1162 ll_cl_add(file, env, io);
1163 result = cl_io_loop(env, io);
1164 ll_cl_remove(file, env);
1165 up_read(&lli->lli_trunc_sem);
1167 CDEBUG(D_VFSTRACE, "Range unlock [%llu, %llu]\n",
1168 range.rl_node.in_extent.start,
1169 range.rl_node.in_extent.end);
1170 range_unlock(&lli->lli_write_tree, &range);
1173 /* cl_io_rw_init() handled IO */
1174 result = io->ci_result;
1177 if (io->ci_nob > 0) {
1178 result = io->ci_nob;
1179 *ppos = io->u.ci_wr.wr.crw_pos;
1183 cl_io_fini(env, io);
1184 /* If any bit been read/written (result != 0), we just return
1185 * short read/write instead of restart io.
1187 if ((result == 0 || result == -ENODATA) && io->ci_need_restart) {
1188 CDEBUG(D_VFSTRACE, "Restart %s on %pD from %lld, count:%zu\n",
1189 iot == CIT_READ ? "read" : "write",
1190 file, *ppos, count);
1191 LASSERTF(io->ci_nob == 0, "%zd\n", io->ci_nob);
1195 if (iot == CIT_READ) {
1197 ll_stats_ops_tally(ll_i2sbi(file_inode(file)),
1198 LPROC_LL_READ_BYTES, result);
1199 } else if (iot == CIT_WRITE) {
1201 ll_stats_ops_tally(ll_i2sbi(file_inode(file)),
1202 LPROC_LL_WRITE_BYTES, result);
1203 fd->fd_write_failed = false;
1204 } else if (result != -ERESTARTSYS) {
1205 fd->fd_write_failed = true;
1208 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1213 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1216 struct vvp_io_args *args;
1220 env = cl_env_get(&refcheck);
1222 return PTR_ERR(env);
1224 args = ll_env_args(env);
1225 args->u.normal.via_iter = to;
1226 args->u.normal.via_iocb = iocb;
1228 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1229 &iocb->ki_pos, iov_iter_count(to));
1230 cl_env_put(env, &refcheck);
1235 * Write to a file (through the page cache).
1237 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1240 struct vvp_io_args *args;
1244 env = cl_env_get(&refcheck);
1246 return PTR_ERR(env);
1248 args = ll_env_args(env);
1249 args->u.normal.via_iter = from;
1250 args->u.normal.via_iocb = iocb;
1252 result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1253 &iocb->ki_pos, iov_iter_count(from));
1254 cl_env_put(env, &refcheck);
1258 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
1259 __u64 flags, struct lov_user_md *lum,
1262 struct lov_stripe_md *lsm = NULL;
1263 struct lookup_intent oit = {
1265 .it_flags = flags | MDS_OPEN_BY_FID,
1269 lsm = ccc_inode_lsm_get(inode);
1271 ccc_inode_lsm_put(inode, lsm);
1272 CDEBUG(D_IOCTL, "stripe already exists for inode "DFID"\n",
1273 PFID(ll_inode2fid(inode)));
1278 ll_inode_size_lock(inode);
1279 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
1286 ll_release_openhandle(inode, &oit);
1289 ll_inode_size_unlock(inode);
1290 ll_intent_release(&oit);
1291 ccc_inode_lsm_put(inode, lsm);
1296 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1297 struct lov_mds_md **lmmp, int *lmm_size,
1298 struct ptlrpc_request **request)
1300 struct ll_sb_info *sbi = ll_i2sbi(inode);
1301 struct mdt_body *body;
1302 struct lov_mds_md *lmm = NULL;
1303 struct ptlrpc_request *req = NULL;
1304 struct md_op_data *op_data;
1307 rc = ll_get_default_mdsize(sbi, &lmmsize);
1311 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1312 strlen(filename), lmmsize,
1313 LUSTRE_OPC_ANY, NULL);
1314 if (IS_ERR(op_data))
1315 return PTR_ERR(op_data);
1317 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1318 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1319 ll_finish_md_op_data(op_data);
1321 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
1326 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1328 lmmsize = body->mbo_eadatasize;
1330 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1336 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1338 if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1339 (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1345 * This is coming from the MDS, so is probably in
1346 * little endian. We convert it to host endian before
1347 * passing it to userspace.
1349 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
1352 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
1353 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
1356 /* if function called for directory - we should
1357 * avoid swab not existent lsm objects
1359 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1360 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1361 if (S_ISREG(body->mbo_mode))
1362 lustre_swab_lov_user_md_objects(
1363 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1365 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1366 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1367 if (S_ISREG(body->mbo_mode))
1368 lustre_swab_lov_user_md_objects(
1369 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1376 *lmm_size = lmmsize;
1381 static int ll_lov_setea(struct inode *inode, struct file *file,
1384 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1385 struct lov_user_md *lump;
1386 int lum_size = sizeof(struct lov_user_md) +
1387 sizeof(struct lov_user_ost_data);
1390 if (!capable(CFS_CAP_SYS_ADMIN))
1393 lump = libcfs_kvzalloc(lum_size, GFP_NOFS);
1397 if (copy_from_user(lump, (struct lov_user_md __user *)arg, lum_size)) {
1402 rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, lump,
1404 cl_lov_delay_create_clear(&file->f_flags);
1410 static int ll_file_getstripe(struct inode *inode,
1411 struct lov_user_md __user *lum)
1417 env = cl_env_get(&refcheck);
1419 return PTR_ERR(env);
1421 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum);
1422 cl_env_put(env, &refcheck);
1426 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1429 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
1430 struct lov_user_md *klum;
1432 __u64 flags = FMODE_WRITE;
1434 rc = ll_copy_user_md(lum, &klum);
1439 rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, klum,
1441 cl_lov_delay_create_clear(&file->f_flags);
1445 put_user(0, &lum->lmm_stripe_count);
1447 ll_layout_refresh(inode, &gen);
1448 rc = ll_file_getstripe(inode, (struct lov_user_md __user *)arg);
1456 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1458 struct ll_inode_info *lli = ll_i2info(inode);
1459 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1460 struct ll_grouplock grouplock;
1464 CWARN("group id for group lock must not be 0\n");
1468 if (ll_file_nolock(file))
1471 spin_lock(&lli->lli_lock);
1472 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1473 CWARN("group lock already existed with gid %lu\n",
1474 fd->fd_grouplock.lg_gid);
1475 spin_unlock(&lli->lli_lock);
1478 LASSERT(!fd->fd_grouplock.lg_lock);
1479 spin_unlock(&lli->lli_lock);
1481 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
1482 arg, (file->f_flags & O_NONBLOCK), &grouplock);
1486 spin_lock(&lli->lli_lock);
1487 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1488 spin_unlock(&lli->lli_lock);
1489 CERROR("another thread just won the race\n");
1490 cl_put_grouplock(&grouplock);
1494 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1495 fd->fd_grouplock = grouplock;
1496 spin_unlock(&lli->lli_lock);
1498 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1502 static int ll_put_grouplock(struct inode *inode, struct file *file,
1505 struct ll_inode_info *lli = ll_i2info(inode);
1506 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1507 struct ll_grouplock grouplock;
1509 spin_lock(&lli->lli_lock);
1510 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1511 spin_unlock(&lli->lli_lock);
1512 CWARN("no group lock held\n");
1515 LASSERT(fd->fd_grouplock.lg_lock);
1517 if (fd->fd_grouplock.lg_gid != arg) {
1518 CWARN("group lock %lu doesn't match current id %lu\n",
1519 arg, fd->fd_grouplock.lg_gid);
1520 spin_unlock(&lli->lli_lock);
1524 grouplock = fd->fd_grouplock;
1525 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1526 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1527 spin_unlock(&lli->lli_lock);
1529 cl_put_grouplock(&grouplock);
1530 CDEBUG(D_INFO, "group lock %lu released\n", arg);
1535 * Close inode open handle
1537 * \param inode [in] inode in question
1538 * \param it [in,out] intent which contains open info and result
1541 * \retval <0 failure
1543 int ll_release_openhandle(struct inode *inode, struct lookup_intent *it)
1545 struct obd_client_handle *och;
1550 /* Root ? Do nothing. */
1551 if (is_root_inode(inode))
1554 /* No open handle to close? Move away */
1555 if (!it_disposition(it, DISP_OPEN_OPEN))
1558 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1560 och = kzalloc(sizeof(*och), GFP_NOFS);
1566 ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
1568 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1571 /* this one is in place of ll_file_open */
1572 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1573 ptlrpc_req_finished(it->it_request);
1574 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1580 * Get size for inode for which FIEMAP mapping is requested.
1581 * Make the FIEMAP get_info call and returns the result.
1583 static int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1586 struct obd_export *exp = ll_i2dtexp(inode);
1587 struct lov_stripe_md *lsm = NULL;
1588 struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1589 __u32 vallen = num_bytes;
1592 /* Checks for fiemap flags */
1593 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1594 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1598 /* Check for FIEMAP_FLAG_SYNC */
1599 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1600 rc = filemap_fdatawrite(inode->i_mapping);
1605 lsm = ccc_inode_lsm_get(inode);
1609 /* If the stripe_count > 1 and the application does not understand
1610 * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1612 if (lsm->lsm_stripe_count > 1 &&
1613 !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1618 fm_key.oa.o_oi = lsm->lsm_oi;
1619 fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1621 if (i_size_read(inode) == 0) {
1622 rc = ll_glimpse_size(inode);
1627 obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE);
1628 obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid);
1629 /* If filesize is 0, then there would be no objects for mapping */
1630 if (fm_key.oa.o_size == 0) {
1631 fiemap->fm_mapped_extents = 0;
1636 memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1638 rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen,
1641 CERROR("obd_get_info failed: rc = %d\n", rc);
1644 ccc_inode_lsm_put(inode, lsm);
1648 int ll_fid2path(struct inode *inode, void __user *arg)
1650 struct obd_export *exp = ll_i2mdexp(inode);
1651 const struct getinfo_fid2path __user *gfin = arg;
1652 struct getinfo_fid2path *gfout;
1657 if (!capable(CFS_CAP_DAC_READ_SEARCH) &&
1658 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
1661 /* Only need to get the buflen */
1662 if (get_user(pathlen, &gfin->gf_pathlen))
1665 if (pathlen > PATH_MAX)
1668 outsize = sizeof(*gfout) + pathlen;
1670 gfout = kzalloc(outsize, GFP_NOFS);
1674 if (copy_from_user(gfout, arg, sizeof(*gfout))) {
1679 /* Call mdc_iocontrol */
1680 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1684 if (copy_to_user(arg, gfout, outsize))
1692 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1694 struct ll_user_fiemap *fiemap_s;
1695 size_t num_bytes, ret_bytes;
1696 unsigned int extent_count;
1699 /* Get the extent count so we can calculate the size of
1700 * required fiemap buffer
1702 if (get_user(extent_count,
1703 &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1707 (SIZE_MAX - sizeof(*fiemap_s)) / sizeof(struct ll_fiemap_extent))
1709 num_bytes = sizeof(*fiemap_s) + (extent_count *
1710 sizeof(struct ll_fiemap_extent));
1712 fiemap_s = libcfs_kvzalloc(num_bytes, GFP_NOFS);
1716 /* get the fiemap value */
1717 if (copy_from_user(fiemap_s, (struct ll_user_fiemap __user *)arg,
1718 sizeof(*fiemap_s))) {
1723 /* If fm_extent_count is non-zero, read the first extent since
1724 * it is used to calculate end_offset and device from previous
1728 if (copy_from_user(&fiemap_s->fm_extents[0],
1729 (char __user *)arg + sizeof(*fiemap_s),
1730 sizeof(struct ll_fiemap_extent))) {
1736 rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1740 ret_bytes = sizeof(struct ll_user_fiemap);
1742 if (extent_count != 0)
1743 ret_bytes += (fiemap_s->fm_mapped_extents *
1744 sizeof(struct ll_fiemap_extent));
1746 if (copy_to_user((void __user *)arg, fiemap_s, ret_bytes))
1755 * Read the data_version for inode.
1757 * This value is computed using stripe object version on OST.
1758 * Version is computed using server side locking.
1760 * @param sync if do sync on the OST side;
1762 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
1763 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
1765 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
1767 struct lov_stripe_md *lsm = NULL;
1768 struct ll_sb_info *sbi = ll_i2sbi(inode);
1769 struct obdo *obdo = NULL;
1772 /* If no stripe, we consider version is 0. */
1773 lsm = ccc_inode_lsm_get(inode);
1774 if (!lsm_has_objects(lsm)) {
1776 CDEBUG(D_INODE, "No object for inode\n");
1781 obdo = kzalloc(sizeof(*obdo), GFP_NOFS);
1787 rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, obdo, 0, flags);
1789 if (!(obdo->o_valid & OBD_MD_FLDATAVERSION))
1792 *data_version = obdo->o_data_version;
1797 ccc_inode_lsm_put(inode, lsm);
1802 * Trigger a HSM release request for the provided inode.
1804 int ll_hsm_release(struct inode *inode)
1806 struct cl_env_nest nest;
1808 struct obd_client_handle *och = NULL;
1809 __u64 data_version = 0;
1812 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
1813 ll_get_fsname(inode->i_sb, NULL, 0),
1814 PFID(&ll_i2info(inode)->lli_fid));
1816 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
1822 /* Grab latest data_version and [am]time values */
1823 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
1827 env = cl_env_nested_get(&nest);
1833 ll_merge_attr(env, inode);
1834 cl_env_nested_put(&nest, env);
1836 /* Release the file.
1837 * NB: lease lock handle is released in mdc_hsm_release_pack() because
1838 * we still need it to pack l_remote_handle to MDT.
1840 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och,
1845 if (och && !IS_ERR(och)) /* close the file */
1846 ll_lease_close(och, inode, NULL);
1851 struct ll_swap_stack {
1852 struct iattr ia1, ia2;
1854 struct inode *inode1, *inode2;
1855 bool check_dv1, check_dv2;
1858 static int ll_swap_layouts(struct file *file1, struct file *file2,
1859 struct lustre_swap_layouts *lsl)
1861 struct mdc_swap_layouts msl;
1862 struct md_op_data *op_data;
1865 struct ll_swap_stack *llss = NULL;
1868 llss = kzalloc(sizeof(*llss), GFP_NOFS);
1872 llss->inode1 = file_inode(file1);
1873 llss->inode2 = file_inode(file2);
1875 if (!S_ISREG(llss->inode2->i_mode)) {
1880 if (inode_permission(llss->inode1, MAY_WRITE) ||
1881 inode_permission(llss->inode2, MAY_WRITE)) {
1886 if (llss->inode2->i_sb != llss->inode1->i_sb) {
1891 /* we use 2 bool because it is easier to swap than 2 bits */
1892 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
1893 llss->check_dv1 = true;
1895 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
1896 llss->check_dv2 = true;
1898 /* we cannot use lsl->sl_dvX directly because we may swap them */
1899 llss->dv1 = lsl->sl_dv1;
1900 llss->dv2 = lsl->sl_dv2;
1902 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
1903 if (rc == 0) /* same file, done! */ {
1908 if (rc < 0) { /* sequentialize it */
1909 swap(llss->inode1, llss->inode2);
1911 swap(llss->dv1, llss->dv2);
1912 swap(llss->check_dv1, llss->check_dv2);
1916 if (gid != 0) { /* application asks to flush dirty cache */
1917 rc = ll_get_grouplock(llss->inode1, file1, gid);
1921 rc = ll_get_grouplock(llss->inode2, file2, gid);
1923 ll_put_grouplock(llss->inode1, file1, gid);
1928 /* to be able to restore mtime and atime after swap
1929 * we need to first save them
1932 (SWAP_LAYOUTS_KEEP_MTIME | SWAP_LAYOUTS_KEEP_ATIME)) {
1933 llss->ia1.ia_mtime = llss->inode1->i_mtime;
1934 llss->ia1.ia_atime = llss->inode1->i_atime;
1935 llss->ia1.ia_valid = ATTR_MTIME | ATTR_ATIME;
1936 llss->ia2.ia_mtime = llss->inode2->i_mtime;
1937 llss->ia2.ia_atime = llss->inode2->i_atime;
1938 llss->ia2.ia_valid = ATTR_MTIME | ATTR_ATIME;
1941 /* ultimate check, before swapping the layouts we check if
1942 * dataversion has changed (if requested)
1944 if (llss->check_dv1) {
1945 rc = ll_data_version(llss->inode1, &dv, 0);
1948 if (dv != llss->dv1) {
1954 if (llss->check_dv2) {
1955 rc = ll_data_version(llss->inode2, &dv, 0);
1958 if (dv != llss->dv2) {
1964 /* struct md_op_data is used to send the swap args to the mdt
1965 * only flags is missing, so we use struct mdc_swap_layouts
1966 * through the md_op_data->op_data
1968 /* flags from user space have to be converted before they are send to
1969 * server, no flag is sent today, they are only used on the client
1973 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
1974 0, LUSTRE_OPC_ANY, &msl);
1975 if (IS_ERR(op_data)) {
1976 rc = PTR_ERR(op_data);
1980 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
1981 sizeof(*op_data), op_data, NULL);
1982 ll_finish_md_op_data(op_data);
1986 ll_put_grouplock(llss->inode2, file2, gid);
1987 ll_put_grouplock(llss->inode1, file1, gid);
1990 /* rc can be set from obd_iocontrol() or from a GOTO(putgl, ...) */
1994 /* clear useless flags */
1995 if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_MTIME)) {
1996 llss->ia1.ia_valid &= ~ATTR_MTIME;
1997 llss->ia2.ia_valid &= ~ATTR_MTIME;
2000 if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_ATIME)) {
2001 llss->ia1.ia_valid &= ~ATTR_ATIME;
2002 llss->ia2.ia_valid &= ~ATTR_ATIME;
2005 /* update time if requested */
2007 if (llss->ia2.ia_valid != 0) {
2008 inode_lock(llss->inode1);
2009 rc = ll_setattr(file1->f_path.dentry, &llss->ia2);
2010 inode_unlock(llss->inode1);
2013 if (llss->ia1.ia_valid != 0) {
2016 inode_lock(llss->inode2);
2017 rc1 = ll_setattr(file2->f_path.dentry, &llss->ia1);
2018 inode_unlock(llss->inode2);
2029 static int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2031 struct md_op_data *op_data;
2034 /* Detect out-of range masks */
2035 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2038 /* Non-root users are forbidden to set or clear flags which are
2039 * NOT defined in HSM_USER_MASK.
2041 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2042 !capable(CFS_CAP_SYS_ADMIN))
2045 /* Detect out-of range archive id */
2046 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2047 (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
2050 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2051 LUSTRE_OPC_ANY, hss);
2052 if (IS_ERR(op_data))
2053 return PTR_ERR(op_data);
2055 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
2056 sizeof(*op_data), op_data, NULL);
2058 ll_finish_md_op_data(op_data);
2063 static int ll_hsm_import(struct inode *inode, struct file *file,
2064 struct hsm_user_import *hui)
2066 struct hsm_state_set *hss = NULL;
2067 struct iattr *attr = NULL;
2070 if (!S_ISREG(inode->i_mode))
2074 hss = kzalloc(sizeof(*hss), GFP_NOFS);
2078 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2079 hss->hss_archive_id = hui->hui_archive_id;
2080 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2081 rc = ll_hsm_state_set(inode, hss);
2085 attr = kzalloc(sizeof(*attr), GFP_NOFS);
2091 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2092 attr->ia_mode |= S_IFREG;
2093 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2094 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2095 attr->ia_size = hui->hui_size;
2096 attr->ia_mtime.tv_sec = hui->hui_mtime;
2097 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2098 attr->ia_atime.tv_sec = hui->hui_atime;
2099 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2101 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2102 ATTR_UID | ATTR_GID |
2103 ATTR_MTIME | ATTR_MTIME_SET |
2104 ATTR_ATIME | ATTR_ATIME_SET;
2108 rc = ll_setattr_raw(file->f_path.dentry, attr, true);
2112 inode_unlock(inode);
2120 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2122 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2123 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2127 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2129 struct inode *inode = file_inode(file);
2130 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2133 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),cmd=%x\n",
2134 PFID(ll_inode2fid(inode)), inode, cmd);
2135 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
2137 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
2138 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
2142 case LL_IOC_GETFLAGS:
2143 /* Get the current value of the file flags */
2144 return put_user(fd->fd_flags, (int __user *)arg);
2145 case LL_IOC_SETFLAGS:
2146 case LL_IOC_CLRFLAGS:
2147 /* Set or clear specific file flags */
2148 /* XXX This probably needs checks to ensure the flags are
2149 * not abused, and to handle any flag side effects.
2151 if (get_user(flags, (int __user *)arg))
2154 if (cmd == LL_IOC_SETFLAGS) {
2155 if ((flags & LL_FILE_IGNORE_LOCK) &&
2156 !(file->f_flags & O_DIRECT)) {
2157 CERROR("%s: unable to disable locking on non-O_DIRECT file\n",
2162 fd->fd_flags |= flags;
2164 fd->fd_flags &= ~flags;
2167 case LL_IOC_LOV_SETSTRIPE:
2168 return ll_lov_setstripe(inode, file, arg);
2169 case LL_IOC_LOV_SETEA:
2170 return ll_lov_setea(inode, file, arg);
2171 case LL_IOC_LOV_SWAP_LAYOUTS: {
2173 struct lustre_swap_layouts lsl;
2175 if (copy_from_user(&lsl, (char __user *)arg,
2176 sizeof(struct lustre_swap_layouts)))
2179 if ((file->f_flags & O_ACCMODE) == 0) /* O_RDONLY */
2182 file2 = fget(lsl.sl_fd);
2187 if ((file2->f_flags & O_ACCMODE) != 0) /* O_WRONLY or O_RDWR */
2188 rc = ll_swap_layouts(file, file2, &lsl);
2192 case LL_IOC_LOV_GETSTRIPE:
2193 return ll_file_getstripe(inode,
2194 (struct lov_user_md __user *)arg);
2195 case FSFILT_IOC_FIEMAP:
2196 return ll_ioctl_fiemap(inode, arg);
2197 case FSFILT_IOC_GETFLAGS:
2198 case FSFILT_IOC_SETFLAGS:
2199 return ll_iocontrol(inode, file, cmd, arg);
2200 case FSFILT_IOC_GETVERSION_OLD:
2201 case FSFILT_IOC_GETVERSION:
2202 return put_user(inode->i_generation, (int __user *)arg);
2203 case LL_IOC_GROUP_LOCK:
2204 return ll_get_grouplock(inode, file, arg);
2205 case LL_IOC_GROUP_UNLOCK:
2206 return ll_put_grouplock(inode, file, arg);
2207 case IOC_OBD_STATFS:
2208 return ll_obd_statfs(inode, (void __user *)arg);
2210 /* We need to special case any other ioctls we want to handle,
2211 * to send them to the MDS/OST as appropriate and to properly
2212 * network encode the arg field.
2213 case FSFILT_IOC_SETVERSION_OLD:
2214 case FSFILT_IOC_SETVERSION:
2216 case LL_IOC_FLUSHCTX:
2217 return ll_flush_ctx(inode);
2218 case LL_IOC_PATH2FID: {
2219 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
2220 sizeof(struct lu_fid)))
2225 case LL_IOC_GETPARENT:
2226 return ll_getparent(file, (struct getparent __user *)arg);
2227 case OBD_IOC_FID2PATH:
2228 return ll_fid2path(inode, (void __user *)arg);
2229 case LL_IOC_DATA_VERSION: {
2230 struct ioc_data_version idv;
2233 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
2236 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
2237 rc = ll_data_version(inode, &idv.idv_version, idv.idv_flags);
2238 if (rc == 0 && copy_to_user((char __user *)arg, &idv,
2245 case LL_IOC_GET_MDTIDX: {
2248 mdtidx = ll_get_mdt_idx(inode);
2252 if (put_user(mdtidx, (int __user *)arg))
2257 case OBD_IOC_GETDTNAME:
2258 case OBD_IOC_GETMDNAME:
2259 return ll_get_obd_name(inode, cmd, arg);
2260 case LL_IOC_HSM_STATE_GET: {
2261 struct md_op_data *op_data;
2262 struct hsm_user_state *hus;
2265 hus = kzalloc(sizeof(*hus), GFP_NOFS);
2269 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2270 LUSTRE_OPC_ANY, hus);
2271 if (IS_ERR(op_data)) {
2273 return PTR_ERR(op_data);
2276 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2279 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
2282 ll_finish_md_op_data(op_data);
2286 case LL_IOC_HSM_STATE_SET: {
2287 struct hsm_state_set *hss;
2290 hss = memdup_user((char __user *)arg, sizeof(*hss));
2292 return PTR_ERR(hss);
2294 rc = ll_hsm_state_set(inode, hss);
2299 case LL_IOC_HSM_ACTION: {
2300 struct md_op_data *op_data;
2301 struct hsm_current_action *hca;
2304 hca = kzalloc(sizeof(*hca), GFP_NOFS);
2308 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2309 LUSTRE_OPC_ANY, hca);
2310 if (IS_ERR(op_data)) {
2312 return PTR_ERR(op_data);
2315 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2318 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
2321 ll_finish_md_op_data(op_data);
2325 case LL_IOC_SET_LEASE: {
2326 struct ll_inode_info *lli = ll_i2info(inode);
2327 struct obd_client_handle *och = NULL;
2332 case LL_LEASE_WRLCK:
2333 if (!(file->f_mode & FMODE_WRITE))
2335 fmode = FMODE_WRITE;
2337 case LL_LEASE_RDLCK:
2338 if (!(file->f_mode & FMODE_READ))
2342 case LL_LEASE_UNLCK:
2343 mutex_lock(&lli->lli_och_mutex);
2344 if (fd->fd_lease_och) {
2345 och = fd->fd_lease_och;
2346 fd->fd_lease_och = NULL;
2348 mutex_unlock(&lli->lli_och_mutex);
2353 fmode = och->och_flags;
2354 rc = ll_lease_close(och, inode, &lease_broken);
2361 return ll_lease_type_from_fmode(fmode);
2366 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
2368 /* apply for lease */
2369 och = ll_lease_open(inode, file, fmode, 0);
2371 return PTR_ERR(och);
2374 mutex_lock(&lli->lli_och_mutex);
2375 if (!fd->fd_lease_och) {
2376 fd->fd_lease_och = och;
2379 mutex_unlock(&lli->lli_och_mutex);
2381 /* impossible now that only excl is supported for now */
2382 ll_lease_close(och, inode, &lease_broken);
2387 case LL_IOC_GET_LEASE: {
2388 struct ll_inode_info *lli = ll_i2info(inode);
2389 struct ldlm_lock *lock = NULL;
2392 mutex_lock(&lli->lli_och_mutex);
2393 if (fd->fd_lease_och) {
2394 struct obd_client_handle *och = fd->fd_lease_och;
2396 lock = ldlm_handle2lock(&och->och_lease_handle);
2398 lock_res_and_lock(lock);
2399 if (!ldlm_is_cancel(lock))
2400 fmode = och->och_flags;
2401 unlock_res_and_lock(lock);
2402 LDLM_LOCK_PUT(lock);
2405 mutex_unlock(&lli->lli_och_mutex);
2406 return ll_lease_type_from_fmode(fmode);
2408 case LL_IOC_HSM_IMPORT: {
2409 struct hsm_user_import *hui;
2411 hui = memdup_user((void __user *)arg, sizeof(*hui));
2413 return PTR_ERR(hui);
2415 rc = ll_hsm_import(inode, file, hui);
2423 if (ll_iocontrol_call(inode, file, cmd, arg, &err) ==
2427 return obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
2428 (void __user *)arg);
2433 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
2435 struct inode *inode = file_inode(file);
2436 loff_t retval, eof = 0;
2438 retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
2439 (origin == SEEK_CUR) ? file->f_pos : 0);
2440 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
2441 PFID(ll_inode2fid(inode)), inode, retval, retval, origin);
2442 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
2444 if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
2445 retval = ll_glimpse_size(inode);
2448 eof = i_size_read(inode);
2451 return generic_file_llseek_size(file, offset, origin,
2452 ll_file_maxbytes(inode), eof);
2455 static int ll_flush(struct file *file, fl_owner_t id)
2457 struct inode *inode = file_inode(file);
2458 struct ll_inode_info *lli = ll_i2info(inode);
2459 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2462 LASSERT(!S_ISDIR(inode->i_mode));
2464 /* catch async errors that were recorded back when async writeback
2465 * failed for pages in this mapping.
2467 rc = lli->lli_async_rc;
2468 lli->lli_async_rc = 0;
2469 if (lli->lli_clob) {
2470 err = lov_read_and_clear_async_rc(lli->lli_clob);
2475 /* The application has been told about write failure already.
2476 * Do not report failure again.
2478 if (fd->fd_write_failed)
2480 return rc ? -EIO : 0;
2484 * Called to make sure a portion of file has been written out.
2485 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
2487 * Return how many pages have been written.
2489 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
2490 enum cl_fsync_mode mode, int ignore_layout)
2492 struct cl_env_nest nest;
2495 struct cl_fsync_io *fio;
2498 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2499 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2502 env = cl_env_nested_get(&nest);
2504 return PTR_ERR(env);
2506 io = vvp_env_thread_io(env);
2507 io->ci_obj = ll_i2info(inode)->lli_clob;
2508 io->ci_ignore_layout = ignore_layout;
2510 /* initialize parameters for sync */
2511 fio = &io->u.ci_fsync;
2512 fio->fi_start = start;
2514 fio->fi_fid = ll_inode2fid(inode);
2515 fio->fi_mode = mode;
2516 fio->fi_nr_written = 0;
2518 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2519 result = cl_io_loop(env, io);
2521 result = io->ci_result;
2523 result = fio->fi_nr_written;
2524 cl_io_fini(env, io);
2525 cl_env_nested_put(&nest, env);
2530 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2532 struct inode *inode = file_inode(file);
2533 struct ll_inode_info *lli = ll_i2info(inode);
2534 struct ptlrpc_request *req;
2537 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2538 PFID(ll_inode2fid(inode)), inode);
2539 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2541 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2544 /* catch async errors that were recorded back when async writeback
2545 * failed for pages in this mapping.
2547 if (!S_ISDIR(inode->i_mode)) {
2548 err = lli->lli_async_rc;
2549 lli->lli_async_rc = 0;
2552 err = lov_read_and_clear_async_rc(lli->lli_clob);
2557 err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
2561 ptlrpc_req_finished(req);
2563 if (S_ISREG(inode->i_mode)) {
2564 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2566 err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
2567 if (rc == 0 && err < 0)
2570 fd->fd_write_failed = true;
2572 fd->fd_write_failed = false;
2575 inode_unlock(inode);
2580 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2582 struct inode *inode = file_inode(file);
2583 struct ll_sb_info *sbi = ll_i2sbi(inode);
2584 struct ldlm_enqueue_info einfo = {
2585 .ei_type = LDLM_FLOCK,
2586 .ei_cb_cp = ldlm_flock_completion_ast,
2587 .ei_cbdata = file_lock,
2589 struct md_op_data *op_data;
2590 struct lustre_handle lockh = {0};
2591 ldlm_policy_data_t flock = { {0} };
2592 int fl_type = file_lock->fl_type;
2597 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
2598 PFID(ll_inode2fid(inode)), file_lock);
2600 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2602 if (file_lock->fl_flags & FL_FLOCK)
2603 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2604 else if (!(file_lock->fl_flags & FL_POSIX))
2607 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2608 flock.l_flock.pid = file_lock->fl_pid;
2609 flock.l_flock.start = file_lock->fl_start;
2610 flock.l_flock.end = file_lock->fl_end;
2612 /* Somewhat ugly workaround for svc lockd.
2613 * lockd installs custom fl_lmops->lm_compare_owner that checks
2614 * for the fl_owner to be the same (which it always is on local node
2615 * I guess between lockd processes) and then compares pid.
2616 * As such we assign pid to the owner field to make it all work,
2617 * conflict with normal locks is unlikely since pid space and
2618 * pointer space for current->files are not intersecting
2620 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
2621 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2625 einfo.ei_mode = LCK_PR;
2628 /* An unlock request may or may not have any relation to
2629 * existing locks so we may not be able to pass a lock handle
2630 * via a normal ldlm_lock_cancel() request. The request may even
2631 * unlock a byte range in the middle of an existing lock. In
2632 * order to process an unlock request we need all of the same
2633 * information that is given with a normal read or write record
2634 * lock request. To avoid creating another ldlm unlock (cancel)
2635 * message we'll treat a LCK_NL flock request as an unlock.
2637 einfo.ei_mode = LCK_NL;
2640 einfo.ei_mode = LCK_PW;
2643 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
2658 flags = LDLM_FL_BLOCK_NOWAIT;
2664 flags = LDLM_FL_TEST_LOCK;
2667 CERROR("unknown fcntl lock command: %d\n", cmd);
2672 * Save the old mode so that if the mode in the lock changes we
2673 * can decrement the appropriate reader or writer refcount.
2675 file_lock->fl_type = einfo.ei_mode;
2677 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2678 LUSTRE_OPC_ANY, NULL);
2679 if (IS_ERR(op_data))
2680 return PTR_ERR(op_data);
2682 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, start=%llu, end=%llu\n",
2683 PFID(ll_inode2fid(inode)), flock.l_flock.pid, flags,
2684 einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2686 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, NULL, op_data, &lockh,
2689 /* Restore the file lock type if not TEST lock. */
2690 if (!(flags & LDLM_FL_TEST_LOCK))
2691 file_lock->fl_type = fl_type;
2693 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
2694 !(flags & LDLM_FL_TEST_LOCK))
2695 rc2 = locks_lock_file_wait(file, file_lock);
2697 if (rc2 && file_lock->fl_type != F_UNLCK) {
2698 einfo.ei_mode = LCK_NL;
2699 md_enqueue(sbi->ll_md_exp, &einfo, &flock, NULL, op_data,
2704 ll_finish_md_op_data(op_data);
2709 int ll_get_fid_by_name(struct inode *parent, const char *name,
2710 int namelen, struct lu_fid *fid)
2712 struct md_op_data *op_data = NULL;
2713 struct ptlrpc_request *req;
2714 struct mdt_body *body;
2717 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
2718 LUSTRE_OPC_ANY, NULL);
2719 if (IS_ERR(op_data))
2720 return PTR_ERR(op_data);
2722 op_data->op_valid = OBD_MD_FLID;
2723 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
2724 ll_finish_md_op_data(op_data);
2728 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2734 *fid = body->mbo_fid1;
2736 ptlrpc_req_finished(req);
2740 int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
2741 const char *name, int namelen)
2743 struct ptlrpc_request *request = NULL;
2744 struct inode *child_inode = NULL;
2745 struct dentry *dchild = NULL;
2746 struct md_op_data *op_data;
2750 CDEBUG(D_VFSTRACE, "migrate %s under "DFID" to MDT%d\n",
2751 name, PFID(ll_inode2fid(parent)), mdtidx);
2753 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
2754 0, LUSTRE_OPC_ANY, NULL);
2755 if (IS_ERR(op_data))
2756 return PTR_ERR(op_data);
2758 /* Get child FID first */
2759 qstr.hash = full_name_hash(parent, name, namelen);
2762 dchild = d_lookup(file_dentry(file), &qstr);
2764 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
2765 if (dchild->d_inode) {
2766 child_inode = igrab(dchild->d_inode);
2768 inode_lock(child_inode);
2769 op_data->op_fid3 = *ll_inode2fid(child_inode);
2770 ll_invalidate_aliases(child_inode);
2775 rc = ll_get_fid_by_name(parent, name, namelen,
2781 if (!fid_is_sane(&op_data->op_fid3)) {
2782 CERROR("%s: migrate %s, but fid "DFID" is insane\n",
2783 ll_get_fsname(parent->i_sb, NULL, 0), name,
2784 PFID(&op_data->op_fid3));
2789 rc = ll_get_mdt_idx_by_fid(ll_i2sbi(parent), &op_data->op_fid3);
2794 CDEBUG(D_INFO, "%s:"DFID" is already on MDT%d.\n", name,
2795 PFID(&op_data->op_fid3), mdtidx);
2800 op_data->op_mds = mdtidx;
2801 op_data->op_cli_flags = CLI_MIGRATE;
2802 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name,
2803 namelen, name, namelen, &request);
2805 ll_update_times(request, parent);
2807 ptlrpc_req_finished(request);
2811 clear_nlink(child_inode);
2812 inode_unlock(child_inode);
2816 ll_finish_md_op_data(op_data);
2821 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2827 * test if some locks matching bits and l_req_mode are acquired
2828 * - bits can be in different locks
2829 * - if found clear the common lock bits in *bits
2830 * - the bits not found, are kept in *bits
2832 * \param bits [IN] searched lock bits [IN]
2833 * \param l_req_mode [IN] searched lock mode
2834 * \retval boolean, true iff all bits are found
2836 int ll_have_md_lock(struct inode *inode, __u64 *bits,
2837 enum ldlm_mode l_req_mode)
2839 struct lustre_handle lockh;
2840 ldlm_policy_data_t policy;
2841 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
2842 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
2850 fid = &ll_i2info(inode)->lli_fid;
2851 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2852 ldlm_lockname[mode]);
2854 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2855 for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2856 policy.l_inodebits.bits = *bits & (1 << i);
2857 if (policy.l_inodebits.bits == 0)
2860 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2861 &policy, mode, &lockh)) {
2862 struct ldlm_lock *lock;
2864 lock = ldlm_handle2lock(&lockh);
2867 ~(lock->l_policy_data.l_inodebits.bits);
2868 LDLM_LOCK_PUT(lock);
2870 *bits &= ~policy.l_inodebits.bits;
2877 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
2878 struct lustre_handle *lockh, __u64 flags,
2879 enum ldlm_mode mode)
2881 ldlm_policy_data_t policy = { .l_inodebits = {bits} };
2884 fid = &ll_i2info(inode)->lli_fid;
2885 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2887 return md_lock_match(ll_i2mdexp(inode), flags | LDLM_FL_BLOCK_GRANTED,
2888 fid, LDLM_IBITS, &policy, mode, lockh);
2891 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
2893 /* Already unlinked. Just update nlink and return success */
2894 if (rc == -ENOENT) {
2896 /* This path cannot be hit for regular files unless in
2897 * case of obscure races, so no need to validate size.
2899 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
2901 } else if (rc != 0) {
2902 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
2903 "%s: revalidate FID "DFID" error: rc = %d\n",
2904 ll_get_fsname(inode->i_sb, NULL, 0),
2905 PFID(ll_inode2fid(inode)), rc);
2911 static int __ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
2913 struct inode *inode = d_inode(dentry);
2914 struct ptlrpc_request *req = NULL;
2915 struct obd_export *exp;
2918 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%pd\n",
2919 PFID(ll_inode2fid(inode)), inode, dentry);
2921 exp = ll_i2mdexp(inode);
2923 /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2924 * But under CMD case, it caused some lock issues, should be fixed
2925 * with new CMD ibits lock. See bug 12718
2927 if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) {
2928 struct lookup_intent oit = { .it_op = IT_GETATTR };
2929 struct md_op_data *op_data;
2931 if (ibits == MDS_INODELOCK_LOOKUP)
2932 oit.it_op = IT_LOOKUP;
2934 /* Call getattr by fid, so do not provide name at all. */
2935 op_data = ll_prep_md_op_data(NULL, inode,
2937 LUSTRE_OPC_ANY, NULL);
2938 if (IS_ERR(op_data))
2939 return PTR_ERR(op_data);
2941 rc = md_intent_lock(exp, op_data, &oit, &req,
2942 &ll_md_blocking_ast, 0);
2943 ll_finish_md_op_data(op_data);
2945 rc = ll_inode_revalidate_fini(inode, rc);
2949 rc = ll_revalidate_it_finish(req, &oit, inode);
2951 ll_intent_release(&oit);
2955 /* Unlinked? Unhash dentry, so it is not picked up later by
2956 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2957 * here to preserve get_cwd functionality on 2.6.
2960 if (!d_inode(dentry)->i_nlink) {
2961 spin_lock(&inode->i_lock);
2962 d_lustre_invalidate(dentry, 0);
2963 spin_unlock(&inode->i_lock);
2966 ll_lookup_finish_locks(&oit, inode);
2967 } else if (!ll_have_md_lock(d_inode(dentry), &ibits, LCK_MINMODE)) {
2968 struct ll_sb_info *sbi = ll_i2sbi(d_inode(dentry));
2969 u64 valid = OBD_MD_FLGETATTR;
2970 struct md_op_data *op_data;
2973 if (S_ISREG(inode->i_mode)) {
2974 rc = ll_get_default_mdsize(sbi, &ealen);
2977 valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2980 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2981 0, ealen, LUSTRE_OPC_ANY,
2983 if (IS_ERR(op_data))
2984 return PTR_ERR(op_data);
2986 op_data->op_valid = valid;
2987 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2988 ll_finish_md_op_data(op_data);
2990 return ll_inode_revalidate_fini(inode, rc);
2992 rc = ll_prep_inode(&inode, req, NULL, NULL);
2995 ptlrpc_req_finished(req);
2999 static int ll_merge_md_attr(struct inode *inode)
3001 struct cl_attr attr = { 0 };
3004 LASSERT(ll_i2info(inode)->lli_lsm_md);
3005 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
3006 &attr, ll_md_blocking_ast);
3010 set_nlink(inode, attr.cat_nlink);
3011 inode->i_blocks = attr.cat_blocks;
3012 i_size_write(inode, attr.cat_size);
3014 ll_i2info(inode)->lli_atime = attr.cat_atime;
3015 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
3016 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
3021 static int ll_inode_revalidate(struct dentry *dentry, __u64 ibits)
3023 struct inode *inode = d_inode(dentry);
3026 rc = __ll_inode_revalidate(dentry, ibits);
3030 /* if object isn't regular file, don't validate size */
3031 if (!S_ISREG(inode->i_mode)) {
3032 if (S_ISDIR(inode->i_mode) &&
3033 ll_i2info(inode)->lli_lsm_md) {
3034 rc = ll_merge_md_attr(inode);
3039 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_atime;
3040 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_mtime;
3041 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_ctime;
3043 /* In case of restore, the MDT has the right size and has
3044 * already send it back without granting the layout lock,
3045 * inode is up-to-date so glimpse is useless.
3046 * Also to glimpse we need the layout, in case of a running
3047 * restore the MDT holds the layout lock so the glimpse will
3048 * block up to the end of restore (getattr will block)
3050 if (!(ll_i2info(inode)->lli_flags & LLIF_FILE_RESTORING))
3051 rc = ll_glimpse_size(inode);
3056 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
3058 struct inode *inode = d_inode(de);
3059 struct ll_sb_info *sbi = ll_i2sbi(inode);
3060 struct ll_inode_info *lli = ll_i2info(inode);
3063 res = ll_inode_revalidate(de, MDS_INODELOCK_UPDATE |
3064 MDS_INODELOCK_LOOKUP);
3065 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
3070 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
3072 stat->dev = inode->i_sb->s_dev;
3073 if (ll_need_32bit_api(sbi))
3074 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
3076 stat->ino = inode->i_ino;
3077 stat->mode = inode->i_mode;
3078 stat->uid = inode->i_uid;
3079 stat->gid = inode->i_gid;
3080 stat->rdev = inode->i_rdev;
3081 stat->atime = inode->i_atime;
3082 stat->mtime = inode->i_mtime;
3083 stat->ctime = inode->i_ctime;
3084 stat->blksize = 1 << inode->i_blkbits;
3086 stat->nlink = inode->i_nlink;
3087 stat->size = i_size_read(inode);
3088 stat->blocks = inode->i_blocks;
3093 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3094 __u64 start, __u64 len)
3098 struct ll_user_fiemap *fiemap;
3099 unsigned int extent_count = fieinfo->fi_extents_max;
3101 num_bytes = sizeof(*fiemap) + (extent_count *
3102 sizeof(struct ll_fiemap_extent));
3103 fiemap = libcfs_kvzalloc(num_bytes, GFP_NOFS);
3108 fiemap->fm_flags = fieinfo->fi_flags;
3109 fiemap->fm_extent_count = fieinfo->fi_extents_max;
3110 fiemap->fm_start = start;
3111 fiemap->fm_length = len;
3112 if (extent_count > 0 &&
3113 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
3114 sizeof(struct ll_fiemap_extent)) != 0) {
3119 rc = ll_do_fiemap(inode, fiemap, num_bytes);
3121 fieinfo->fi_flags = fiemap->fm_flags;
3122 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
3123 if (extent_count > 0 &&
3124 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
3125 fiemap->fm_mapped_extents *
3126 sizeof(struct ll_fiemap_extent)) != 0) {
3136 struct posix_acl *ll_get_acl(struct inode *inode, int type)
3138 struct ll_inode_info *lli = ll_i2info(inode);
3139 struct posix_acl *acl = NULL;
3141 spin_lock(&lli->lli_lock);
3142 /* VFS' acl_permission_check->check_acl will release the refcount */
3143 acl = posix_acl_dup(lli->lli_posix_acl);
3144 #ifdef CONFIG_FS_POSIX_ACL
3145 forget_cached_acl(inode, type);
3147 spin_unlock(&lli->lli_lock);
3152 int ll_inode_permission(struct inode *inode, int mask)
3154 struct ll_sb_info *sbi;
3155 struct root_squash_info *squash;
3156 const struct cred *old_cred = NULL;
3157 struct cred *cred = NULL;
3158 bool squash_id = false;
3162 if (mask & MAY_NOT_BLOCK)
3165 /* as root inode are NOT getting validated in lookup operation,
3166 * need to do it before permission check.
3169 if (is_root_inode(inode)) {
3170 rc = __ll_inode_revalidate(inode->i_sb->s_root,
3171 MDS_INODELOCK_LOOKUP);
3176 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
3177 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
3179 /* squash fsuid/fsgid if needed */
3180 sbi = ll_i2sbi(inode);
3181 squash = &sbi->ll_squash;
3182 if (unlikely(squash->rsi_uid &&
3183 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
3184 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
3189 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
3190 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
3191 squash->rsi_uid, squash->rsi_gid);
3194 * update current process's credentials
3197 cred = prepare_creds();
3201 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
3202 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
3203 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
3204 if ((1 << cap) & CFS_CAP_FS_MASK)
3205 cap_lower(cred->cap_effective, cap);
3207 old_cred = override_creds(cred);
3210 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
3211 rc = generic_permission(inode, mask);
3213 /* restore current process's credentials and FS capability */
3215 revert_creds(old_cred);
3222 /* -o localflock - only provides locally consistent flock locks */
3223 struct file_operations ll_file_operations = {
3224 .read_iter = ll_file_read_iter,
3225 .write_iter = ll_file_write_iter,
3226 .unlocked_ioctl = ll_file_ioctl,
3227 .open = ll_file_open,
3228 .release = ll_file_release,
3229 .mmap = ll_file_mmap,
3230 .llseek = ll_file_seek,
3231 .splice_read = generic_file_splice_read,
3236 struct file_operations ll_file_operations_flock = {
3237 .read_iter = ll_file_read_iter,
3238 .write_iter = ll_file_write_iter,
3239 .unlocked_ioctl = ll_file_ioctl,
3240 .open = ll_file_open,
3241 .release = ll_file_release,
3242 .mmap = ll_file_mmap,
3243 .llseek = ll_file_seek,
3244 .splice_read = generic_file_splice_read,
3247 .flock = ll_file_flock,
3248 .lock = ll_file_flock
3251 /* These are for -o noflock - to return ENOSYS on flock calls */
3252 struct file_operations ll_file_operations_noflock = {
3253 .read_iter = ll_file_read_iter,
3254 .write_iter = ll_file_write_iter,
3255 .unlocked_ioctl = ll_file_ioctl,
3256 .open = ll_file_open,
3257 .release = ll_file_release,
3258 .mmap = ll_file_mmap,
3259 .llseek = ll_file_seek,
3260 .splice_read = generic_file_splice_read,
3263 .flock = ll_file_noflock,
3264 .lock = ll_file_noflock
3267 const struct inode_operations ll_file_inode_operations = {
3268 .setattr = ll_setattr,
3269 .getattr = ll_getattr,
3270 .permission = ll_inode_permission,
3271 .setxattr = generic_setxattr,
3272 .getxattr = generic_getxattr,
3273 .listxattr = ll_listxattr,
3274 .removexattr = generic_removexattr,
3275 .fiemap = ll_fiemap,
3276 .get_acl = ll_get_acl,
3279 /* dynamic ioctl number support routines */
3280 static struct llioc_ctl_data {
3281 struct rw_semaphore ioc_sem;
3282 struct list_head ioc_head;
3284 __RWSEM_INITIALIZER(llioc.ioc_sem),
3285 LIST_HEAD_INIT(llioc.ioc_head)
3289 struct list_head iocd_list;
3290 unsigned int iocd_size;
3291 llioc_callback_t iocd_cb;
3292 unsigned int iocd_count;
3293 unsigned int iocd_cmd[0];
3296 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
3299 struct llioc_data *in_data = NULL;
3301 if (!cb || !cmd || count > LLIOC_MAX_CMD || count < 0)
3304 size = sizeof(*in_data) + count * sizeof(unsigned int);
3305 in_data = kzalloc(size, GFP_NOFS);
3309 in_data->iocd_size = size;
3310 in_data->iocd_cb = cb;
3311 in_data->iocd_count = count;
3312 memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
3314 down_write(&llioc.ioc_sem);
3315 list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
3316 up_write(&llioc.ioc_sem);
3320 EXPORT_SYMBOL(ll_iocontrol_register);
3322 void ll_iocontrol_unregister(void *magic)
3324 struct llioc_data *tmp;
3329 down_write(&llioc.ioc_sem);
3330 list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
3332 list_del(&tmp->iocd_list);
3333 up_write(&llioc.ioc_sem);
3339 up_write(&llioc.ioc_sem);
3341 CWARN("didn't find iocontrol register block with magic: %p\n", magic);
3343 EXPORT_SYMBOL(ll_iocontrol_unregister);
3345 static enum llioc_iter
3346 ll_iocontrol_call(struct inode *inode, struct file *file,
3347 unsigned int cmd, unsigned long arg, int *rcp)
3349 enum llioc_iter ret = LLIOC_CONT;
3350 struct llioc_data *data;
3351 int rc = -EINVAL, i;
3353 down_read(&llioc.ioc_sem);
3354 list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
3355 for (i = 0; i < data->iocd_count; i++) {
3356 if (cmd != data->iocd_cmd[i])
3359 ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
3363 if (ret == LLIOC_STOP)
3366 up_read(&llioc.ioc_sem);
3373 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
3375 struct ll_inode_info *lli = ll_i2info(inode);
3376 struct cl_env_nest nest;
3383 env = cl_env_nested_get(&nest);
3385 return PTR_ERR(env);
3387 result = cl_conf_set(env, lli->lli_clob, conf);
3388 cl_env_nested_put(&nest, env);
3390 if (conf->coc_opc == OBJECT_CONF_SET) {
3391 struct ldlm_lock *lock = conf->coc_lock;
3394 LASSERT(ldlm_has_layout(lock));
3396 /* it can only be allowed to match after layout is
3397 * applied to inode otherwise false layout would be
3398 * seen. Applying layout should happen before dropping
3401 ldlm_lock_allow_match(lock);
3407 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
3408 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
3411 struct ll_sb_info *sbi = ll_i2sbi(inode);
3412 struct ptlrpc_request *req;
3413 struct mdt_body *body;
3419 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
3420 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
3421 lock->l_lvb_data, lock->l_lvb_len);
3423 if (lock->l_lvb_data && ldlm_is_lvb_ready(lock))
3426 /* if layout lock was granted right away, the layout is returned
3427 * within DLM_LVB of dlm reply; otherwise if the lock was ever
3428 * blocked and then granted via completion ast, we have to fetch
3429 * layout here. Please note that we can't use the LVB buffer in
3430 * completion AST because it doesn't have a large enough buffer
3432 rc = ll_get_default_mdsize(sbi, &lmmsize);
3434 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
3435 OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
3440 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3446 lmmsize = body->mbo_eadatasize;
3447 if (lmmsize == 0) /* empty layout */ {
3452 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
3458 lvbdata = libcfs_kvzalloc(lmmsize, GFP_NOFS);
3464 memcpy(lvbdata, lmm, lmmsize);
3465 lock_res_and_lock(lock);
3466 if (lock->l_lvb_data)
3467 kvfree(lock->l_lvb_data);
3469 lock->l_lvb_data = lvbdata;
3470 lock->l_lvb_len = lmmsize;
3471 unlock_res_and_lock(lock);
3474 ptlrpc_req_finished(req);
3479 * Apply the layout to the inode. Layout lock is held and will be released
3482 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
3483 struct inode *inode, __u32 *gen, bool reconf)
3485 struct ll_inode_info *lli = ll_i2info(inode);
3486 struct ll_sb_info *sbi = ll_i2sbi(inode);
3487 struct ldlm_lock *lock;
3488 struct lustre_md md = { NULL };
3489 struct cl_object_conf conf;
3492 bool wait_layout = false;
3494 LASSERT(lustre_handle_is_used(lockh));
3496 lock = ldlm_handle2lock(lockh);
3498 LASSERT(ldlm_has_layout(lock));
3500 LDLM_DEBUG(lock, "File "DFID"(%p) being reconfigured: %d",
3501 PFID(&lli->lli_fid), inode, reconf);
3503 /* in case this is a caching lock and reinstate with new inode */
3504 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
3506 lock_res_and_lock(lock);
3507 lvb_ready = ldlm_is_lvb_ready(lock);
3508 unlock_res_and_lock(lock);
3509 /* checking lvb_ready is racy but this is okay. The worst case is
3510 * that multi processes may configure the file on the same time.
3512 if (lvb_ready || !reconf) {
3515 /* layout_gen must be valid if layout lock is not
3516 * cancelled and stripe has already set
3518 *gen = ll_layout_version_get(lli);
3524 rc = ll_layout_fetch(inode, lock);
3528 /* for layout lock, lmm is returned in lock's lvb.
3529 * lvb_data is immutable if the lock is held so it's safe to access it
3530 * without res lock. See the description in ldlm_lock_decref_internal()
3531 * for the condition to free lvb_data of layout lock
3533 if (lock->l_lvb_data) {
3534 rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm,
3535 lock->l_lvb_data, lock->l_lvb_len);
3537 *gen = LL_LAYOUT_GEN_EMPTY;
3539 *gen = md.lsm->lsm_layout_gen;
3542 CERROR("%s: file " DFID " unpackmd error: %d\n",
3543 ll_get_fsname(inode->i_sb, NULL, 0),
3544 PFID(&lli->lli_fid), rc);
3550 /* set layout to file. Unlikely this will fail as old layout was
3553 memset(&conf, 0, sizeof(conf));
3554 conf.coc_opc = OBJECT_CONF_SET;
3555 conf.coc_inode = inode;
3556 conf.coc_lock = lock;
3557 conf.u.coc_md = &md;
3558 rc = ll_layout_conf(inode, &conf);
3561 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
3563 /* refresh layout failed, need to wait */
3564 wait_layout = rc == -EBUSY;
3567 LDLM_LOCK_PUT(lock);
3568 ldlm_lock_decref(lockh, mode);
3570 /* wait for IO to complete if it's still being used. */
3572 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
3573 ll_get_fsname(inode->i_sb, NULL, 0),
3574 PFID(&lli->lli_fid), inode);
3576 memset(&conf, 0, sizeof(conf));
3577 conf.coc_opc = OBJECT_CONF_WAIT;
3578 conf.coc_inode = inode;
3579 rc = ll_layout_conf(inode, &conf);
3583 CDEBUG(D_INODE, "%s: file="DFID" waiting layout return: %d.\n",
3584 ll_get_fsname(inode->i_sb, NULL, 0),
3585 PFID(&lli->lli_fid), rc);
3591 * This function checks if there exists a LAYOUT lock on the client side,
3592 * or enqueues it if it doesn't have one in cache.
3594 * This function will not hold layout lock so it may be revoked any time after
3595 * this function returns. Any operations depend on layout should be redone
3598 * This function should be called before lov_io_init() to get an uptodate
3599 * layout version, the caller should save the version number and after IO
3600 * is finished, this function should be called again to verify that layout
3601 * is not changed during IO time.
3603 int ll_layout_refresh(struct inode *inode, __u32 *gen)
3605 struct ll_inode_info *lli = ll_i2info(inode);
3606 struct ll_sb_info *sbi = ll_i2sbi(inode);
3607 struct md_op_data *op_data;
3608 struct lookup_intent it;
3609 struct lustre_handle lockh;
3610 enum ldlm_mode mode;
3611 struct ldlm_enqueue_info einfo = {
3612 .ei_type = LDLM_IBITS,
3614 .ei_cb_bl = &ll_md_blocking_ast,
3615 .ei_cb_cp = &ldlm_completion_ast,
3619 *gen = ll_layout_version_get(lli);
3620 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != LL_LAYOUT_GEN_NONE)
3624 LASSERT(fid_is_sane(ll_inode2fid(inode)));
3625 LASSERT(S_ISREG(inode->i_mode));
3627 /* take layout lock mutex to enqueue layout lock exclusively. */
3628 mutex_lock(&lli->lli_layout_mutex);
3631 /* mostly layout lock is caching on the local side, so try to match
3632 * it before grabbing layout lock mutex.
3634 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
3635 LCK_CR | LCK_CW | LCK_PR | LCK_PW);
3636 if (mode != 0) { /* hit cached lock */
3637 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3641 mutex_unlock(&lli->lli_layout_mutex);
3645 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
3646 0, 0, LUSTRE_OPC_ANY, NULL);
3647 if (IS_ERR(op_data)) {
3648 mutex_unlock(&lli->lli_layout_mutex);
3649 return PTR_ERR(op_data);
3652 /* have to enqueue one */
3653 memset(&it, 0, sizeof(it));
3654 it.it_op = IT_LAYOUT;
3655 lockh.cookie = 0ULL;
3657 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
3658 ll_get_fsname(inode->i_sb, NULL, 0),
3659 PFID(&lli->lli_fid), inode);
3661 rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL, &it, op_data, &lockh, 0);
3662 ptlrpc_req_finished(it.it_request);
3663 it.it_request = NULL;
3665 ll_finish_md_op_data(op_data);
3667 mode = it.it_lock_mode;
3668 it.it_lock_mode = 0;
3669 ll_intent_drop_lock(&it);
3672 /* set lock data in case this is a new lock */
3673 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
3674 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3678 mutex_unlock(&lli->lli_layout_mutex);
3684 * This function send a restore request to the MDT
3686 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
3688 struct hsm_user_request *hur;
3691 len = sizeof(struct hsm_user_request) +
3692 sizeof(struct hsm_user_item);
3693 hur = kzalloc(len, GFP_NOFS);
3697 hur->hur_request.hr_action = HUA_RESTORE;
3698 hur->hur_request.hr_archive_id = 0;
3699 hur->hur_request.hr_flags = 0;
3700 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
3701 sizeof(hur->hur_user_item[0].hui_fid));
3702 hur->hur_user_item[0].hui_extent.offset = offset;
3703 hur->hur_user_item[0].hui_extent.length = length;
3704 hur->hur_request.hr_itemcount = 1;
3705 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,